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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\Attribute\Data;
/**
* EAV Entity Attribute Image File Data Model
*
* @author Magento Core Team <core@magentocommerce.com>
*/
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)
*/
protected function _validateByRules($value)
{
$label = __($this->getAttribute()->getStoreLabel());
$rules = $this->getAttribute()->getValidateRules();
$imageProp = @getimagesize($value['tmp_name']);
$allowImageTypes = [1 => 'gif', 2 => 'jpg', 3 => 'png'];
if (!isset($allowImageTypes[$imageProp[2]])) {
return [__('"%1" is not a valid image format', $label)];
}
// modify image name
$extension = pathinfo($value['name'], PATHINFO_EXTENSION);
if ($extension != $allowImageTypes[$imageProp[2]]) {
$value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $allowImageTypes[$imageProp[2]];
}
$errors = [];
if (!empty($rules['max_file_size'])) {
$size = $value['size'];
if ($rules['max_file_size'] < $size) {
$errors[] = __('"%1" exceeds the allowed file size.', $label);
}
}
if (!empty($rules['max_image_width'])) {
if ($rules['max_image_width'] < $imageProp[0]) {
$r = $rules['max_image_width'];
$errors[] = __('"%1" width exceeds allowed value of %2 px.', $label, $r);
}
}
if (!empty($rules['max_image_height'])) {
if ($rules['max_image_height'] < $imageProp[1]) {
$r = $rules['max_image_height'];
$errors[] = __('"%1" height exceeds allowed value of %2 px.', $label, $r);
}
}
return $errors;
}
}