1: <?php
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: class Mage_Eav_Model_Attribute_Data_Image extends Mage_Eav_Model_Attribute_Data_File
36: {
37: 38: 39: 40: 41: 42: 43:
44: protected function _validateByRules($value)
45: {
46: $label = Mage::helper('eav')->__($this->getAttribute()->getStoreLabel());
47: $rules = $this->getAttribute()->getValidateRules();
48:
49: $imageProp = @getimagesize($value['tmp_name']);
50:
51: if (!is_uploaded_file($value['tmp_name']) || !$imageProp) {
52: return array(
53: Mage::helper('eav')->__('"%s" is not a valid file', $label)
54: );
55: }
56:
57: $allowImageTypes = array(
58: 1 => 'gif',
59: 2 => 'jpg',
60: 3 => 'png',
61: );
62:
63: if (!isset($allowImageTypes[$imageProp[2]])) {
64: return array(
65: Mage::helper('eav')->__('"%s" is not a valid image format', $label)
66: );
67: }
68:
69:
70: $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
71: if ($extension != $allowImageTypes[$imageProp[2]]) {
72: $value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $allowImageTypes[$imageProp[2]];
73: }
74:
75: $errors = array();
76: if (!empty($rules['max_file_size'])) {
77: $size = $value['size'];
78: if ($rules['max_file_size'] < $size) {
79: $errors[] = Mage::helper('eav')->__('"%s" exceeds the allowed file size.', $label);
80: };
81: }
82:
83: if (!empty($rules['max_image_width'])) {
84: if ($rules['max_image_width'] < $imageProp[0]) {
85: $r = $rules['max_image_width'];
86: $errors[] = Mage::helper('eav')->__('"%s" width exceeds allowed value of %s px.', $label, $r);
87: };
88: }
89: if (!empty($rules['max_image_heght'])) {
90: if ($rules['max_image_heght'] < $imageProp[1]) {
91: $r = $rules['max_image_heght'];
92: $errors[] = Mage::helper('eav')->__('"%s" height exceeds allowed value of %s px.', $label, $r);
93: };
94: }
95:
96: return $errors;
97: }
98: }
99: