Skip to content

Commit 1d3e030

Browse files
committed
Remove rewrite of product image model and fix Varien Image private methods
1 parent 4c3933f commit 1d3e030

File tree

4 files changed

+175
-11
lines changed

4 files changed

+175
-11
lines changed

app/code/local/Oggetto/AdaptiveResize/Helper/Catalog/Image.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @category Oggetto
2121
* @package Oggetto_AdaptiveResize
22-
* @copyright Copyright (C) 2011 Oggetto Web ltd (http://oggettoweb.com/)
22+
* @copyright Copyright (C) 2012 Oggetto Web ltd (http://oggettoweb.com/)
2323
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
2424
*/
2525

@@ -108,4 +108,36 @@ public function __toString()
108108
return $url;
109109
}
110110

111+
/**
112+
* Init Image processor model
113+
*
114+
* Rewrited to change model
115+
*
116+
* @param Mage_Catalog_Model_Product $product product
117+
* @param string $attributeName attribute name
118+
* @param string $imageFile image file name
119+
*
120+
* @return \Oggetto_AdaptiveResize_Helper_Catalog_Image
121+
*/
122+
public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile = null)
123+
{
124+
$this->_reset();
125+
$this->_setModel(Mage::getModel('adaptiveResize/catalog_product_image'));
126+
$this->_getModel()->setDestinationSubdir($attributeName);
127+
$this->setProduct($product);
128+
$subdir = $this->_getModel()->getDestinationSubdir();
129+
$this->setWatermark(Mage::getStoreConfig("design/watermark/{$subdir}_image"));
130+
$this->setWatermarkImageOpacity(Mage::getStoreConfig("design/watermark/{$subdir}_imageOpacity"));
131+
$this->setWatermarkPosition(Mage::getStoreConfig("design/watermark/{$subdir}_position"));
132+
$this->setWatermarkSize(Mage::getStoreConfig("design/watermark/{$subdir}_size"));
133+
134+
if ($imageFile) {
135+
$this->setImageFile($imageFile);
136+
} else {
137+
// add for work original size
138+
$this->_getModel()->setBaseFile($this->getProduct()->getData($subdir));
139+
}
140+
return $this;
141+
}
142+
111143
}

