Hatimeria_Crop - Version 0.1.0

Version Notes

Fix bugs

Download this release

Release Info

Developer Magento Core Team
Extension Hatimeria_Crop
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

app/code/local/Hatimeria/Crop/Helper/Data.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Hatimeria_Core_Helper_Data extends Mage_Core_Helper_Abstract
4
+ {
5
+
6
+ }
app/code/local/Hatimeria/Crop/Helper/Image.php ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Hatimeria_Crop_Helper_Image extends Mage_Catalog_Helper_Image
4
+ {
5
+ /**
6
+ * Is sheduled to crop?
7
+ *
8
+ * @var bool
9
+ */
10
+ protected $_scheduleCrop = false;
11
+
12
+ /**
13
+ * Map of types images to geter of attribute
14
+ *
15
+ * @var array
16
+ */
17
+ protected $modeMap = array(
18
+ 'small_image' => 'getSmallImageCrop',
19
+ 'thumbnail' => 'getThumbnailImageCrop'
20
+ );
21
+
22
+
23
+ /**
24
+ * Map of crop coords
25
+ * [filename => <coords>]
26
+ *
27
+ * @var type
28
+ */
29
+ protected $_cropMap = array();
30
+
31
+ /**
32
+ * Resets helper
33
+ */
34
+ protected function _reset()
35
+ {
36
+ parent::_reset();
37
+ $this->_scheduleCrop = false;
38
+ $this->_cropMap = array();
39
+ }
40
+
41
+ /**
42
+ *
43
+ * @param type $map
44
+ */
45
+ public function setCropMap($map)
46
+ {
47
+ $this->_cropMap = $map;
48
+ }
49
+
50
+ /**
51
+ * Defered crop image
52
+ *
53
+ * @return Hatimeria_Crop_Helper_Image
54
+ */
55
+ public function crop()
56
+ {
57
+ $product = $this->getProduct();
58
+ $destination = $this->_getModel()->getDestinationSubdir();
59
+ $method = $this->modeMap[$destination];
60
+ $values = '';
61
+
62
+ if (is_callable(array($product, $method)))
63
+ {
64
+ $json = call_user_func(array($product, $method));
65
+ }
66
+
67
+ if (!$this->isJson($json))
68
+ {
69
+ return $this;
70
+ }
71
+
72
+ $this->_scheduleCrop = true;
73
+ $this->setCropMap(json_decode($json, true));
74
+
75
+ return $this;
76
+ }
77
+
78
+ /**
79
+ * Gets coords from CropMap by filename
80
+ *
81
+ */
82
+ public function getCoordsFromCropMap($file)
83
+ {
84
+ $index = preg_replace("/(^.*)\//", '', $file);
85
+
86
+ if (isset($this->_cropMap[$index]))
87
+ {
88
+ return $this->_cropMap[$index] ;
89
+ }
90
+ else
91
+ {
92
+ return null;
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Validates string
98
+ *
99
+ * @param type $str
100
+ * @return type
101
+ */
102
+ public function isValid($str)
103
+ {
104
+ if (!$str)
105
+ {
106
+ return false;
107
+ }
108
+
109
+ return preg_match('/^[0-9]+:[0-9]+,[0-9]+:[0-9]+,[0-9]+:[0-9]+,[0-9]+$/', $str);
110
+ }
111
+
112
+ /**
113
+ * Validates JSON data
114
+ *
115
+ * @return bool
116
+ */
117
+ public function isJson($json)
118
+ {
119
+ $data = json_decode($json, true);
120
+
121
+ return is_array($data);
122
+ }
123
+
124
+ /**
125
+ * Retrieves parameters
126
+ *
127
+ * @param type $strAttributes
128
+ * @return array
129
+ */
130
+ protected function getAttributeParameters($strAttributes)
131
+ {
132
+ list ($size, $coords, $xRatios, $yRatios) = explode(":", $strAttributes);
133
+ $coord = explode(',', $coords);
134
+ $xRatio = explode(',', $xRatios);
135
+ $yRatio = explode(',', $yRatios);
136
+
137
+ return array(
138
+ 'size' => $size,
139
+ 'x' => $coord[0],
140
+ 'y' => $coord[1],
141
+ 'x_ratio_original' => $xRatio[0],
142
+ 'x_ratio_resized' => $xRatio[1],
143
+ 'y_ratio_original' => $yRatio[0],
144
+ 'y_ratio_resized' => $yRatio[1],
145
+ );
146
+ }
147
+
148
+ /**
149
+ * Prepares parameters ready to send.
150
+ *
151
+ * @param array $attrs
152
+ * @return array
153
+ */
154
+ protected function prepareParameters($attrs)
155
+ {
156
+ if ($this->isVirtualRect($attrs))
157
+ {
158
+ /*
159
+ * x_ratio_original = 100%
160
+ * x_ratio_resized = ?%
161
+ *
162
+ * percent = (x_ratio_resized * 100) / x_ratio_original
163
+ */
164
+ $percent = self::scale($attrs['x_ratio_original'], $attrs['x_ratio_resized'], 100);
165
+ $percent = 100 / $percent;
166
+
167
+ $attrs['x'] = round($attrs['x'] * $percent);
168
+ $attrs['y'] = round($attrs['y'] * $percent);
169
+ $attrs['size'] = round($attrs['size'] * $percent);
170
+ }
171
+
172
+ return array(
173
+ 'left' => $attrs['x'],
174
+ 'top' => $attrs['y'],
175
+ 'right' => $attrs['x_ratio_original'] - $attrs['size'] - $attrs['x'],
176
+ 'bottom' => $attrs['y_ratio_original'] - $attrs['size'] - $attrs['y'],
177
+ );
178
+ }
179
+
180
+ /**
181
+ * Check if size of rectangle is virtual
182
+ *
183
+ * @param $attrs
184
+ * @return type
185
+ */
186
+ protected function isVirtualRect($attrs)
187
+ {
188
+ return ($attrs['x_ratio_original'] != $attrs['x_ratio_resized'] && $attrs['y_ratio_original'] != $attrs['y_ratio_resized']) ;
189
+ }
190
+
191
+ /**
192
+ * Computes scale
193
+ *
194
+ * @param int $a
195
+ * @param int $b
196
+ * @param int $c
197
+ * @return int
198
+ */
199
+ protected static function scale($a, $b, $c)
200
+ {
201
+ return round(($b * $c) / $a);
202
+ }
203
+
204
+ /**
205
+ * Output
206
+ *
207
+ * @return string (url)
208
+ */
209
+ public function __toString()
210
+ {
211
+ try {
212
+ if( $this->getImageFile() ) {
213
+ $this->_getModel()->setBaseFile( $this->getImageFile() );
214
+ } else {
215
+ $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
216
+ }
217
+
218
+ if( $this->_getModel()->isCached() ) {
219
+ return $this->_getModel()->getUrl();
220
+ } else {
221
+ if( $this->_scheduleRotate ) {
222
+ $this->_getModel()->rotate( $this->getAngle() );
223
+ }
224
+
225
+ if ($this->_scheduleCrop) {
226
+ $strCoords = $this->getCoordsFromCropMap($this->_getModel()->getBaseFile());
227
+ if ($this->isValid($strCoords))
228
+ {
229
+ $coords = $this->prepareParameters($this->getAttributeParameters($strCoords));
230
+ $this->_getModel()->setCropAttributes($coords);
231
+ $this->_getModel()->crop();
232
+ }
233
+ }
234
+
235
+ if ($this->_scheduleResize) {
236
+ $this->_getModel()->resize();
237
+ }
238
+
239
+ if( $this->getWatermark() ) {
240
+ $this->_getModel()->setWatermark($this->getWatermark());
241
+ }
242
+
243
+ $url = $this->_getModel()->saveFile()->getUrl();
244
+ }
245
+ } catch( Exception $e ) {
246
+ $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
247
+ }
248
+ return $url;
249
+ }
250
+ }
app/code/local/Hatimeria/Crop/Model/Image.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Hatimeria_Crop_Model_Image extends Mage_Catalog_Model_Product_Image
4
+ {
5
+ /**
6
+ * Ctop parameters
7
+ *
8
+ * @var array
9
+ */
10
+ protected $_cropAttributes;
11
+
12
+ /**
13
+ * Sets attributes for crop
14
+ *
15
+ * @param array $attrs
16
+ */
17
+ public function setCropAttributes($attrs)
18
+ {
19
+ $this->_cropAttributes = $attrs;
20
+ }
21
+
22
+ /**
23
+ * Crops image
24
+ *
25
+ * @param array $attrs
26
+ * @return Hatimeria_Crop_Model_Image
27
+ */
28
+ public function crop()
29
+ {
30
+ $attrs = $this->_cropAttributes;
31
+
32
+ $this->getImageProcessor()->crop($attrs['bottom'], $attrs['top'], $attrs['right'], $attrs['left']);
33
+
34
+ return $this;
35
+ }
36
+ }
app/code/local/Hatimeria/Crop/Model/Resource/Eav/Mysql4/Setup.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Hatimeria_Crop_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
3
+ {
4
+ public function getDefaultEntities()
5
+ {
6
+ return array(
7
+ 'catalog_product' => array(
8
+ 'entity_model' => 'catalog/product',
9
+ 'attribute_model' => 'catalog/resource_eav_attribute',
10
+ 'table' => 'catalog/product',
11
+ 'additional_attribute_table' => 'catalog/eav_attribute',
12
+ 'entity_attribute_collection' => 'catalog/product_attribute_collection',
13
+ 'attributes' => array(
14
+ 'small_image_crop' => array(
15
+ 'group' => 'ImageInfo',
16
+ 'label' => 'Small crop info',
17
+ 'type' => 'varchar',
18
+ 'input' => 'text',
19
+ 'default' => '',
20
+ 'class' => '',
21
+ 'backend' => '',
22
+ 'frontend' => '',
23
+ 'source' => '',
24
+ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
25
+ 'visible' => true,
26
+ 'required' => false,
27
+ 'user_defined' => false,
28
+ 'searchable' => false,
29
+ 'filterable' => false,
30
+ 'comparable' => false,
31
+ 'visible_on_front' => true,
32
+ 'used_in_product_listing' => true,
33
+ 'visible_in_advanced_search' => false,
34
+ 'unique' => false
35
+ ),
36
+ 'thumbnail_image_crop' => array(
37
+ 'group' => 'ImageInfo',
38
+ 'label' => 'Thumbnail crop info',
39
+ 'type' => 'varchar',
40
+ 'input' => 'text',
41
+ 'default' => '',
42
+ 'class' => '',
43
+ 'backend' => '',
44
+ 'frontend' => '',
45
+ 'source' => '',
46
+ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
47
+ 'visible' => true,
48
+ 'required' => false,
49
+ 'user_defined' => false,
50
+ 'searchable' => false,
51
+ 'filterable' => false,
52
+ 'comparable' => false,
53
+ 'visible_on_front' => true,
54
+ 'used_in_product_listing' => true,
55
+ 'visible_in_advanced_search' => false,
56
+ 'unique' => false
57
+ ),
58
+ )
59
+ ),
60
+ );
61
+ }
62
+ }
app/code/local/Hatimeria/Crop/etc/config.xml ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Hatimeria_Crop>
5
+ <version>0.1.0</version>
6
+ </Hatimeria_Crop>
7
+ </modules>
8
+ <global>
9
+ <helpers>
10
+ <catalog>
11
+ <rewrite>
12
+ <image>Hatimeria_Crop_Helper_Image</image>
13
+ </rewrite>
14
+ </catalog>
15
+ </helpers>
16
+ <models>
17
+ <catalog>
18
+ <rewrite>
19
+ <product_image>Hatimeria_Crop_Model_Image</product_image>
20
+ </rewrite>
21
+ </catalog>
22
+ <crop>
23
+ <class>Hatimeria_Crop</class>
24
+ </crop>
25
+ </models>
26
+ <resources>
27
+ <crop_setup>
28
+ <setup>
29
+ <module>Hatimeria_Crop</module>
30
+ <class>Hatimeria_Crop_Model_Resource_Eav_Mysql4_Setup</class>
31
+ </setup>
32
+ <connection>
33
+ <use>core_setup</use>
34
+ </connection>
35
+ </crop_setup>
36
+ <crop_write>
37
+ <connection>
38
+ <use>core_write</use>
39
+ </connection>
40
+ </crop_write>
41
+ <crop_read>
42
+ <connection>
43
+ <use>core_read</use>
44
+ </connection>
45
+ </crop_read>
46
+ </resources>
47
+ </global>
48
+ <adminhtml>
49
+ <layout>
50
+ <updates>
51
+ <crop>
52
+ <file>crop.xml</file>
53
+ </crop>
54
+ </updates>
55
+ </layout>
56
+ </adminhtml>
57
+ </config>
app/code/local/Hatimeria/Crop/sql/crop_setup/mysql4-install-0.1.1.php ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ <?php
2
+
3
+ $installer = $this;
4
+ $installer->installEntities();
app/etc/modules/Hatimeria_Crop.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Hatimeria_Crop>
5
+ <active>true</active>
6
+ <codePool>local</codePool>
7
+ </Hatimeria_Crop>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Hatimeria_Crop</name>
4
+ <version>0.1.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.opensource.org/licenses/OSL-3.0">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Module to crop images(eg. product image or thumbnail ) on the product page/category page or other page where you want to crop image.</summary>
10
+ <description>Crop module enable two extra option to every image on the Images tab (in admin panel edit product page):&#xD;
11
+ Crop Thumbnail&#xD;
12
+ Crop Small Image&#xD;
13
+ &#xD;
14
+ User could select interesting part of image and set as product image or product thumbnail.&#xD;
15
+ &#xD;
16
+ Module write selected image coordinates to ImageInfo tab (eg. 664:26,99:1820,718:2022,798)</description>
17
+ <notes>Fix bugs</notes>
18
+ <authors><author><name>Hatimeria</name><user>auto-converted</user><email>magento@hatimeria.pl</email></author></authors>
19
+ <date>2011-11-18</date>
20
+ <time>15:11:15</time>
21
+ <contents><target name="magelocal"><dir name="Hatimeria"><dir name="Crop"><dir name="Helper"><file name="Data.php" hash="6237fe9e1a0ba8d5132afa9d1b1faacc"/><file name="Image.php" hash="bd8f6582bc49ed1cccdce58a687d3636"/></dir><dir name="Model"><dir name="Resource"><dir name="Eav"><dir name="Mysql4"><file name="Setup.php" hash="2d195b864ae61eb9b87d9fdaebef0a83"/></dir></dir></dir><file name="Image.php" hash="7a7900bd9f189c9a34c209e53b2d141b"/></dir><dir name="etc"><file name="config.xml" hash="c7b6c411e465ec6698d0fd4c7ec83cf1"/></dir><dir name="sql"><dir name="crop_setup"><file name="mysql4-install-0.1.1.php" hash="2737bf7565d398549b5cadc789f6e31e"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Hatimeria_Crop.xml" hash="a1a31125678cb721f01bd64fab7b6fc3"/></dir></target></contents>
22
+ <compatible/>
23
+ <dependencies/>
24
+ </package>