Magento Extensions Rating 2024
EXTENSIONS BY CATEGORY
B2B (Business-To-Business)
Blog
Customer
ERP (Enterprise Resource Planning)
Mega Menu
One Step Checkout
Order
POS (Point Of Sale)
Search
Shopping Cart
Sitemap
SEO
Social
Stock & Inventory Management
EXTENSIONS BY DEVELOPER
aheadWorks
Amasty
Boost My Shop
BSS Commerce
Magestore
MageWorx
Mirasvit
Templates Master
Wyomind
XTENTO
Magento 2 Documentation
Magento 2 Documentation
2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
vendor
magento
framework
Api
ImageContentValidator.php
Go to the documentation of this file.
1
<?php
8
namespace
Magento\Framework\Api
;
9
10
use
Magento\Framework\Api\Data\ImageContentInterface
;
11
use
Magento\Framework\Exception\InputException
;
12
use
Magento\Framework\Phrase
;
13
17
class
ImageContentValidator
implements
ImageContentValidatorInterface
18
{
22
private
$defaultMimeTypes = [
23
'image/jpg'
,
24
'image/jpeg'
,
25
'image/gif'
,
26
'image/png'
,
27
];
28
32
private
$allowedMimeTypes;
33
37
public
function
__construct
(
38
array $allowedMimeTypes = []
39
) {
40
$this->allowedMimeTypes = array_merge($this->defaultMimeTypes, $allowedMimeTypes);
41
}
42
50
public
function
isValid
(
ImageContentInterface
$imageContent
)
51
{
52
$fileContent = @base64_decode(
$imageContent
->getBase64EncodedData(),
true
);
53
if
(empty($fileContent)) {
54
throw
new
InputException
(
new
Phrase
(
'The image content must be valid base64 encoded data.'
));
55
}
56
$imageProperties = @getimagesizefromstring($fileContent);
57
if
(empty($imageProperties)) {
58
throw
new
InputException
(
new
Phrase
(
'The image content must be valid base64 encoded data.'
));
59
}
60
$sourceMimeType = $imageProperties[
'mime'
];
61
if
($sourceMimeType !=
$imageContent
->getType() || !$this->
isMimeTypeValid
($sourceMimeType)) {
62
throw
new
InputException
(
new
Phrase
(
'The image MIME type is not valid or not supported.'
));
63
}
64
if
(!$this->
isNameValid
($imageContent->
getName
())) {
65
throw
new
InputException
(
new
Phrase
(
'Provided image name contains forbidden characters.'
));
66
}
67
return
true
;
68
}
69
76
protected
function
isMimeTypeValid
($mimeType)
77
{
78
return
in_array($mimeType, $this->allowedMimeTypes);
79
}
80
87
protected
function
isNameValid
(
$name
)
88
{
89
// Cannot contain \ / : * ? " < > |
90
if
(!preg_match(
'/^[^\\/?*:";<>()|{}\\\\]+$/'
,
$name
)) {
91
return
false
;
92
}
93
return
true
;
94
}
95
}
Magento\Framework\Api\Data\ImageContentInterface\getName
getName()
Magento\Framework\Api
Definition:
AbstractExtensibleObject.php:6
Magento\Framework\Api\ImageContentValidator\isMimeTypeValid
isMimeTypeValid($mimeType)
Definition:
ImageContentValidator.php:76
$imageContent
$imageContent
Definition:
product_simple_with_media_gallery_entries.php:26
Magento\Framework\Exception\InputException
Definition:
InputException.php:17
Magento\Framework\Api\ImageContentValidator\__construct
__construct(array $allowedMimeTypes=[])
Definition:
ImageContentValidator.php:37
Magento\Framework\Api\ImageContentValidator
Definition:
ImageContentValidator.php:17
Magento\Framework\Api\ImageContentValidator\isNameValid
isNameValid($name)
Definition:
ImageContentValidator.php:87
Magento\Framework\Api\Data\ImageContentInterface
Definition:
ImageContentInterface.php:15
Magento\Framework\Api\ImageContentValidator\isValid
isValid(ImageContentInterface $imageContent)
Definition:
ImageContentValidator.php:50
Magento\Framework\Phrase
Definition:
Phrase.php:17
Magento\Framework\Phrase
Magento\Framework\Api\ImageContentValidatorInterface
Definition:
ImageContentValidatorInterface.php:18
$name
if(!isset($_GET['name'])) $name
Definition:
log.php:14