-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Expand file tree
/
Copy pathImage.php
More file actions
76 lines (69 loc) · 2.97 KB
/
Image.php
File metadata and controls
76 lines (69 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Copyright 2013 Adobe
* All Rights Reserved.
*/
namespace Magento\Eav\Model\Attribute\Data;
use Magento\Framework\Filesystem\ExtendedDriverInterface;
/**
* EAV Entity Attribute Image File Data Model
*/
class Image extends \Magento\Eav\Model\Attribute\Data\File
{
/**
* Validate file by attribute validate rules
*
* Return array of errors
*
* @param array $value
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _validateByRules($value)
{
$label = __($this->getAttribute()->getStoreLabel());
$rules = $this->getAttribute()->getValidateRules();
$localStorage = !$this->_directory->getDriver() instanceof ExtendedDriverInterface;
$imageProp = $localStorage
// @phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged,Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
? @getimagesize($value['tmp_name'])
: $this->_directory->getDriver()->getMetadata($value['tmp_name']);
$allowImageTypes = ['gif', 'jpg', 'jpeg', 'png'];
if (!isset($imageProp['extension']) && isset($imageProp[2])) {
$extensionsMap = [1 => 'gif', 2 => 'jpg', 3 => 'png'];
$imageProp['extension'] = $extensionsMap[$imageProp[2]] ?? null;
}
if (!\in_array($imageProp['extension'], $allowImageTypes, true)) {
return [__('"%1" is not a valid image format', $label)];
}
// modify image name
// phpcs:ignore Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
$extension = pathinfo($value['name'], PATHINFO_EXTENSION);
if ($extension !== $imageProp['extension']) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction.DiscouragedWithAlternative
$value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $imageProp['extension'];
}
$errors = [];
if (!empty($rules['max_file_size'])) {
$size = $value['size'];
if ($rules['max_file_size'] < $size) {
$errors[] = __('"%1" exceeds the allowed file size.', $label);
}
}
$imageWidth = $imageProp['extra']['image-width'] ?? $imageProp[0];
if (!empty($rules['max_image_width']) && !empty($imageWidth)
&& ($rules['max_image_width'] < $imageWidth)) {
$r = $rules['max_image_width'];
$errors[] = __('"%1" width exceeds allowed value of %2 px.', $label, $r);
}
$imageHeight = $imageProp['extra']['image-height'] ?? $imageProp[1];
if (!empty($rules['max_image_height']) && !empty($imageHeight)
&& ($rules['max_image_height'] < $imageHeight)) {
$r = $rules['max_image_height'];
$errors[] = __('"%1" height exceeds allowed value of %2 px.', $label, $r);
}
return $errors;
}
}