Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Image.php
Go to the documentation of this file.
1 <?php
7 
13 class Image extends \Magento\Eav\Model\Attribute\Data\File
14 {
24  protected function _validateByRules($value)
25  {
26  $label = __($this->getAttribute()->getStoreLabel());
27  $rules = $this->getAttribute()->getValidateRules();
28 
29  $imageProp = @getimagesize($value['tmp_name']);
30 
31  $allowImageTypes = [1 => 'gif', 2 => 'jpg', 3 => 'png'];
32 
33  if (!isset($allowImageTypes[$imageProp[2]])) {
34  return [__('"%1" is not a valid image format', $label)];
35  }
36 
37  // modify image name
38  $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
39  if ($extension != $allowImageTypes[$imageProp[2]]) {
40  $value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $allowImageTypes[$imageProp[2]];
41  }
42 
43  $errors = [];
44  if (!empty($rules['max_file_size'])) {
45  $size = $value['size'];
46  if ($rules['max_file_size'] < $size) {
47  $errors[] = __('"%1" exceeds the allowed file size.', $label);
48  }
49  }
50 
51  if (!empty($rules['max_image_width'])) {
52  if ($rules['max_image_width'] < $imageProp[0]) {
53  $r = $rules['max_image_width'];
54  $errors[] = __('"%1" width exceeds allowed value of %2 px.', $label, $r);
55  }
56  }
57  if (!empty($rules['max_image_heght'])) {
58  if ($rules['max_image_heght'] < $imageProp[1]) {
59  $r = $rules['max_image_heght'];
60  $errors[] = __('"%1" height exceeds allowed value of %2 px.', $label, $r);
61  }
62  }
63 
64  return $errors;
65  }
66 }
__()
Definition: __.php:13
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$errors
Definition: overview.phtml:9