Skip to content

Commit e96444d

Browse files
authored
Merge pull request #37 from freestream/fix/give-back-my-transparency-again
Transparency on png images after upload.
2 parents 9adc799 + 85724a8 commit e96444d

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
/**
4+
* Magento
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.txt.
10+
* It is also available through the world-wide-web at this URL:
11+
* http://opensource.org/licenses/osl-3.0.php
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@magento.com so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade Magento to newer
19+
* versions in the future. If you wish to customize Magento for your
20+
* needs please refer to http://www.magento.com for more information.
21+
*
22+
* @category Mage
23+
* @package Mage_Core
24+
* @copyright Copyright (c) 2006-2017 X.commerce, Inc. and affiliates (http://www.magento.com)
25+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
26+
*/
27+
28+
/**
29+
* Validator for check is uploaded file is image
30+
*
31+
* @category Mage
32+
* @package Mage_Core
33+
* @author Magento Core Team <core@magentocommerce.com>
34+
*/
35+
class Mage_Core_Model_File_Validator_Image
36+
{
37+
const NAME = "isImage";
38+
39+
protected $_allowedImageTypes = array(
40+
IMAGETYPE_JPEG,
41+
IMAGETYPE_GIF,
42+
IMAGETYPE_JPEG2000,
43+
IMAGETYPE_PNG,
44+
IMAGETYPE_ICO,
45+
IMAGETYPE_TIFF_II,
46+
IMAGETYPE_TIFF_MM
47+
);
48+
49+
/**
50+
* Setter for allowed image types
51+
*
52+
* @param array $imageFileExtensions
53+
* @return $this
54+
*/
55+
public function setAllowedImageTypes(array $imageFileExtensions = array())
56+
{
57+
$map = array(
58+
'tif' => array(IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM),
59+
'tiff' => array(IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM),
60+
'jpg' => array(IMAGETYPE_JPEG, IMAGETYPE_JPEG2000),
61+
'jpe' => array(IMAGETYPE_JPEG, IMAGETYPE_JPEG2000),
62+
'jpeg' => array(IMAGETYPE_JPEG, IMAGETYPE_JPEG2000),
63+
'gif' => array(IMAGETYPE_GIF),
64+
'png' => array(IMAGETYPE_PNG),
65+
'ico' => array(IMAGETYPE_ICO),
66+
'apng' => array(IMAGETYPE_PNG)
67+
);
68+
69+
$this->_allowedImageTypes = array();
70+
71+
foreach ($imageFileExtensions as $extension) {
72+
if (isset($map[$extension])) {
73+
foreach ($map[$extension] as $imageType) {
74+
$this->_allowedImageTypes[$imageType] = $imageType;
75+
}
76+
}
77+
}
78+
79+
return $this;
80+
}
81+
82+
/**
83+
* Validation callback for checking is file is image
84+
*
85+
* @param string $filePath Path to temporary uploaded file
86+
* @return null
87+
* @throws Mage_Core_Exception
88+
*/
89+
public function validate($filePath)
90+
{
91+
list($imageWidth, $imageHeight, $fileType) = getimagesize($filePath);
92+
if ($fileType) {
93+
if ($this->isImageType($fileType)) {
94+
//replace tmp image with re-sampled copy to exclude images with malicious data
95+
$image = imagecreatefromstring(file_get_contents($filePath));
96+
if ($image !== false) {
97+
$img = imagecreatetruecolor($imageWidth, $imageHeight);
98+
imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
99+
switch ($fileType) {
100+
case IMAGETYPE_GIF:
101+
imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
102+
imagegif($img, $filePath);
103+
break;
104+
case IMAGETYPE_JPEG:
105+
imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
106+
imagejpeg($img, $filePath, 100);
107+
break;
108+
case IMAGETYPE_PNG:
109+
imagecolortransparent($img, imagecolorallocatealpha($img, 0, 0, 0, 127));
110+
imagealphablending($img, false);
111+
imagesavealpha($img, true);
112+
imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
113+
imagepng($img, $filePath);
114+
break;
115+
default:
116+
return;
117+
}
118+
imagedestroy($img);
119+
imagedestroy($image);
120+
return null;
121+
} else {
122+
throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid image.'));
123+
}
124+
}
125+
}
126+
throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid MIME type.'));
127+
}
128+
129+
/**
130+
* Returns is image by image type
131+
* @param int $nImageType
132+
* @return bool
133+
*/
134+
protected function isImageType($nImageType)
135+
{
136+
return in_array($nImageType, $this->_allowedImageTypes);
137+
}
138+
}

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
"app/code/community/Mage/CatalogRule/Model/Rule.php",
8686
"app/code/community/Mage/CatalogRule/Model/Rule.php"
8787
],
88+
[
89+
"app/code/community/Mage/Core/Model/File/Validator/Image.php",
90+
"app/code/community/Mage/Core/Model/File/Validator/Image.php"
91+
],
8892
[
8993
"app/code/community/Mage/SalesRule/Model/Rule/Condition/Product/Subselect.php",
9094
"app/code/community/Mage/SalesRule/Model/Rule/Condition/Product/Subselect.php"

0 commit comments

Comments
 (0)