app/code/local/Oggetto/AdaptiveResize/Model/Catalog/Product/Image.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* that is bundled with this package in the file LICENSE.txt.
1010
* It is also available through the world-wide-web at this URL:
1111
* http://opensource.org/licenses/osl-3.0.php
12-
*
12+
*
1313
* DISCLAIMER
1414
*
1515
* Do not edit or add to this file if you wish to upgrade
@@ -19,7 +19,7 @@
1919
*
2020
* @category Oggetto
2121
* @package Oggetto_AdaptiveResize
22-
* @copyright Copyright (C) 2011 Oggetto Web ltd (http://oggettoweb.com/)
22+
* @copyright Copyright (C) 2012 Oggetto Web ltd (http://oggettoweb.com/)
2323
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
2424
*/
2525

@@ -35,8 +35,8 @@ class Oggetto_AdaptiveResize_Model_Catalog_Product_Image extends Mage_Catalog_Mo
3535
{
3636
/**
3737
* Adaptive Resize
38-
*
39-
* @return Oggetto_AdaptiveResize_Model_Catalog_Product_Image
38+
*
39+
* @return Oggetto_AdaptiveResize_Model_Catalog_Product_Image
4040
*/
4141
public function adaptiveResize()
4242
{
@@ -47,4 +47,23 @@ public function adaptiveResize()
4747
return $this;
4848
}
4949

50+
/**
51+
* Get Image Processor
52+
*
53+
* @return Varien_Image
54+
*/
55+
public function getImageProcessor()
56+
{
57+
if( !$this->_processor ) {
58+
$this->_processor = Mage::getModel('adaptiveResize/image', $this->getBaseFile());
59+
}
60+
$this->_processor->keepAspectRatio($this->_keepAspectRatio);
61+
$this->_processor->keepFrame($this->_keepFrame);
62+
$this->_processor->keepTransparency($this->_keepTransparency);
63+
$this->_processor->constrainOnly($this->_constrainOnly);
64+
$this->_processor->backgroundColor($this->_backgroundColor);
65+
$this->_processor->quality($this->_quality);
66+
return $this->_processor;
67+
}
68+
5069
}

app/code/local/Oggetto/AdaptiveResize/Model/Image/Adapter/Gd2.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,122 @@ public function adaptiveResize($frameWidth = null, $frameHeight = null)
7676
$this->refreshImageDimensions();
7777
}
7878

79+
/**
80+
* Get Transparency
81+
*
82+
* @param resource $imageResource image resourc
83+
* @param string $fileType fill type
84+
* @param bool &$isAlpha is alpha channel
85+
* @param bool &$isTrueColor is image true color
86+
*
87+
* @return boolean
88+
*/
89+
private function _getTransparency($imageResource, $fileType, &$isAlpha = false, &$isTrueColor = false)
90+
{
91+
$isAlpha = false;
92+
$isTrueColor = false;
93+
// assume that transparency is supported by gif/png only
94+
if ((IMAGETYPE_GIF === $fileType) || (IMAGETYPE_PNG === $fileType)) {
95+
// check for specific transparent color
96+
$transparentIndex = imagecolortransparent($imageResource);
97+
if ($transparentIndex >= 0) {
98+
return $transparentIndex;
99+
} elseif (IMAGETYPE_PNG === $fileType) {
100+
// assume that truecolor PNG has transparency
101+
$isAlpha = $this->checkAlpha($this->_fileName);
102+
$isTrueColor = true;
103+
return $transparentIndex; // -1
104+
}
105+
}
106+
if (IMAGETYPE_JPEG === $fileType) {
107+
$isTrueColor = true;
108+
}
109+
return false;
110+
}
111+
112+
/**
113+
* Fill background color
114+
*
115+
* @param resource &$imageResourceTo image resource
116+
*
117+
* @return string
118+
* @throws Exception
119+
*/
120+
private function _fillBackgroundColor(&$imageResourceTo)
121+
{
122+
// try to keep transparency, if any
123+
if ($this->_keepTransparency) {
124+
$isAlpha = false;
125+
$transparentIndex = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
126+
try {
127+
// fill truecolor png with alpha transparency
128+
if ($isAlpha) {
129+
130+
if (!imagealphablending($imageResourceTo, false)) {
131+
throw new Exception('Failed to set alpha blending for PNG image.');
132+
}
133+
$transparentAlphaColor = imagecolorallocatealpha($imageResourceTo, 0, 0, 0, 127);
134+
if (false === $transparentAlphaColor) {
135+
throw new Exception('Failed to allocate alpha transparency for PNG image.');
136+
}
137+
if (!imagefill($imageResourceTo, 0, 0, $transparentAlphaColor)) {
138+
throw new Exception('Failed to fill PNG image with alpha transparency.');
139+
}
140+
if (!imagesavealpha($imageResourceTo, true)) {
141+
throw new Exception('Failed to save alpha transparency into PNG image.');
142+
}
143+
144+
return $transparentAlphaColor;
145+
} elseif (false !== $transparentIndex) {
146+
// fill image with indexed non-alpha transparency
147+
list($r, $g, $b) = array_values(imagecolorsforindex($this->_imageHandler, $transparentIndex));
148+
$transparentColor = imagecolorallocate($imageResourceTo, $r, $g, $b);
149+
if (false === $transparentColor) {
150+
throw new Exception('Failed to allocate transparent color for image.');
151+
}
152+
if (!imagefill($imageResourceTo, 0, 0, $transparentColor)) {
153+
throw new Exception('Failed to fill image with transparency.');
154+
}
155+
imagecolortransparent($imageResourceTo, $transparentColor);
156+
return $transparentColor;
157+
}
158+
}
159+
catch (Exception $e) {
160+
// fallback to default background color
161+
}
162+
}
163+
list($r, $g, $b) = $this->_backgroundColor;
164+
$color = imagecolorallocate($imageResourceTo, $r, $g, $b);
165+
if (!imagefill($imageResourceTo, 0, 0, $color)) {
166+
throw new Exception("Failed to fill image background with color {$r} {$g} {$b}.");
167+
}
168+
169+
return $color;
170+
}
171+
172+
/**
173+
* Refresh image dimensions
174+
*
175+
* @return void
176+
*/
177+
private function refreshImageDimensions()
178+
{
179+
$this->_imageSrcWidth = imagesx($this->_imageHandler);
180+
$this->_imageSrcHeight = imagesy($this->_imageHandler);
181+
}
182+
183+
/**
184+
* Fixes saving PNG alpha channel
185+
*
186+
* @param resource $imageHandler image handler
187+
*
188+
* @return void
189+
*/
190+
private function _saveAlpha($imageHandler)
191+
{
192+
$background = imagecolorallocate($imageHandler, 0, 0, 0);
193+
ImageColorTransparent($imageHandler, $background);
194+
imagealphablending($imageHandler, false);
195+
imagesavealpha($imageHandler, true);
196+
}
79197
}

app/code/local/Oggetto/AdaptiveResize/etc/config.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* that is bundled with this package in the file LICENSE.txt.
1010
* It is also available through the world-wide-web at this URL:
1111
* http://opensource.org/licenses/osl-3.0.php
12-
*
12+
*
1313
* DISCLAIMER
1414
*
1515
* Do not edit or add to this file if you wish to upgrade
@@ -33,11 +33,6 @@
3333
</catalog>
3434
</helpers>
3535
<models>
36-
<catalog>
37-
<rewrite>
38-
<product_image>Oggetto_AdaptiveResize_Model_Catalog_Product_Image</product_image>
39-
</rewrite>
40-
</catalog>
4136
<adaptiveResize>
4237
<class>Oggetto_AdaptiveResize_Model</class>
4338
</adaptiveResize>

0 commit comments

Comments
 (0)