Skip to content

Commit

Permalink
Allowed to upload images in WebP format (OpenMage#3384)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabrizio Balliano <fabrizio.balliano@gmail.com>
  • Loading branch information
empiricompany and fballiano authored Jul 19, 2023
1 parent ca00bda commit 0cb5439
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires
ExpiresActive On
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function _prepareLayout()
$browseConfig = $this->getUploader()->getButtonConfig();
$browseConfig
->setAttributes([
'accept' => $browseConfig->getMimeTypesByExtensions('gif, png, jpeg, jpg')
'accept' => $browseConfig->getMimeTypesByExtensions(Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS)
]);

Mage::dispatchEvent('catalog_product_gallery_prepare_layout', ['block' => $this]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Mage_Adminhtml_Model_System_Config_Backend_Image extends Mage_Adminhtml_Mo
*/
protected function _getAllowedExtensions()
{
return ['jpg', 'jpeg', 'gif', 'png'];
return Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function uploadAction()
{
try {
$uploader = new Mage_Core_Model_File_Uploader('image');
$uploader->setAllowedExtensions(['jpg','jpeg','gif','png']);
$uploader->setAllowedExtensions(Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS);
$uploader->addValidateCallback(
'catalog_product_image',
Mage::helper('catalog/image'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ public function viewfileAction()
case 'jpg':
$contentType = 'image/jpeg';
break;
case 'webp':
$contentType = 'image/webp';
break;
case 'png':
$contentType = 'image/png';
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class Mage_Catalog_Model_Api2_Product_Image_Rest extends Mage_Catalog_M
* @var array
*/
protected $_mimeTypes = [
'image/webp' => 'webp',
'image/jpg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Mage_Catalog_Model_Category_Attribute_Backend_Image extends Mage_Eav_Model
*/
public function getAllowedExtensions(): array
{
return ['jpg', 'jpeg', 'gif', 'png'];
return Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ public function addImage(
Mage::dispatchEvent('catalog_product_media_add_image', ['product' => $product, 'image' => $file]);

$pathinfo = pathinfo($file);
$imgExtensions = ['jpg','jpeg','gif','png'];
if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) {
if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS)) {
Mage::throwException(Mage::helper('catalog')->__('Invalid image file type.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Mage_Catalog_Model_Product_Attribute_Media_Api extends Mage_Catalog_Model_
* @var array
*/
protected $_mimeTypes = [
'image/webp' => 'webp',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/png' => 'png'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Mage_Catalog_Model_Resource_Product_Attribute_Backend_Image extends Mage_E
*/
public function getAllowedExtensions(): array
{
return ['jpg', 'jpeg', 'gif', 'png'];
return Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/code/core/Mage/Cms/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@
<jpeg>1</jpeg>
<png>1</png>
<gif>1</gif>
<webp>1</webp>
</allowed>
<image_allowed>
<jpg>1</jpg>
<jpeg>1</jpeg>
<png>1</png>
<gif>1</gif>
<webp>1</webp>
</image_allowed>
<media_allowed>
<flv>1</flv>
Expand Down
5 changes: 5 additions & 0 deletions app/code/core/Mage/Core/Model/File/Validator/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Mage_Core_Model_File_Validator_Image
public const NAME = "isImage";

protected $_allowedImageTypes = [
IMAGETYPE_WEBP,
IMAGETYPE_JPEG,
IMAGETYPE_GIF,
IMAGETYPE_JPEG2000,
Expand All @@ -42,6 +43,7 @@ class Mage_Core_Model_File_Validator_Image
public function setAllowedImageTypes(array $imageFileExtensions = [])
{
$map = [
'webp' => [IMAGETYPE_WEBP],
'tif' => [IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM],
'tiff' => [IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM],
'jpg' => [IMAGETYPE_JPEG, IMAGETYPE_JPEG2000],
Expand Down Expand Up @@ -121,6 +123,9 @@ public function validate($filePath)
case IMAGETYPE_JPEG:
imagejpeg($img, $filePath, $imageQuality);
break;
case IMAGETYPE_WEBP:
imagewebp($img, $filePath, $imageQuality);
break;
case IMAGETYPE_PNG:
imagepng($img, $filePath);
break;
Expand Down
1 change: 1 addition & 0 deletions app/code/core/Mage/Eav/Model/Attribute/Data/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected function _validateByRules($value)
1 => 'gif',
2 => 'jpg',
3 => 'png',
18 => 'webp'
];

if (!isset($allowImageTypes[$imageProp[2]])) {
Expand Down
1 change: 1 addition & 0 deletions app/code/core/Mage/ImportExport/Model/Import/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Mage_ImportExport_Model_Import_Uploader extends Mage_Core_Model_File_Uploa
protected $_tmpDir = '';
protected $_destDir = '';
protected $_allowedMimeTypes = [
'webp' => 'image/webp',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
Expand Down
7 changes: 7 additions & 0 deletions app/code/core/Mage/Media/Model/File/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function getImage(Mage_Media_Model_Image $object)
$resource = imagecreatefromjpeg($object->getFilePath());
break;

case 'webp':
$resource = imagecreatefromwebp($object->getFilePath());
break;

case 'gif':
$resource = imagecreatefromgif($object->getFilePath());
break;
Expand Down Expand Up @@ -176,6 +180,9 @@ public function saveAs(Mage_Media_Model_Image $object, $extension = null)
case 'jpeg':
$result = imagejpeg($object->getTmpImage(), $object->getFilePath(true), 80);
break;
case 'webp':
$result = imagewebp($object->getTmpImage(), $object->getFilePath(true), 80);
break;
case 'gif':
$result = imagegif($object->getTmpImage(), $object->getFilePath(true));
break;
Expand Down
1 change: 1 addition & 0 deletions app/code/core/Mage/Uploader/Helper/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class Mage_Uploader_Helper_File extends Mage_Core_Helper_Abstract
'xjisp' => 'application/vnd.jisp',
'xjlt' => 'application/vnd.hp-jlyt',
'xjoda' => 'application/vnd.joost.joda-archive',
'xwebp' => 'image/webp',
'xjpe' => 'image/jpeg',
'xjpeg' => 'image/jpeg',
'xjpg' => 'image/jpeg',
Expand Down
1 change: 1 addition & 0 deletions lib/Varien/File/Transfer/Adapter/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Varien_File_Transfer_Adapter_Http
'flv' => 'video/x-flv',

// images
'webp' => 'image/webp',
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
Expand Down
6 changes: 6 additions & 0 deletions lib/Varien/Image/Adapter/Gd2.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Varien_Image_Adapter_Gd2 extends Varien_Image_Adapter_Abstract
{
protected $_requiredExtensions = ["gd"];
private static $_callbacks = [
IMAGETYPE_WEBP => ['output' => 'imagewebp', 'create' => 'imagecreatefromwebp'],
IMAGETYPE_GIF => ['output' => 'imagegif', 'create' => 'imagecreatefromgif'],
IMAGETYPE_JPEG => ['output' => 'imagejpeg', 'create' => 'imagecreatefromjpeg'],
IMAGETYPE_PNG => ['output' => 'imagepng', 'create' => 'imagecreatefrompng'],
Expand Down Expand Up @@ -150,6 +151,11 @@ public function save($destination = null, $newName = null)
}
}

// convert palette based image to true color
if ($this->_fileType == IMAGETYPE_WEBP) {
imagepalettetotruecolor($this->_imageHandler);
}

if (!$this->_resized) {
// keep alpha transparency
$isAlpha = false;
Expand Down
7 changes: 6 additions & 1 deletion lib/Varien/Io/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ class Varien_Io_File extends Varien_Io_Abstract
*/
protected $_streamException;

/**
* @var string[]
*/
public const ALLOWED_IMAGES_EXTENSIONS = ['webp', 'jpg', 'jpeg', 'png', 'gif', 'bmp'];

public function __construct()
{
// Initialize shutdown function
Expand Down Expand Up @@ -823,7 +828,7 @@ public function ls($grep = null)
$list_item['size'] = filesize($fullpath);
$list_item['leaf'] = true;
if (isset($pathinfo['extension'])
&& in_array(strtolower($pathinfo['extension']), ['jpg', 'jpeg', 'gif', 'bmp', 'png'])
&& in_array(strtolower($pathinfo['extension']), self::ALLOWED_IMAGES_EXTENSIONS)
&& $list_item['size'] > 0
) {
$list_item['is_image'] = true;
Expand Down

0 comments on commit 0cb5439

Please sign in to comment.