From 84f4d0838ce8d6517e7572728dff3fdc9cafe429 Mon Sep 17 00:00:00 2001 From: rMalie Date: Mon, 23 Jan 2012 10:21:51 +0000 Subject: [PATCH] [*] Classes : images.inc.php functions are now all deprecated, use the new class ImageManager instead --- cache/class_index.php | 2 + classes/AdminController.php | 4 +- classes/AdminTab.php | 8 +- classes/FileUploader.php | 111 +++--- classes/ImageManager.php | 359 ++++++++++++++++++ classes/helper/HelperList.php | 2 +- .../WebserviceSpecificManagementImages.php | 12 +- controllers/admin/AdminCartsController.php | 2 +- .../admin/AdminCategoriesController.php | 4 +- controllers/admin/AdminImagesController.php | 12 +- controllers/admin/AdminImportController.php | 4 +- .../admin/AdminLanguagesController.php | 14 +- .../admin/AdminManufacturersController.php | 4 +- controllers/admin/AdminOrdersController.php | 2 +- controllers/admin/AdminProductsController.php | 6 +- controllers/admin/AdminScenesController.php | 4 +- controllers/admin/AdminStoresController.php | 4 +- .../admin/AdminSuppliersController.php | 4 +- controllers/admin/AdminThemesController.php | 26 +- controllers/front/ProductController.php | 6 +- images.inc.php | 357 ++++------------- install-new/classes/fixtures.php | 4 +- install-new/classes/xmlLoader.php | 4 +- install-new/models/install.php | 2 +- modules/blockadvertising/blockadvertising.php | 2 +- modules/blockreinsurance/blockreinsurance.php | 4 +- modules/blockstore/blockstore.php | 2 +- modules/editorial/editorial.php | 4 +- modules/homeslider/homeslider.php | 4 +- modules/shopimporter/shopimporter.php | 10 +- modules/watermark/watermark.php | 6 +- override/classes/ImageManager.php | 7 + 32 files changed, 569 insertions(+), 427 deletions(-) create mode 100644 classes/ImageManager.php create mode 100644 override/classes/ImageManager.php diff --git a/cache/class_index.php b/cache/class_index.php index 16172b854..55cc932d8 100644 --- a/cache/class_index.php +++ b/cache/class_index.php @@ -384,6 +384,8 @@ 'IdentityControllerCore' => 'controllers/front/IdentityController.php', 'Image' => 'override/classes/Image.php', 'ImageCore' => 'classes/Image.php', + 'ImageManager' => 'override/classes/ImageManager.php', + 'ImageManagerCore' => 'classes/ImageManager.php', 'ImageType' => 'override/classes/ImageType.php', 'ImageTypeCore' => 'classes/ImageType.php', 'ImportModule' => 'override/classes/ImportModule.php', diff --git a/classes/AdminController.php b/classes/AdminController.php index 28df16741..b0fc38207 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -2418,7 +2418,7 @@ class AdminControllerCore extends Controller // Check image validity $max_size = isset($this->max_image_size) ? $this->max_image_size : 0; - if ($error = checkImage($_FILES[$name], Tools::getMaxUploadSize($max_size))) + if ($error = ImageManager::validateUpload($_FILES[$name], Tools::getMaxUploadSize($max_size))) $this->errors[] = $error; else if (!$tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS') || !move_uploaded_file($_FILES[$name]['tmp_name'], $tmp_name)) return false; @@ -2426,7 +2426,7 @@ class AdminControllerCore extends Controller { $tmp_name = $_FILES[$name]['tmp_name']; // Copy new image - if (!imageResize($tmp_name, _PS_IMG_DIR_.$dir.$id.'.'.$this->imageType, (int)$width, (int)$height, ($ext ? $ext : $this->imageType))) + if (!ImageManager::resize($tmp_name, _PS_IMG_DIR_.$dir.$id.'.'.$this->imageType, (int)$width, (int)$height, ($ext ? $ext : $this->imageType))) $this->errors[] = Tools::displayError('An error occurred while uploading image.'); if (count($this->errors)) return false; diff --git a/classes/AdminTab.php b/classes/AdminTab.php index 47dae5f79..bbdee7225 100644 --- a/classes/AdminTab.php +++ b/classes/AdminTab.php @@ -1083,7 +1083,7 @@ abstract class AdminTabCore // Check image validity $max_size = isset($this->maxImageSize) ? $this->maxImageSize : 0; - if ($error = checkImage($_FILES[$name], Tools::getMaxUploadSize($max_size))) + if ($error = ImageManager::validateUpload($_FILES[$name], Tools::getMaxUploadSize($max_size))) $this->_errors[] = $error; elseif (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') || !move_uploaded_file($_FILES[$name]['tmp_name'], $tmpName)) return false; @@ -1091,7 +1091,7 @@ abstract class AdminTabCore { $tmpName = $_FILES[$name]['tmp_name']; // Copy new image - if (!imageResize($tmpName, _PS_IMG_DIR_.$dir.$id.'.'.$this->imageType, (int)$width, (int)$height, ($ext ? $ext : $this->imageType))) + if (!ImageManager::resize($tmpName, _PS_IMG_DIR_.$dir.$id.'.'.$this->imageType, (int)$width, (int)$height, ($ext ? $ext : $this->imageType))) $this->_errors[] = Tools::displayError('An error occurred while uploading image.'); if (count($this->_errors)) return false; @@ -1374,7 +1374,7 @@ abstract class AdminTabCore if ($id && file_exists($image)) echo '
- '.cacheImage($image, $this->table.'_'.(int)($id).'.'.$this->imageType, $size, $this->imageType, $disableCache).' + '.ImageManager::thumbnail($image, $this->table.'_'.(int)($id).'.'.$this->imageType, $size, $this->imageType, $disableCache).'

'.$this->l('File size').' '.(filesize($image) / 1000).'kb

'.$this->l('Delete').' '.$this->l('Delete').' @@ -1683,7 +1683,7 @@ abstract class AdminTabCore }else $path_to_image = _PS_IMG_DIR_.$params['image'].'/'.$item_id.(isset($tr['id_image']) ? '-'.(int)($tr['id_image']) : '').'.'.$this->imageType; - echo cacheImage($path_to_image, $this->table.'_mini_'.$item_id.'.'.$this->imageType, 45, $this->imageType); + echo ImageManager::thumbnail($path_to_image, $this->table.'_mini_'.$item_id.'.'.$this->imageType, 45, $this->imageType); } elseif (isset($params['icon']) && (isset($params['icon'][$tr[$key]]) || isset($params['icon']['default']))) echo ''.$tr[$key]).''; diff --git a/classes/FileUploader.php b/classes/FileUploader.php index 6c21fba54..f99cc4e93 100755 --- a/classes/FileUploader.php +++ b/classes/FileUploader.php @@ -32,27 +32,27 @@ class FileUploaderCore private $file; private $sizeLimit; - function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760) + public function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760) { - $allowedExtensions = array_map("strtolower", $allowedExtensions); + $allowedExtensions = array_map('strtolower', $allowedExtensions); $this->allowedExtensions = $allowedExtensions; $this->sizeLimit = $sizeLimit; - if (isset($_GET['qqfile'])) { - $this->file = new qqUploadedFileXhr(); - } elseif (isset($_FILES['qqfile'])) { - $this->file = new qqUploadedFileForm(); - } else { - $this->file = false; - } + if (isset($_GET['qqfile'])) + $this->file = new QqUploadedFileXhr(); + elseif (isset($_FILES['qqfile'])) + $this->file = new QqUploadedFileForm(); + else + $this->file = false; } protected function toBytes($str) { $val = trim($str); - $last = strtolower($str[strlen($str)-1]); - switch($last) { + $last = strtolower($str[strlen($str) - 1]); + switch ($last) + { case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; @@ -63,41 +63,38 @@ class FileUploaderCore /** * Returns array('success'=>true) or array('error'=>'error message') */ - function handleUpload() + public function handleUpload() { - if (!$this->file){ + if (!$this->file) return array('error' => Tools::displayError('No files were uploaded.')); - } $size = $this->file->getSize(); - if ($size == 0) { + if ($size == 0) return array('error' => Tools::displayError('File is empty')); - } - if ($size > $this->sizeLimit) { + if ($size > $this->sizeLimit) return array('error' => Tools::displayError('File is too large')); - } $pathinfo = pathinfo($this->file->getName()); - $filename = $pathinfo['filename']; $these = implode(', ', $this->allowedExtensions); if (!isset($pathinfo['extension'])) - return array('error' => Tools::displayError('File has an invalid extension, it should be one of '). $these . '.'); + return array('error' => Tools::displayError('File has an invalid extension, it should be one of ').$these.'.'); $ext = $pathinfo['extension']; - if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) - return array('error' => Tools::displayError('File has an invalid extension, it should be one of '). $these . '.'); + if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) + return array('error' => Tools::displayError('File has an invalid extension, it should be one of ').$these.'.'); return $this->file->save(); } } -class qqUploadedFileForm { +class QqUploadedFileForm +{ /** * Save the file to the specified path * @return boolean TRUE on success */ - function save() + public function save() { $product = new Product($_GET['id_product']); if (!Validate::isLoadedObject($product)) @@ -105,7 +102,7 @@ class qqUploadedFileForm { else { $image = new Image(); - $image->id_product = (int)($product->id); + $image->id_product = (int)$product->id; $image->position = Image::getHighestPosition($product->id) + 1; if (!Image::getCover($image->id_product)) $image->cover = 1; @@ -114,66 +111,66 @@ class qqUploadedFileForm { if (!$image->add()) return array('error' => Tools::displayError('Error while creating additional image')); else - { return $this->copyImage($product->id, $image->id); - } } } public function copyImage($id_product, $id_image, $method = 'auto') { $image = new Image($id_image); - $p = new Product($id_product); if (!$new_path = $image->getPathForCreation()) return array('error' => Tools::displayError('An error occurred during new folder creation')); - if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($_FILES['qqfile']['tmp_name'], $tmpName)) + if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') || !move_uploaded_file($_FILES['qqfile']['tmp_name'], $tmpName)) return array('error' => Tools::displayError('An error occurred during the image upload')); - elseif (!imageResize($tmpName, $new_path.'.'.$image->image_format)) + elseif (!ImageManager::resize($tmpName, $new_path.'.'.$image->image_format)) return array('error' => Tools::displayError('An error occurred while copying image.')); - elseif($method == 'auto') + elseif ($method == 'auto') { $imagesTypes = ImageType::getImagesTypes('products'); - foreach ($imagesTypes AS $k => $imageType) + foreach ($imagesTypes as $imageType) { $theme = (Shop::isFeatureActive() ? '-'.$imageType['id_theme'] : ''); - if (!imageResize($tmpName, $new_path.'-'.stripslashes($imageType['name']).$theme.'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) + if (!ImageManager::resize($tmpName, $new_path.'-'.stripslashes($imageType['name']).$theme.'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) return array('error' => Tools::displayError('An error occurred while copying image:').' '.stripslashes($imageType['name'])); } } unlink($tmpName); Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_product)); - $lang = Context::getContext()->employee->id_lang; if (!$image->update()) return array('error' => Tools::displayError('Error while updating status')); $img = array('id_image' => $image->id, 'position' => $image->position, 'cover' => $image->cover, 'name' => $this->getName()); - return array("success" => $img); + return array('success' => $img); } - function getName() { + + public function getName() + { return $_FILES['qqfile']['name']; } - function getSize() { + + public function getSize() + { return $_FILES['qqfile']['size']; } } /** * Handle file uploads via XMLHttpRequest */ -class qqUploadedFileXhr +class QqUploadedFileXhr { /** - * Save the file to the specified path - * @return boolean TRUE on success - */ - function upload($path) + * Save the file to the specified path + * @return boolean TRUE on success + */ + public function upload($path) { - $input = fopen("php://input", "r"); + $input = fopen('php://input', 'r'); $temp = tmpfile(); $realSize = stream_copy_to_stream($input, $temp); fclose($input); if ($realSize != $this->getSize()) return false; - $target = fopen($path, "w"); + $target = fopen($path, 'w'); fseek($temp, 0, SEEK_SET); stream_copy_to_stream($temp, $target); fclose($target); @@ -181,7 +178,7 @@ class qqUploadedFileXhr return true; } - function save() + public function save() { $product = new Product($_GET['id_product']); if (!Validate::isLoadedObject($product)) @@ -198,9 +195,7 @@ class qqUploadedFileXhr if (!$image->add()) return array('error' => Tools::displayError('Error while creating additional image')); else - { return $this->copyImage($product->id, $image->id); - } } } @@ -210,21 +205,21 @@ class qqUploadedFileXhr $p = new Product($id_product); if (!$new_path = $image->getPathForCreation()) return array('error' => Tools::displayError('An error occurred during new folder creation')); - if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !$this->upload($tmpName)) + if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') || !$this->upload($tmpName)) return array('error' => Tools::displayError('An error occurred during the image upload')); - elseif (!imageResize($tmpName, $new_path.'.'.$image->image_format)) + elseif (!ImageManager::resize($tmpName, $new_path.'.'.$image->image_format)) return array('error' => Tools::displayError('An error occurred while copying image.')); - elseif($method == 'auto') + elseif ($method == 'auto') { $imagesTypes = ImageType::getImagesTypes('products'); - foreach ($imagesTypes AS $k => $imageType) + foreach ($imagesTypes as $imageType) { /* $theme = (Shop::isFeatureActive() ? '-'.$imageType['id_theme'] : ''); - if (!imageResize($tmpName, $new_path.'-'.stripslashes($imageType['name']).$theme.'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) + if (!ImageManager::resize($tmpName, $new_path.'-'.stripslashes($imageType['name']).$theme.'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) return array('error' => Tools::displayError('An error occurred while copying image:').' '.stripslashes($imageType['name'])); */ - if (!imageResize($tmpName, $new_path.'-'.stripslashes($imageType['name']).'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) + if (!ImageManager::resize($tmpName, $new_path.'-'.stripslashes($imageType['name']).'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) return array('error' => Tools::displayError('An error occurred while copying image:').' '.stripslashes($imageType['name'])); } } @@ -234,18 +229,18 @@ class qqUploadedFileXhr if (!$image->update()) return array('error' => Tools::displayError('Error while updating status')); $img = array('id_image' => $image->id, 'position' => $image->position, 'cover' => $image->cover, 'name' => $this->getName()); - return array("success" => $img); + return array('success' => $img); } - function getName() + public function getName() { return $_GET['qqfile']; } - function getSize() + public function getSize() { - if (isset($_SERVER["CONTENT_LENGTH"])) - return (int)$_SERVER["CONTENT_LENGTH"]; + if (isset($_SERVER['CONTENT_LENGTH'])) + return (int)$_SERVER['CONTENT_LENGTH']; else throw new Exception('Getting content length is not supported.'); } diff --git a/classes/ImageManager.php b/classes/ImageManager.php new file mode 100644 index 000000000..2c2267ade --- /dev/null +++ b/classes/ImageManager.php @@ -0,0 +1,359 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +/** + * This class includes functions for image manipulation + * + * @since 1.5.0 + */ +class ImageManagerCore +{ + /** + * Generate a cached thumbnail for object lists (eg. carrier, order states...etc) + * + * @param string $image Real image filename + * @param string $cache_image Cached filename + * @param int $size Desired size + * @param string $image_type Image type + * @param bool $disable_cache When turned on a timestamp will be added to the image URI to disable the HTTP cache + * @return string + */ + public static function thumbnail($image, $cache_image, $size, $image_type = 'jpg', $disable_cache = false) + { + if (!file_exists($image)) + return ''; + + if (!file_exists(_PS_TMP_IMG_DIR_.$cache_image)) + { + $infos = getimagesize($image); + + $memory_limit = Tools::getMemoryLimit(); + // memory_limit == -1 => unlimited memory + if (function_exists('memory_get_usage') && (int)$memory_limit != -1) + { + $current_memory = memory_get_usage(); + + // Evaluate the memory required to resize the image: if it's too much, you can't resize it. + if (($infos[0] * $infos[1] * $infos['bits'] * (isset($infos['channels']) ? ($infos['channels'] / 8) : 1) + pow(2, 16)) * 1.8 + $current_memory > $memory_limit - 1024 * 1024) + return false; + } + + $x = $infos[0]; + $y = $infos[1]; + $max_x = $size * 3; + + // Size is already ok + if ($y < $size && $x <= $max_x) + copy($image, _PS_TMP_IMG_DIR_.$cache_image); + // We need to resize */ + else + { + $ratioX = $x / ($y / $size); + if ($ratioX > $max_x) + { + $ratioX = $max_x; + $size = $y / ($x / $max_x); + } + + ImageManager::resize($image, _PS_TMP_IMG_DIR_.$cache_image, $ratioX, $size, 'jpg'); + } + } + return ''; + } + + /** + * Resize, cut and optimize image + * + * @param string $src_file Image object from $_FILE + * @param string $dst_file Destination filename + * @param integer $dst_width Desired width (optional) + * @param integer $dst_height Desired height (optional) + * @param string $file_type + * @return boolean Operation result + */ + public static function resize($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg') + { + if (!file_exists($src_file)) + return false; + list($src_width, $src_height, $type) = getimagesize($src_file); + + // If PS_IMAGE_QUALITY is activated, the generated image will be a PNG with .jpg as a file extension. + // This allow for higher quality and for transparency. JPG source files will also benefit from a higher quality + // because JPG reencoding by GD, even with max quality setting, degrades the image. + if (Configuration::get('PS_IMAGE_QUALITY') == 'png_all' + || (Configuration::get('PS_IMAGE_QUALITY') == 'png' && $type == IMAGETYPE_PNG)) + $file_type = 'png'; + + if (!$src_width) + return false; + if (!$dst_width) + $dst_width = $src_width; + if (!$dst_height) + $dst_height = $src_height; + + $src_image = ImageManager::create($type, $src_file); + + $width_diff = $dst_width / $src_width; + $height_diff = $dst_height / $src_height; + + if ($width_diff > 1 && $height_diff > 1) + { + $nextWidth = $src_width; + $nextHeight = $src_height; + } + else + { + if (Configuration::get('PS_IMAGE_GENERATION_METHOD') == 2 || (!Configuration::get('PS_IMAGE_GENERATION_METHOD') && $width_diff > $height_diff)) + { + $nextHeight = $dst_height; + $nextWidth = round(($src_width * $nextHeight) / $src_height); + $dst_width = (int)(!Configuration::get('PS_IMAGE_GENERATION_METHOD') ? $dst_width : $nextWidth); + } + else + { + $nextWidth = $dst_width; + $nextHeight = round($src_height * $dst_width / $src_width); + $dst_height = (int)(!Configuration::get('PS_IMAGE_GENERATION_METHOD') ? $dst_height : $nextHeight); + } + } + + $destImage = imagecreatetruecolor($dst_width, $dst_height); + + // If image is a PNG and the output is PNG, fill with transparency. Else fill with white background. + if ($file_type == 'png' && $type == IMAGETYPE_PNG) + { + imagealphablending($destImage, false); + imagesavealpha($destImage, true); + $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127); + imagefilledrectangle($destImage, 0, 0, $dst_width, $dst_height, $transparent); + } + else + { + $white = imagecolorallocate($destImage, 255, 255, 255); + imagefilledrectangle ($destImage, 0, 0, $dst_width, $dst_height, $white); + } + + imagecopyresampled($destImage, $src_image, (int)(($dst_width - $nextWidth) / 2), (int)(($dst_height - $nextHeight) / 2), 0, 0, $nextWidth, $nextHeight, $src_width, $src_height); + return (ImageManager::write($file_type, $destImage, $dst_file)); + } + + /** + * Check if file is a real image + * + * @param string $filename File path to check + * @param string $file_mime_type File known mime type (generally from $_FILES) + * @param array $mime_type_list Allowed MIME types + * @return bool + */ + public static function isRealImage($filename, $file_mime_type = null, $mime_type_list = null) + { + // Detect mime content type + $mime_type = false; + if (!$mime_type_list) + $mime_type_list = array('image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png'); + + // Try 4 different methods to determine the mime type + if (function_exists('finfo_open')) + { + $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME; + $finfo = finfo_open($const); + $mime_type = finfo_file($finfo, $filename); + finfo_close($finfo); + } + elseif (function_exists('mime_content_type')) + $mime_type = mime_content_type($filename); + elseif (function_exists('exec')) + { + $mime_type = trim(exec('file -b --mime-type '.escapeshellarg($filename))); + if (!$mime_type) + $mime_type = trim(exec('file --mime '.escapeshellarg($filename))); + if (!$mime_type) + $mime_type = trim(exec('file -bi '.escapeshellarg($filename))); + } + + if ($file_mime_type && (empty($mime_type) || $mime_type == 'regular file' || $mime_type == 'text/plain')) + $mime_type = $file_mime_type; + + // For each allowed MIME type, we are looking for it inside the current MIME type + foreach ($mime_type_list as $type) + if (strstr($mime_type, $type)) + return true; + + return false; + } + + /** + * Validate image upload (check image type and weight) + * + * @param array $file Upload $_FILE value + * @param integer $max_file_size Maximum upload size + * @return bool|string Return false if no error encountered + */ + public static function validateUpload($file, $max_file_size = 0) + { + if ((int)$max_file_size > 0 && $file['size'] > (int)$max_file_size) + return Tools::displayError('Image is too large').' ('.($file['size'] / 1000).Tools::displayError('KB').'). '.Tools::displayError('Maximum allowed:').' '.($max_file_size / 1000).Tools::displayError('KB'); + if (!ImageManager::isRealImage($file['tmp_name'], $file['type'])) + return Tools::displayError('Image format not recognized, allowed formats are: .gif, .jpg, .png'); + if ($file['error']) + return Tools::displayError('Error while uploading image; please change your server\'s settings.').'('.Tools::displayError('Error code: ').$file['error'].')'; + return false; + } + + /** + * Validate icon upload + * + * @param array $file Upload $_FILE value + * @param int $max_file_size Maximum upload size + * @return bool|string Return false if no error encountered + */ + public static function validateIconUpload($file, $max_file_size = 0) + { + if ((int)$max_file_size > 0 && $file['size'] > $max_file_size) + return Tools::displayError('Image is too large').' ('.($file['size'] / 1000).'ko). '.Tools::displayError('Maximum allowed:').' '.($max_file_size / 1000).'ko'; + if (substr($file['name'], -4) != '.ico') + return Tools::displayError('Image format not recognized, allowed formats are: .ico'); + if ($file['error']) + return Tools::displayError('Error while uploading image; please change your server\'s settings.'); + return false; + } + + /** + * Cut image + * + * @param array $src_file Origin filename + * @param string $dst_file Destination filename + * @param integer $dst_width Desired width + * @param integer $dst_height Desired height + * @param string $file_type + * @param int $dst_x + * @param int $dst_y + * + * @return bool Operation result + */ + public static function cut($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg', $dst_x = 0, $dst_y = 0) + { + if (!file_exists($src_file)) + return false; + + // Source information + $src_info = getimagesize($src_file); + $src = array( + 'width' => $src_info[0], + 'height' => $src_info[1], + 'ressource' => ImageManager::create($src_info[2], $src_file), + ); + + // Destination information + $dest = array(); + $dest['x'] = $dst_x; + $dest['y'] = $dst_y; + $dest['width'] = !is_null($dst_width) ? $dst_width : $src['width']; + $dest['height'] = !is_null($dst_height) ? $dst_height : $src['height']; + $dest['ressource'] = ImageManager::createWhiteImage($dest['width'], $dest['height']); + + $white = imagecolorallocate($dest['ressource'], 255, 255, 255); + imagecopyresampled($dest['ressource'], $src['ressource'], 0, 0, $dest['x'], $dest['y'], $dest['width'], $dest['height'], $dest['width'], $dest['height']); + imagecolortransparent($dest['ressource'], $white); + $return = ImageManager::write($file_type, $dest['ressource'], $dst_file); + return $return; + } + + /** + * Create an image with GD extension from a given type + * + * @param string $type + * @param string $filename + * @return resource + */ + public static function create($type, $filename) + { + switch ($type) + { + case IMAGETYPE_GIF : + return imagecreatefromgif($filename); + break; + + case IMAGETYPE_PNG : + return imagecreatefrompng($filename); + break; + + case IMAGETYPE_JPEG : + default: + return imagecreatefromjpeg($filename); + break; + } + } + + /** + * Create an empty image with white background + * + * @param int $width + * @param int $height + * @return resource + */ + public static function createWhiteImage($width, $height) + { + $image = imagecreatetruecolor($width, $height); + $white = imagecolorallocate($image, 255, 255, 255); + imagefill($image, 0, 0, $white); + return $image; + } + + /** + * Generate and write image + * + * @param string $type + * @param resource $resource + * @param string $filename + * @return bool + */ + public static function write($type, $resource, $filename) + { + switch ($type) + { + case 'gif': + $success = imagegif($resource, $filename); + break; + + case 'png': + $quality = (Configuration::get('PS_PNG_QUALITY') === false ? 7 : Configuration::get('PS_PNG_QUALITY')); + $success = imagepng($resource, $filename, (int)$quality); + break; + + case 'jpg': + case 'jpeg': + default: + $quality = (Configuration::get('PS_JPEG_QUALITY') === false ? 90 : Configuration::get('PS_JPEG_QUALITY')); + $success = imagejpeg($resource, $filename, (int)$quality); + break; + } + imagedestroy($resource); + @chmod($filename, 0664); + return $success; + } +} diff --git a/classes/helper/HelperList.php b/classes/helper/HelperList.php index 69a003bd2..980635f57 100644 --- a/classes/helper/HelperList.php +++ b/classes/helper/HelperList.php @@ -275,7 +275,7 @@ class HelperListCore extends Helper else $path_to_image = _PS_IMG_DIR_.$params['image'].'/'.$item_id.(isset($tr['id_image']) ? '-'.(int)$tr['id_image'] : '').'.'.$this->imageType; - $this->_list[$index][$key] = cacheImage($path_to_image, $this->table.'_mini_'.$item_id.'.'.$this->imageType, 45, $this->imageType); + $this->_list[$index][$key] = ImageManager::thumbnail($path_to_image, $this->table.'_mini_'.$item_id.'.'.$this->imageType, 45, $this->imageType); } else if (isset($params['icon']) && (isset($params['icon'][$tr[$key]]) || isset($params['icon']['default']))) $this->_list[$index][$key] = isset($params['icon'][$tr[$key]]) ? $params['icon'][$tr[$key]] : $params['icon']['default']; diff --git a/classes/webservice/WebserviceSpecificManagementImages.php b/classes/webservice/WebserviceSpecificManagementImages.php index 27a7de9ed..28f03e042 100755 --- a/classes/webservice/WebserviceSpecificManagementImages.php +++ b/classes/webservice/WebserviceSpecificManagementImages.php @@ -944,7 +944,7 @@ class WebserviceSpecificManagementImagesCore implements WebserviceSpecificManage if ($file['size'] > $this->imgMaxUploadSize) throw new WebserviceException(sprintf('The image size is too large (maximum allowed is %d KB)', ($this->imgMaxUploadSize/1000)), array(72, 400)); require_once(_PS_ROOT_DIR_.'/images.inc.php'); - if ($error = checkImageUploadError($file)) + if ($error = ImageManager::getErrorFromCode($file['error'])) throw new WebserviceException('Image upload error : '.$error, array(76, 400)); if (isset($file['tmp_name']) AND $file['tmp_name'] != NULL) { @@ -965,7 +965,7 @@ class WebserviceSpecificManagementImagesCore implements WebserviceSpecificManage // copy image if (!isset($file['tmp_name'])) return false; - if ($error = checkImage($file, $this->imgMaxUploadSize)) + if ($error = ImageManager::validateUpload($file, $this->imgMaxUploadSize)) throw new WebserviceException('Bad image : '.$error, array(76, 400)); if ($this->imageType == 'products') @@ -976,13 +976,13 @@ class WebserviceSpecificManagementImagesCore implements WebserviceSpecificManage if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($file['tmp_name'], $tmpName)) throw new WebserviceException('An error occurred during the image upload', array(76, 400)); - elseif (!imageResize($tmpName, _PS_PROD_IMG_DIR_.$image->getExistingImgPath().'.'.$image->image_format)) + elseif (!ImageManager::resize($tmpName, _PS_PROD_IMG_DIR_.$image->getExistingImgPath().'.'.$image->image_format)) throw new WebserviceException('An error occurred while copying image', array(76, 400)); - else + else { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes AS $imageType) - if (!imageResize($tmpName, _PS_PROD_IMG_DIR_.$image->getExistingImgPath().'-'.stripslashes($imageType['name']).'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) + if (!ImageManager::resize($tmpName, _PS_PROD_IMG_DIR_.$image->getExistingImgPath().'-'.stripslashes($imageType['name']).'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) $this->_errors[] = Tools::displayError('An error occurred while copying image:').' '.stripslashes($imageType['name']); } @unlink($tmpName); @@ -992,7 +992,7 @@ class WebserviceSpecificManagementImagesCore implements WebserviceSpecificManage { if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($file['tmp_name'], $tmpName)) throw new WebserviceException('An error occurred during the image upload', array(76, 400)); - elseif (!imageResize($tmpName, $receptionPath)) + elseif (!ImageManager::resize($tmpName, $receptionPath)) throw new WebserviceException('An error occurred while copying image', array(76, 400)); @unlink(_PS_TMP_IMG_DIR_.$tmpName); $this->imgToDisplay = $receptionPath; diff --git a/controllers/admin/AdminCartsController.php b/controllers/admin/AdminCartsController.php index 631d47be2..b37ef90d1 100755 --- a/controllers/admin/AdminCartsController.php +++ b/controllers/admin/AdminCartsController.php @@ -159,7 +159,7 @@ class AdminCartsControllerCore extends AdminController $product['qty_in_stock'] = StockAvailable::getQuantityAvailableByProduct($product['id_product'], isset($product['id_product_attribute']) ? $product['id_product_attribute'] : null, (int)$order->id_shop); $image_product = new Image($image['id_image']); - $product['image'] = (isset($image['id_image']) ? cacheImage(_PS_IMG_DIR_.'p/'.$image_product->getExistingImgPath().'.jpg', 'product_mini_'.(int)$product['id_product'].(isset($product['id_product_attribute']) ? '_'.(int)$product['id_product_attribute'] : '').'.jpg', 45, 'jpg') : '--'); + $product['image'] = (isset($image['id_image']) ? ImageManager::thumbnail(_PS_IMG_DIR_.'p/'.$image_product->getExistingImgPath().'.jpg', 'product_mini_'.(int)$product['id_product'].(isset($product['id_product_attribute']) ? '_'.(int)$product['id_product_attribute'] : '').'.jpg', 45, 'jpg') : '--'); } $this->tpl_view_vars = array( diff --git a/controllers/admin/AdminCategoriesController.php b/controllers/admin/AdminCategoriesController.php index 289849798..d99d5bcc7 100644 --- a/controllers/admin/AdminCategoriesController.php +++ b/controllers/admin/AdminCategoriesController.php @@ -401,7 +401,7 @@ class AdminCategoriesControllerCore extends AdminController if (!($obj = $this->loadObject(true))) return; - $image = cacheImage(_PS_CAT_IMG_DIR_.'/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true); + $image = ImageManager::thumbnail(_PS_CAT_IMG_DIR_.'/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true); $this->fields_value = array( 'image' => $image ? $image : false, @@ -546,7 +546,7 @@ class AdminCategoriesControllerCore extends AdminController foreach ($images_types as $k => $image_type) { $theme = (Shop::isFeatureActive() ? '-'.$image_type['id_theme'] : ''); - imageResize( + ImageManager::resize( _PS_CAT_IMG_DIR_.$id_category.'.jpg', _PS_CAT_IMG_DIR_.$id_category.'-'.stripslashes($image_type['name']).$theme.'.jpg', (int)$image_type['width'], (int)$image_type['height'] diff --git a/controllers/admin/AdminImagesController.php b/controllers/admin/AdminImagesController.php index b8af22c5f..c06b40c75 100644 --- a/controllers/admin/AdminImagesController.php +++ b/controllers/admin/AdminImagesController.php @@ -369,7 +369,7 @@ class AdminImagesControllerCore extends AdminController if (!file_exists($newDir)) continue; if (!file_exists($newDir.substr($image, 0, -4).'-'.stripslashes($imageType['name']).'.jpg')) - if (!imageResize($dir.$image, $newDir.substr($image, 0, -4).'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']))) + if (!ImageManager::resize($dir.$image, $newDir.substr($image, 0, -4).'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']))) $errors = true; if (time() - $this->start_time > $this->max_execution_time - 4) // stop 4 seconds before the tiemout, just enough time to process the end of the page on a slow server return 'timeout'; @@ -385,7 +385,7 @@ class AdminImagesControllerCore extends AdminController foreach ($type AS $k => $imageType) { if (!file_exists($dir.$imageObj->getExistingImgPath().'-'.stripslashes($imageType['name']).'.jpg')) - if (!imageResize($dir.$imageObj->getExistingImgPath().'.jpg', $dir.$imageObj->getExistingImgPath().'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']))) + if (!ImageManager::resize($dir.$imageObj->getExistingImgPath().'.jpg', $dir.$imageObj->getExistingImgPath().'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']))) $errors = true; if (time() - $this->start_time > $this->max_execution_time - 4) // stop 4 seconds before the tiemout, just enough time to process the end of the page on a slow server return 'timeout'; @@ -408,7 +408,7 @@ class AdminImagesControllerCore extends AdminController if (!file_exists($file)) $file = _PS_PROD_IMG_DIR_.Language::getIsoById((int)(Configuration::get('PS_LANG_DEFAULT'))).'.jpg'; if (!file_exists($dir.$language['iso_code'].'-default-'.stripslashes($imageType['name']).'.jpg')) - if (!imageResize($file, $dir.$language['iso_code'].'-default-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']))) + if (!ImageManager::resize($file, $dir.$language['iso_code'].'-default-'.stripslashes($imageType['name']).'.jpg', (int)$imageType['width'], (int)$imageType['height'])) $errors = true; } } @@ -424,14 +424,14 @@ class AdminImagesControllerCore extends AdminController LEFT JOIN `'._DB_PREFIX_.'hook` h ON hm.`id_hook` = h.`id_hook` WHERE h.`name` = \'watermark\' AND m.`active` = 1'); - if ($result AND sizeof($result)) + if ($result && count($result)) { $productsImages = Image::getAllImages(); - foreach ($productsImages AS $k => $image) + foreach ($productsImages as $image) { $imageObj = new Image($image['id_image']); if (file_exists($dir.$imageObj->getExistingImgPath().'.jpg')) - foreach ($result AS $k => $module) + foreach ($result as $module) { if ($moduleInstance = Module::getInstanceByName($module['name']) AND is_callable(array($moduleInstance, 'hookwatermark'))) call_user_func(array($moduleInstance, 'hookwatermark'), array('id_image' => $imageObj->id, 'id_product' => $imageObj->id_product)); diff --git a/controllers/admin/AdminImportController.php b/controllers/admin/AdminImportController.php index ce2988b9a..55444da78 100644 --- a/controllers/admin/AdminImportController.php +++ b/controllers/admin/AdminImportController.php @@ -778,12 +778,12 @@ class AdminImportControllerCore extends AdminController // Just hide the warning, the traitment will be the same. if (@copy($url, $tmpfile)) { - imageResize($tmpfile, $path.'.jpg'); + ImageManager::resize($tmpfile, $path.'.jpg'); $images_types = ImageType::getImagesTypes($entity); foreach ($images_types as $k => $image_type) { $theme = (Shop::isFeatureActive() ? '-'.$image_type['id_theme'] : ''); - imageResize($tmpfile, $path.'-'.stripslashes($image_type['name']).$theme.'.jpg', $image_type['width'], $image_type['height']); + ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).$theme.'.jpg', $image_type['width'], $image_type['height']); } if (in_array($image_type['id_image_type'], $watermark_types)) Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity)); diff --git a/controllers/admin/AdminLanguagesController.php b/controllers/admin/AdminLanguagesController.php index a27df6d1b..60748aa68 100644 --- a/controllers/admin/AdminLanguagesController.php +++ b/controllers/admin/AdminLanguagesController.php @@ -415,17 +415,17 @@ class AdminLanguagesControllerCore extends AdminController public function copyNoPictureImage($language) { if (isset($_FILES['no-picture']) && $_FILES['no-picture']['error'] === 0) - if ($error = checkImage($_FILES['no-picture'], Tools::getMaxUploadSize())) + if ($error = ImageManager::validateUpload($_FILES['no-picture'], Tools::getMaxUploadSize())) $this->errors[] = $error; else { if (!$tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS') || !move_uploaded_file($_FILES['no-picture']['tmp_name'], $tmp_name)) return false; - if (!imageResize($tmp_name, _PS_IMG_DIR_.'p/'.$language.'.jpg')) + if (!ImageManager::resize($tmp_name, _PS_IMG_DIR_.'p/'.$language.'.jpg')) $this->errors[] = Tools::displayError('An error occurred while copying no-picture image to your product folder.'); - if (!imageResize($tmp_name, _PS_IMG_DIR_.'c/'.$language.'.jpg')) + if (!ImageManager::resize($tmp_name, _PS_IMG_DIR_.'c/'.$language.'.jpg')) $this->errors[] = Tools::displayError('An error occurred while copying no-picture image to your category folder.'); - if (!imageResize($tmp_name, _PS_IMG_DIR_.'m/'.$language.'.jpg')) + if (!ImageManager::resize($tmp_name, _PS_IMG_DIR_.'m/'.$language.'.jpg')) $this->errors[] = Tools::displayError('An error occurred while copying no-picture image to your manufacturer folder'); else { @@ -433,11 +433,11 @@ class AdminLanguagesControllerCore extends AdminController foreach ($images_types as $k => $image_type) { $theme = (Shop::isFeatureActive() ? '-'.$image_type['id_theme'] : ''); - if (!imageResize($tmp_name, _PS_IMG_DIR_.'p/'.$language.'-default-'.stripslashes($image_type['name']).$theme.'.jpg', $image_type['width'], $image_type['height'])) + if (!ImageManager::resize($tmp_name, _PS_IMG_DIR_.'p/'.$language.'-default-'.stripslashes($image_type['name']).$theme.'.jpg', $image_type['width'], $image_type['height'])) $this->errors[] = Tools::displayError('An error occurred while resizing no-picture image to your product directory.'); - if (!imageResize($tmp_name, _PS_IMG_DIR_.'c/'.$language.'-default-'.stripslashes($image_type['name']).$theme.'.jpg', $image_type['width'], $image_type['height'])) + if (!ImageManager::resize($tmp_name, _PS_IMG_DIR_.'c/'.$language.'-default-'.stripslashes($image_type['name']).$theme.'.jpg', $image_type['width'], $image_type['height'])) $this->errors[] = Tools::displayError('An error occurred while resizing no-picture image to your category directory.'); - if (!imageResize($tmp_name, _PS_IMG_DIR_.'m/'.$language.'-default-'.stripslashes($image_type['name']).$theme.'.jpg', $image_type['width'], $image_type['height'])) + if (!ImageManager::resize($tmp_name, _PS_IMG_DIR_.'m/'.$language.'-default-'.stripslashes($image_type['name']).$theme.'.jpg', $image_type['width'], $image_type['height'])) $this->errors[] = Tools::displayError('An error occurred while resizing no-picture image to your manufacturer directory.'); } } diff --git a/controllers/admin/AdminManufacturersController.php b/controllers/admin/AdminManufacturersController.php index 06b9690fc..88fc68ae7 100644 --- a/controllers/admin/AdminManufacturersController.php +++ b/controllers/admin/AdminManufacturersController.php @@ -335,7 +335,7 @@ class AdminManufacturersControllerCore extends AdminController 'class' => 'button' ); - $image = cacheImage(_PS_MANU_IMG_DIR_.'/'.$manufacturer->id.'.jpg', $this->table.'_'.(int)$manufacturer->id.'.'.$this->imageType, 350, $this->imageType, true); + $image = ImageManager::thumbnail(_PS_MANU_IMG_DIR_.'/'.$manufacturer->id.'.jpg', $this->table.'_'.(int)$manufacturer->id.'.'.$this->imageType, 350, $this->imageType, true); $this->fields_value = array( 'image' => $image ? $image : false, @@ -685,7 +685,7 @@ class AdminManufacturersControllerCore extends AdminController foreach ($images_types as $k => $image_type) { $theme = (Shop::isFeatureActive() ? '-'.$image_type['id_theme'] : ''); - imageResize( + ImageManager::resize( _PS_MANU_IMG_DIR_.$id_manufacturer.'.jpg', _PS_MANU_IMG_DIR_.$id_manufacturer.'-'.stripslashes($image_type['name']).$theme.'.jpg', (int)$image_type['width'], diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 8a123f998..96d0d814b 100755 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -1732,7 +1732,7 @@ class AdminOrdersControllerCore extends AdminController { $name = 'product_mini_'.(int)$product['product_id'].(isset($product['product_attribute_id']) ? '_'.(int)$product['product_attribute_id'] : '').'.jpg'; // generate image cache, only for back office - $product['image_tag'] = cacheImage(_PS_IMG_DIR_.'p/'.$product['image']->getExistingImgPath().'.jpg', $name, 45, 'jpg'); + $product['image_tag'] = ImageManager::thumbnail(_PS_IMG_DIR_.'p/'.$product['image']->getExistingImgPath().'.jpg', $name, 45, 'jpg'); if (file_exists(_PS_TMP_IMG_DIR_.$name)) $product['image_size'] = getimagesize(_PS_TMP_IMG_DIR_.$name); else diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index 05ce16460..0b6ea8d11 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -1339,7 +1339,7 @@ class AdminProductsControllerCore extends AdminController { if (!isset($_FILES['image_product']['tmp_name'])) return false; - if ($error = checkImage($_FILES['image_product'])) + if ($error = ImageManager::validateUpload($_FILES['image_product'])) $this->errors[] = $error; else { @@ -1349,7 +1349,7 @@ class AdminProductsControllerCore extends AdminController $this->errors[] = Tools::displayError('An error occurred during new folder creation'); if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') || !move_uploaded_file($_FILES['image_product']['tmp_name'], $tmpName)) $this->errors[] = Tools::displayError('An error occurred during the image upload'); - else if (!imageResize($tmpName, $new_path.'.'.$image->image_format)) + else if (!ImageManager::resize($tmpName, $new_path.'.'.$image->image_format)) $this->errors[] = Tools::displayError('An error occurred while copying image.'); else if ($method == 'auto') { @@ -1357,7 +1357,7 @@ class AdminProductsControllerCore extends AdminController foreach ($imagesTypes as $k => $image_type) { $theme = (Shop::isFeatureActive() ? '-'.$image_type['id_theme'] : ''); - if (!imageResize($tmpName, $new_path.'-'.stripslashes($image_type['name']).$theme.'.'.$image->image_format, $image_type['width'], $image_type['height'], $image->image_format)) + if (!ImageManager::resize($tmpName, $new_path.'-'.stripslashes($image_type['name']).$theme.'.'.$image->image_format, $image_type['width'], $image_type['height'], $image->image_format)) $this->errors[] = Tools::displayError('An error occurred while copying image:').' '.stripslashes($image_type['name']); } } diff --git a/controllers/admin/AdminScenesController.php b/controllers/admin/AdminScenesController.php index 9f621b310..42f3d2815 100644 --- a/controllers/admin/AdminScenesController.php +++ b/controllers/admin/AdminScenesController.php @@ -76,7 +76,7 @@ class AdminScenesControllerCore extends AdminController { $theme = (Shop::isFeatureActive() ? '-'.$image_type['id_theme'] : ''); if ($image_type['name'] == 'large_scene' && isset($_FILES['image'])) - imageResize( + ImageManager::resize( $_FILES['image']['tmp_name'], _PS_SCENE_IMG_DIR_.$obj->id.'-'.stripslashes($image_type['name']).$theme.'.jpg', (int)$image_type['width'], @@ -88,7 +88,7 @@ class AdminScenesControllerCore extends AdminController $tmp_name = $_FILES['thumb']['tmp_name']; else $tmp_name = $_FILES['image']['tmp_name']; - imageResize( + ImageManager::resize( $tmp_name, _PS_SCENE_THUMB_IMG_DIR_.$obj->id.'-'.stripslashes($image_type['name']).$theme.'.jpg', (int)$image_type['width'], diff --git a/controllers/admin/AdminStoresController.php b/controllers/admin/AdminStoresController.php index 742cf3698..e53f141c9 100644 --- a/controllers/admin/AdminStoresController.php +++ b/controllers/admin/AdminStoresController.php @@ -269,7 +269,7 @@ class AdminStoresControllerCore extends AdminController if (!($obj = $this->loadObject(true))) return; - $image = cacheImage(_PS_STORE_IMG_DIR_.'/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true); + $image = ImageManager::thumbnail(_PS_STORE_IMG_DIR_.'/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true); $days = array(); $days[1] = $this->l('Monday'); @@ -376,7 +376,7 @@ class AdminStoresControllerCore extends AdminController foreach ($images_types as $k => $image_type) { $theme = (Shop::isFeatureActive() ? '-'.$image_type['id_theme'] : ''); - imageResize(_PS_STORE_IMG_DIR_.$id_store.'.jpg', + ImageManager::resize(_PS_STORE_IMG_DIR_.$id_store.'.jpg', _PS_STORE_IMG_DIR_.$id_store.'-'.stripslashes($image_type['name']).$theme.'.jpg', (int)$image_type['width'], (int)$image_type['height'] ); diff --git a/controllers/admin/AdminSuppliersController.php b/controllers/admin/AdminSuppliersController.php index 5ca5475a1..3291d7810 100644 --- a/controllers/admin/AdminSuppliersController.php +++ b/controllers/admin/AdminSuppliersController.php @@ -243,7 +243,7 @@ class AdminSuppliersControllerCore extends AdminController } // set logo image - $image = cacheImage(_PS_SUPP_IMG_DIR_.'/'.$this->object->id.'.jpg', $this->table.'_'.(int)$this->object->id.'.'.$this->imageType, 350, $this->imageType, true); + $image = ImageManager::thumbnail(_PS_SUPP_IMG_DIR_.'/'.$this->object->id.'.jpg', $this->table.'_'.(int)$this->object->id.'.'.$this->imageType, 350, $this->imageType, true); $this->fields_value['image'] = $image ? $image : false; $this->fields_value['size'] = $image ? filesize(_PS_SUPP_IMG_DIR_.'/'.$this->object->id.'.jpg') / 1000 : false; @@ -320,7 +320,7 @@ class AdminSuppliersControllerCore extends AdminController { $file = _PS_SUPP_IMG_DIR_.$id_supplier.'.jpg'; $theme = (Shop::isFeatureActive() ? '-'.$image_type['id_theme'] : ''); - imageResize($file, _PS_SUPP_IMG_DIR_.$id_supplier.'-'.stripslashes($image_type['name']).$theme.'.jpg', (int)$image_type['width'], (int)$image_type['height']); + ImageManager::resize($file, _PS_SUPP_IMG_DIR_.$id_supplier.'-'.stripslashes($image_type['name']).$theme.'.jpg', (int)$image_type['width'], (int)$image_type['height']); } } } diff --git a/controllers/admin/AdminThemesController.php b/controllers/admin/AdminThemesController.php index 633970d25..5563bcb37 100644 --- a/controllers/admin/AdminThemesController.php +++ b/controllers/admin/AdminThemesController.php @@ -449,13 +449,13 @@ class AdminThemesControllerCore extends AdminController $id_shop = Context::getContext()->shop->getID(); if (isset($_FILES['PS_LOGO']['tmp_name']) AND $_FILES['PS_LOGO']['tmp_name']) { - if ($error = checkImage($_FILES['PS_LOGO'], 300000)) + if ($error = ImageManager::validateUpload($_FILES['PS_LOGO'], 300000)) $this->errors[] = $error; if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($_FILES['PS_LOGO']['tmp_name'], $tmpName)) return false; - if ($id_shop == Configuration::get('PS_SHOP_DEFAULT') && !@imageResize($tmpName, _PS_IMG_DIR_.'logo.jpg')) + if ($id_shop == Configuration::get('PS_SHOP_DEFAULT') && !@ImageManager::resize($tmpName, _PS_IMG_DIR_.'logo.jpg')) $this->errors[] = 'an error occurred during logo copy'; - if (!@imageResize($tmpName, _PS_IMG_DIR_.'logo-'.(int)$id_shop.'.jpg')) + if (!@ImageManager::resize($tmpName, _PS_IMG_DIR_.'logo-'.(int)$id_shop.'.jpg')) $this->errors[] = 'an error occurred during logo copy'; unlink($tmpName); @@ -470,13 +470,13 @@ class AdminThemesControllerCore extends AdminController $id_shop = Context::getContext()->shop->getID(); if (isset($_FILES['PS_LOGO_MAIL']['tmp_name']) AND $_FILES['PS_LOGO_MAIL']['tmp_name']) { - if ($error = checkImage($_FILES['PS_LOGO_MAIL'], 300000)) + if ($error = ImageManager::validateUpload($_FILES['PS_LOGO_MAIL'], 300000)) $this->errors[] = $error; if (!$tmpName == tempnam(_PS_TMP_IMG_DIR_, 'PS_MAIL') OR !move_uploaded_file($_FILES['PS_LOGO_MAIL']['tmp_name'], $tmpName)) return false; - if ($id_shop == Configuration::get('PS_SHOP_DEFAULT') && !@imageResize($tmpName, _PS_IMG_DIR_.'logo_mail.jpg')) + if ($id_shop == Configuration::get('PS_SHOP_DEFAULT') && !@ImageManager::resize($tmpName, _PS_IMG_DIR_.'logo_mail.jpg')) $this->errors[] = 'an error occurred during logo copy'; - if (!@imageResize($tmpName, _PS_IMG_DIR_.'logo_mail-'.(int)$id_shop.'.jpg')) + if (!@ImageManager::resize($tmpName, _PS_IMG_DIR_.'logo_mail-'.(int)$id_shop.'.jpg')) $this->errors[] = 'an error occurred during logo copy'; unlink($tmpName); } @@ -490,13 +490,13 @@ class AdminThemesControllerCore extends AdminController $id_shop = Context::getContext()->shop->getID(); if (isset($_FILES['PS_LOGO_INVOICE']['tmp_name']) AND $_FILES['PS_LOGO_INVOICE']['tmp_name']) { - if ($error = checkImage($_FILES['PS_LOGO_INVOICE'], 300000)) + if ($error = ImageManager::validateUpload($_FILES['PS_LOGO_INVOICE'], 300000)) $this->errors[] = $error; if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS_INVOICE') OR !move_uploaded_file($_FILES['PS_LOGO_INVOICE']['tmp_name'], $tmpName)) return false; - if ($id_shop == Configuration::get('PS_SHOP_DEFAULT') && !@imageResize($tmpName, _PS_IMG_DIR_.'logo_invoice.jpg')) + if ($id_shop == Configuration::get('PS_SHOP_DEFAULT') && !@ImageManager::resize($tmpName, _PS_IMG_DIR_.'logo_invoice.jpg')) $this->errors[] = 'an error occurred during logo copy'; - if (!@imageResize($tmpName, _PS_IMG_DIR_.'logo_invoice-'.(int)$id_shop.'.jpg')) + if (!@ImageManager::resize($tmpName, _PS_IMG_DIR_.'logo_invoice-'.(int)$id_shop.'.jpg')) $this->errors[] = 'an error occurred during logo copy'; unlink($tmpName); @@ -511,13 +511,13 @@ class AdminThemesControllerCore extends AdminController $id_shop = Context::getContext()->shop->getID(); if (isset($_FILES['PS_STORES_ICON']['tmp_name']) AND $_FILES['PS_STORES_ICON']['tmp_name']) { - if ($error = checkImage($_FILES['PS_STORES_ICON'], 300000)) + if ($error = ImageManager::validateUpload($_FILES['PS_STORES_ICON'], 300000)) $this->errors[] = $error; if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS_STORES_ICON') OR !move_uploaded_file($_FILES['PS_STORES_ICON']['tmp_name'], $tmpName)) return false; - if ($id_shop = Configuration::get('PS_SHOP_DEFAULT') && !@imageResize($tmpName, _PS_IMG_DIR_.'logo_stores.gif')) + if ($id_shop = Configuration::get('PS_SHOP_DEFAULT') && !@ImageManager::resize($tmpName, _PS_IMG_DIR_.'logo_stores.gif')) $this->errors[] = 'an error occurred during logo copy'; - if (!@imageResize($tmpName, _PS_IMG_DIR_.'logo_stores-'.(int)$id_shop.'.gif')) + if (!@ImageManager::resize($tmpName, _PS_IMG_DIR_.'logo_stores-'.(int)$id_shop.'.gif')) $this->errors[] = 'an error occurred during logo copy'; unlink($tmpName); } @@ -532,7 +532,7 @@ class AdminThemesControllerCore extends AdminController if (isset($_FILES[$name]['tmp_name']) && !empty($_FILES[$name]['tmp_name'])) { /* Check ico validity */ - if ($error = checkIco($_FILES[$name])) + if ($error = ImageManager::validateIconUpload($_FILES[$name])) $this->errors[] = $error; /* Copy new ico */ diff --git a/controllers/front/ProductController.php b/controllers/front/ProductController.php index 19cb482d4..a963aeaa9 100644 --- a/controllers/front/ProductController.php +++ b/controllers/front/ProductController.php @@ -466,7 +466,7 @@ class ProductControllerCore extends FrontController if (in_array($field_name, $authorized_file_fields) && isset($file['tmp_name']) && !empty($file['tmp_name'])) { $file_name = md5(uniqid(rand(), true)); - if ($error = checkImage($file, (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE'))) + if ($error = ImageManager::validateUpload($file, (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE'))) $this->errors[] = $error; $product_picture_width = (int)Configuration::get('PS_PRODUCT_PICTURE_WIDTH'); @@ -474,10 +474,10 @@ class ProductControllerCore extends FrontController if ($error || (!$tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS') || !move_uploaded_file($file['tmp_name'], $tmp_name))) return false; /* Original file */ - else if (!imageResize($tmp_name, _PS_UPLOAD_DIR_.$file_name)) + else if (!ImageManager::resize($tmp_name, _PS_UPLOAD_DIR_.$file_name)) $this->errors[] = Tools::displayError('An error occurred during the image upload.'); /* A smaller one */ - else if (!imageResize($tmp_name, _PS_UPLOAD_DIR_.$file_name.'_small', $product_picture_width, $product_picture_height)) + else if (!ImageManager::resize($tmp_name, _PS_UPLOAD_DIR_.$file_name.'_small', $product_picture_width, $product_picture_height)) $this->errors[] = Tools::displayError('An error occurred during the image upload.'); else if (!chmod(_PS_UPLOAD_DIR_.$file_name, 0777) || !chmod(_PS_UPLOAD_DIR_.$file_name.'_small', 0777)) $this->errors[] = Tools::displayError('An error occurred during the image upload.'); diff --git a/images.inc.php b/images.inc.php index d7321dd3e..0f7738f19 100644 --- a/images.inc.php +++ b/images.inc.php @@ -26,328 +26,107 @@ */ /** - * Generate a cached thumbnail for object lists (eg. carrier, order states...etc) - * - * @param string $image Real image filename - * @param string $cacheImage Cached filename - * @param integer $size Desired size - * @param string $imageType Image type - * @param boolean $disableCache When turned on a timestamp will be added to the image URI to disable the HTTP cache + * This file will be removed in 1.6 + */ + +/** + * @deprecated 1.5.0 */ function cacheImage($image, $cacheImage, $size, $imageType = 'jpg', $disableCache = false) { - if (file_exists($image)) - { - if (!file_exists(_PS_TMP_IMG_DIR_.$cacheImage)) - { - $infos = getimagesize($image); - - $memory_limit = Tools::getMemoryLimit(); - // memory_limit == -1 => unlimited memory - if (function_exists('memory_get_usage') && (int)$memory_limit != -1) - { - $current_memory = memory_get_usage(); - - // Evaluate the memory required to resize the image: if it's too much, you can't resize it. - if (($infos[0] * $infos[1] * $infos['bits'] * (isset($infos['channels']) ? ($infos['channels'] / 8) : 1) + pow(2, 16)) * 1.8 + $current_memory > $memory_limit - 1024 * 1024) - return false; - } - - $x = $infos[0]; - $y = $infos[1]; - $max_x = ((int)$size)*3; - - /* Size is already ok */ - if ($y < $size && $x <= $max_x ) - copy($image, _PS_TMP_IMG_DIR_.$cacheImage); - - /* We need to resize */ - else - { - $ratioX = $x / ($y / $size); - if($ratioX > $max_x) - { - $ratioX = $max_x; - $size = $y / ($x / $max_x); - } - - imageResize($image, _PS_TMP_IMG_DIR_.$cacheImage, $ratioX, $size, 'jpg'); - } - } - return ''; - } - return ''; + Tools::displayAsDeprecated(); + return ImageManager::thumbnail($image, $cacheImage, $size, $imageType, $disableCache); } /** - * Check image upload - * - * @param array $file Upload $_FILE value - * @param integer $maxFileSize Maximum upload size (optional) - */ + * @deprecated 1.5.0 + */ function checkImage($file, $maxFileSize = 0) { - if ((int)$maxFileSize > 0 && $file['size'] > (int)$maxFileSize) - return Tools::displayError('Image is too large').' ('.($file['size'] / 1000).Tools::displayError('KB').'). '.Tools::displayError('Maximum allowed:').' '.($maxFileSize / 1000).Tools::displayError('KB'); - if (!isPicture($file)) - return Tools::displayError('Image format not recognized, allowed formats are: .gif, .jpg, .png'); - if ($file['error']) - return Tools::displayError('Error while uploading image; please change your server\'s settings.').'('.Tools::displayError('Error code: ').$file['error'].')'; - return false; + Tools::displayAsDeprecated(); + return ImageManager::validateUpload($file, $maxFileSize); } - - +/** + * @deprecated 1.5.0 + */ function checkImageUploadError($file) { - if ($file['error']) - { - switch ($file['error']) - { - case 1: - return Tools::displayError('The file is too large.'); - break; - - case 2: - return Tools::displayError('The file is too large.'); - break; - - case 3: - return Tools::displayError('The file was partialy uploaded'); - break; - - case 4: - return Tools::displayError('The file is empty'); - break; - } - } + return ImageManager::getErrorFromCode($file['error']); } /** - * Check image MIME type - * - * @param string $file $_FILE of the current file - * @param array $types Allowed MIME types - */ -function isPicture($file, $types = NULL) + * @deprecated 1.5.0 + */ +function isPicture($file, $types = null) { - /* Detect mime content type */ - $mimeType = false; - if (!$types) - $types = array('image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png'); - - /* Try 4 different methods to determine the mime type */ - if (function_exists('finfo_open')) - { - $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME; - $finfo = finfo_open($const); - $mimeType = finfo_file($finfo, $file['tmp_name']); - finfo_close($finfo); - } - elseif (function_exists('mime_content_type')) - $mimeType = mime_content_type($file['tmp_name']); - elseif (function_exists('exec')) - { - $mimeType = trim(exec('file -b --mime-type '.escapeshellarg($file['tmp_name']))); - if (!$mimeType) - $mimeType = trim(exec('file --mime '.escapeshellarg($file['tmp_name']))); - if (!$mimeType) - $mimeType = trim(exec('file -bi '.escapeshellarg($file['tmp_name']))); - } - if (empty($mimeType) OR $mimeType == 'regular file' OR $mimeType == 'text/plain') - $mimeType = $file['type']; - - /* For each allowed MIME type, we are looking for it inside the current MIME type */ - foreach ($types AS $type) - if (strstr($mimeType, $type)) - return true; - - return false; + Tools::displayAsDeprecated(); + return ImageManager::isRealImage($file['tmp_name'], $file['type'], $types); } /** - * Check icon upload - * - * @param array $file Upload $_FILE value - * @param integer $maxFileSize Maximum upload size (optional) + * @deprecated 1.5.0 */ function checkIco($file, $maxFileSize = 0) { - if ((int)$maxFileSize > 0 && $file['size'] > $maxFileSize) - return Tools::displayError('Image is too large').' ('.($file['size'] / 1000).'ko). '.Tools::displayError('Maximum allowed:').' '.($maxFileSize / 1000).'ko'; - if (substr($file['name'], -4) != '.ico') - return Tools::displayError('Image format not recognized, allowed formats are: .ico'); - if ($file['error']) - return Tools::displayError('Error while uploading image; please change your server\'s settings.'); + Tools::displayAsDeprecated(); + return ImageManager::validateIconUpload($file, $maxFileSize); +} + +/** + * @deprecated 1.5.0 + */ +function imageResize($sourceFile, $destFile, $destWidth = null, $destHeight = null, $fileType = 'jpg') +{ + Tools::displayAsDeprecated(); + return ImageManager::resize($sourceFile, $destFile, $destWidth, $destHeight, $fileType); +} + +/** + * @deprecated 1.5.0 + */ +function imageCut($srcFile, $destFile, $destWidth = NULL, $destHeight = NULL, $fileType = 'jpg', $destX = 0, $destY = 0) +{ + Tools::displayAsDeprecated(); + if (isset($srcFile['tmp_name'])) + return ImageManager::cut($srcFile['tmp_name'], $destFile, $destWidth, $destHeight, $fileType, $destX, $destY); return false; } /** - * Resize, cut and optimize image - * - * @param array $sourceFile Image object from $_FILE - * @param string $destFile Destination filename - * @param integer $destWidth Desired width (optional) - * @param integer $destHeight Desired height (optional) - * - * @return boolean Operation result - */ -function imageResize($sourceFile, $destFile, $destWidth = NULL, $destHeight = NULL, $fileType = 'jpg') -{ - if (!file_exists($sourceFile)) - return false; - list($sourceWidth, $sourceHeight, $type, $attr) = getimagesize($sourceFile); - // If PS_IMAGE_QUALITY is activated, the generated image will be a PNG with .jpg as a file extension. - // This allow for higher quality and for transparency. JPG source files will also benefit from a higher quality - // because JPG reencoding by GD, even with max quality setting, degrades the image. - if (Configuration::get('PS_IMAGE_QUALITY') == 'png_all' - || (Configuration::get('PS_IMAGE_QUALITY') == 'png' && $type == IMAGETYPE_PNG)) - $fileType = 'png'; - - if (!$sourceWidth) - return false; - if ($destWidth == NULL) $destWidth = $sourceWidth; - if ($destHeight == NULL) $destHeight = $sourceHeight; - - $sourceImage = createSrcImage($type, $sourceFile); - - $widthDiff = $destWidth / $sourceWidth; - $heightDiff = $destHeight / $sourceHeight; - - if ($widthDiff > 1 AND $heightDiff > 1) - { - $nextWidth = $sourceWidth; - $nextHeight = $sourceHeight; - } - else - { - if (Configuration::get('PS_IMAGE_GENERATION_METHOD') == 2 OR (!Configuration::get('PS_IMAGE_GENERATION_METHOD') AND $widthDiff > $heightDiff)) - { - $nextHeight = $destHeight; - $nextWidth = round(($sourceWidth * $nextHeight) / $sourceHeight); - $destWidth = (int)(!Configuration::get('PS_IMAGE_GENERATION_METHOD') ? $destWidth : $nextWidth); - } - else - { - $nextWidth = $destWidth; - $nextHeight = round($sourceHeight * $destWidth / $sourceWidth); - $destHeight = (int)(!Configuration::get('PS_IMAGE_GENERATION_METHOD') ? $destHeight : $nextHeight); - } - } - - $destImage = imagecreatetruecolor($destWidth, $destHeight); - - // If image is a PNG and the output is PNG, fill with transparency. Else fill with white background. - if ($fileType == 'png' && $type == IMAGETYPE_PNG) - { - imagealphablending($destImage, false); - imagesavealpha($destImage, true); - $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127); - imagefilledrectangle($destImage, 0, 0, $destWidth, $destHeight, $transparent); - }else - { - $white = imagecolorallocate($destImage, 255, 255, 255); - imagefilledrectangle ($destImage, 0, 0, $destWidth, $destHeight, $white); - } - - imagecopyresampled($destImage, $sourceImage, (int)(($destWidth - $nextWidth) / 2), (int)(($destHeight - $nextHeight) / 2), 0, 0, $nextWidth, $nextHeight, $sourceWidth, $sourceHeight); - - return (returnDestImage($fileType, $destImage, $destFile)); -} - -/** - * Cut image - * - * @param array $srcFile Image object from $_FILE - * @param string $destFile Destination filename - * @param integer $destWidth Desired width (optional) - * @param integer $destHeight Desired height (optional) - * - * @return boolean Operation result - */ -function imageCut($srcFile, $destFile, $destWidth = NULL, $destHeight = NULL, $fileType = 'jpg', $destX = 0, $destY = 0) -{ - if (!isset($srcFile['tmp_name']) OR !file_exists($srcFile['tmp_name'])) - return false; - - // Source infos - $srcInfos = getimagesize($srcFile['tmp_name']); - $src['width'] = $srcInfos[0]; - $src['height'] = $srcInfos[1]; - $src['ressource'] = createSrcImage($srcInfos[2], $srcFile['tmp_name']); - - // Destination infos - $dest['x'] = $destX; - $dest['y'] = $destY; - $dest['width'] = $destWidth != NULL ? $destWidth : $src['width']; - $dest['height'] = $destHeight != NULL ? $destHeight : $src['height']; - $dest['ressource'] = createDestImage($dest['width'], $dest['height']); - - $white = imagecolorallocate($dest['ressource'], 255, 255, 255); - imagecopyresampled($dest['ressource'], $src['ressource'], 0, 0, $dest['x'], $dest['y'], $dest['width'], $dest['height'], $dest['width'], $dest['height']); - imagecolortransparent($dest['ressource'], $white); - $return = returnDestImage($fileType, $dest['ressource'], $destFile); - return ($return); -} - + * @deprecated 1.5.0 + */ function createSrcImage($type, $filename) { - switch ($type) - { - case 1: - return imagecreatefromgif($filename); - break; - case 3: - return imagecreatefrompng($filename); - break; - case 2: - default: - return imagecreatefromjpeg($filename); - break; - } -} - -function createDestImage($width, $height) -{ - $image = imagecreatetruecolor($width, $height); - $white = imagecolorallocate($image, 255, 255, 255); - imagefill($image, 0, 0, $white); - return $image; -} - -function returnDestImage($type, $ressource, $filename) -{ - $flag = false; - switch ($type) - { - case 'gif': - $flag = imagegif($ressource, $filename); - break; - case 'png': - $quality = (Configuration::get('PS_PNG_QUALITY') === false ? 7 : Configuration::get('PS_PNG_QUALITY')); - $flag = imagepng($ressource, $filename, (int)$quality); - break; - case 'jpg': - case 'jpeg': - default: - $quality = (Configuration::get('PS_JPEG_QUALITY') === false ? 90 : Configuration::get('PS_JPEG_QUALITY')); - $flag = imagejpeg($ressource, $filename, (int)$quality); - break; - } - imagedestroy($ressource); - @chmod($filename, 0664); - return $flag; + Tools::displayAsDeprecated(); + return ImageManager::create($type, $filename); } /** - * Delete product or category image - * - * @param integer $id_item Product or category id - * @param integer $id_image Image id - * TODO This function will soon be deprecated. - */ + * @deprecated 1.5.0 + */ +function createDestImage($width, $height) +{ + Tools::displayAsDeprecated(); + return ImageManager::createWhiteImage($width, $height); +} + +/** + * @deprecated 1.5.0 + */ +function returnDestImage($type, $ressource, $filename) +{ + Tools::displayAsDeprecated(); + return ImageManager::write($type, $ressource, $filename); +} + +/** + * @deprecated 1.5.0 + */ function deleteImage($id_item, $id_image = NULL) { + Tools::displayAsDeprecated(); + // Category if (!$id_image) { diff --git a/install-new/classes/fixtures.php b/install-new/classes/fixtures.php index 8c6f77065..939add645 100644 --- a/install-new/classes/fixtures.php +++ b/install-new/classes/fixtures.php @@ -245,7 +245,7 @@ abstract class InstallFixtures @chmod($target_file, 0644); } // Resize the image if no cache was prepared in fixtures - else if (!imageResize($path.$id.'.jpg', $target_file, $type['width'], $type['height'])) + else if (!ImageManager::resize($path.$id.'.jpg', $target_file, $type['width'], $type['height'])) $this->setError($this->language->l('Cannot create image "%s"', $id.'-'.$type['name'])); } } @@ -280,7 +280,7 @@ abstract class InstallFixtures @chmod($target_file, 0644); } // Resize the image if no cache was prepared in fixtures - else if (!imageResize($path.$id.'.jpg', $target_file, $type['width'], $type['height'])) + else if (!ImageManager::resize($path.$id.'.jpg', $target_file, $type['width'], $type['height'])) $this->setError($this->language->l('Cannot create image "%s"', $id.'-'.$type['name'])); } } diff --git a/install-new/classes/xmlLoader.php b/install-new/classes/xmlLoader.php index bc11a9827..b55a5294c 100644 --- a/install-new/classes/xmlLoader.php +++ b/install-new/classes/xmlLoader.php @@ -559,7 +559,7 @@ class InstallXmlLoader @chmod($target_file, 0644); } // Resize the image if no cache was prepared in fixtures - else if (!imageResize($from_path.$identifier.'.'.$extension, $target_file, $type['width'], $type['height'])) + else if (!ImageManager::resize($from_path.$identifier.'.'.$extension, $target_file, $type['width'], $type['height'])) $this->setError($this->language->l('Cannot create image "%1$s" for entity "%2$s"', $identifier.'-'.$type['name'], $entity)); } } @@ -626,7 +626,7 @@ class InstallXmlLoader @chmod($target_file, 0644); } // Resize the image if no cache was prepared in fixtures - else if (!imageResize($path.$id.'.jpg', $target_file, $type['width'], $type['height'])) + else if (!ImageManager::resize($path.$id.'.jpg', $target_file, $type['width'], $type['height'])) $this->setError($this->language->l('Cannot create image "%1$s" for entity "%2$s"', $identifier.'-'.$type['name'], 'product')); } } diff --git a/install-new/models/install.php b/install-new/models/install.php index 6b79c3523..40e6e91f0 100644 --- a/install-new/models/install.php +++ b/install-new/models/install.php @@ -313,7 +313,7 @@ class InstallModelInstall extends InstallAbstractModel if (file_exists($img_path.$iso.'-default-'.$type['name'].'.jpg')) copy($img_path.$iso.'-default-'.$type['name'].'.jpg', $dst_path.$iso.'-default-'.$type['name'].'.jpg'); else - imageResize($img_path.$iso.'.jpg', $dst_path.$iso.'-default-'.$type['name'].'.jpg', $type['width'], $type['height']); + ImageManager::resize($img_path.$iso.'.jpg', $dst_path.$iso.'-default-'.$type['name'].'.jpg', $type['width'], $type['height']); } } } diff --git a/modules/blockadvertising/blockadvertising.php b/modules/blockadvertising/blockadvertising.php index 536a6de61..b98033a35 100644 --- a/modules/blockadvertising/blockadvertising.php +++ b/modules/blockadvertising/blockadvertising.php @@ -143,7 +143,7 @@ class BlockAdvertising extends Module $file = false; if (isset($_FILES['adv_img']) AND isset($_FILES['adv_img']['tmp_name']) AND !empty($_FILES['adv_img']['tmp_name'])) { - if ($error = checkImage($_FILES['adv_img'], Tools::convertBytes(ini_get('upload_max_filesize')))) + if ($error = ImageManager::validateUpload($_FILES['adv_img'], Tools::convertBytes(ini_get('upload_max_filesize')))) $errors .= $error; elseif ($dot_pos = strrpos($_FILES['adv_img']['name'],'.')) { diff --git a/modules/blockreinsurance/blockreinsurance.php b/modules/blockreinsurance/blockreinsurance.php index 3fcfd3b30..c0177d3d0 100644 --- a/modules/blockreinsurance/blockreinsurance.php +++ b/modules/blockreinsurance/blockreinsurance.php @@ -82,11 +82,11 @@ class blockreinsurance extends Module $filename = explode('.', $_FILES['info'.$i.'_file']['name']); if (isset($_FILES['info'.$i.'_file']) AND isset($_FILES['info'.$i.'_file']['tmp_name']) AND !empty($_FILES['info'.$i.'_file']['tmp_name'])) { - if ($error = checkImage($_FILES['info'.$i.'_file'])) + if ($error = ImageManager::validateUpload($_FILES['info'.$i.'_file'])) return false; elseif (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($_FILES['info'.$i.'_file']['tmp_name'], $tmpName)) return false; - elseif (!imageResize($tmpName, dirname(__FILE__).'/img/'.$filename[0].'.jpg')) + elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/img/'.$filename[0].'.jpg')) return false; unlink($tmpName); } diff --git a/modules/blockstore/blockstore.php b/modules/blockstore/blockstore.php index c8096495e..4004270fb 100644 --- a/modules/blockstore/blockstore.php +++ b/modules/blockstore/blockstore.php @@ -78,7 +78,7 @@ class BlockStore extends Module { if (isset($_FILES['store_img']) && isset($_FILES['store_img']['tmp_name']) && !empty($_FILES['store_img']['tmp_name'])) { - if ($error = checkImage($_FILES['store_img'], 4000000)) + if ($error = ImageManager::validateUpload($_FILES['store_img'], 4000000)) return $this->displayError($this->l('invalid image')); else { diff --git a/modules/editorial/editorial.php b/modules/editorial/editorial.php index e28276cf7..93658d3a9 100644 --- a/modules/editorial/editorial.php +++ b/modules/editorial/editorial.php @@ -158,11 +158,11 @@ class Editorial extends Module Configuration::set('PS_IMAGE_GENERATION_METHOD', 1); if(file_exists(dirname(__FILE__).'/homepage_logo.jpg')) unlink(dirname(__FILE__).'/homepage_logo.jpg'); - if ($error = checkImage($_FILES['body_homepage_logo'])) + if ($error = ImageManager::validateUpload($_FILES['body_homepage_logo'])) $errors .= $error; elseif (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($_FILES['body_homepage_logo']['tmp_name'], $tmpName)) return false; - elseif (!imageResize($tmpName, dirname(__FILE__).'/homepage_logo.jpg')) + elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/homepage_logo.jpg')) $errors .= $this->displayError($this->l('An error occurred during the image upload.')); if (isset($tmpName)) unlink($tmpName); diff --git a/modules/homeslider/homeslider.php b/modules/homeslider/homeslider.php index 2361e829e..94577f788 100644 --- a/modules/homeslider/homeslider.php +++ b/modules/homeslider/homeslider.php @@ -564,11 +564,11 @@ class HomeSlider extends Module { $temp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS'); $salt = sha1(microtime()); - if ($error = checkImage($_FILES['image_'.$language['id_lang']])) + if ($error = ImageManager::validateUpload($_FILES['image_'.$language['id_lang']])) $errors .= $error; else if (!$temp_name || !move_uploaded_file($_FILES['image_'.$language['id_lang']]['tmp_name'], $temp_name)) return false; - else if (!imageResize($temp_name, dirname(__FILE__).'/images/'.Tools::encrypt($_FILES['image_'.$language['id_lang']]['name'].$salt).$type)) + else if (!ImageManager::resize($temp_name, dirname(__FILE__).'/images/'.Tools::encrypt($_FILES['image_'.$language['id_lang']]['name'].$salt).$type)) $errors .= $this->displayError($this->l('An error occurred during the image upload.')); if (isset($temp_name)) @unlink($temp_name); diff --git a/modules/shopimporter/shopimporter.php b/modules/shopimporter/shopimporter.php index 6a4de6005..f04ca1241 100644 --- a/modules/shopimporter/shopimporter.php +++ b/modules/shopimporter/shopimporter.php @@ -869,7 +869,7 @@ class shopimporter extends ImportModule { $imagesTypes = ImageType::getImagesTypes($type); - imageResize($tmpfile, $path.(int)$matchId[$item[$identifier]].'.jpg'); + ImageManager::resize($tmpfile, $path.(int)$matchId[$item[$identifier]].'.jpg'); if ($className == 'Product') { $image = new Image(); @@ -884,13 +884,13 @@ class shopimporter extends ImportModule $legend[Configuration::get('PS_LANG_DEFAULT')] = Tools::link_rewrite($val); $image->legend = $legend; $image->add(); - imageResize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.(int)$image->id.'.jpg'); + ImageManager::resize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.(int)$image->id.'.jpg'); foreach ($imagesTypes AS $k => $imageType) - imageResize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.(int)$image->id.'-'.stripslashes($imageType['name']).'.jpg', $imageType['width'], $imageType['height']); + ImageManager::resize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.(int)$image->id.'-'.stripslashes($imageType['name']).'.jpg', $imageType['width'], $imageType['height']); } else - foreach ($imagesTypes AS $k => $imageType) - imageResize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.stripslashes($imageType['name']).'.jpg', $imageType['width'], $imageType['height']); + foreach ($imagesTypes as $imageType) + ImageManager::resize($tmpfile, $path.(int)$matchId[$item[$identifier]].'-'.stripslashes($imageType['name']).'.jpg', $imageType['width'], $imageType['height']); } else @unlink($tmpfile); diff --git a/modules/watermark/watermark.php b/modules/watermark/watermark.php index 2e0c96395..0640c8cd9 100644 --- a/modules/watermark/watermark.php +++ b/modules/watermark/watermark.php @@ -113,7 +113,7 @@ class Watermark extends Module if (isset($_FILES['PS_WATERMARK']['tmp_name']) AND !empty($_FILES['PS_WATERMARK']['tmp_name'])) { - if (!isPicture($_FILES['PS_WATERMARK'], array('image/gif'))) + if (!ImageManager::isRealImage($_FILES['PS_WATERMARK']['tmp_name'], $_FILES['PS_WATERMARK']['type'], array('image/gif'))) $this->_postErrors[] = $this->l('Image must be in GIF format.'); } @@ -136,7 +136,7 @@ class Watermark extends Module if (isset($_FILES['PS_WATERMARK']) AND !empty($_FILES['PS_WATERMARK']['tmp_name'])) { /* Check watermark validity */ - if ($error = checkImage($_FILES['PS_WATERMARK'])) + if ($error = ImageManager::validateUpload($_FILES['PS_WATERMARK'])) $this->_errors[] = $error; /* Copy new watermark */ elseif(!copy($_FILES['PS_WATERMARK']['tmp_name'], dirname(__FILE__).'/watermark'.$str_shop.'.gif')) @@ -249,7 +249,7 @@ class Watermark extends Module foreach($this->imageTypes as $imageType) { $newFile = _PS_PROD_IMG_DIR_.$image->getExistingImgPath().'-'.stripslashes($imageType['name']).'.jpg'; - if (!imageResize($file, $newFile, (int)$imageType['width'], (int)$imageType['height'])) + if (!ImageManager::resize($file, $newFile, (int)$imageType['width'], (int)$imageType['height'])) $return = false; } return $return; diff --git a/override/classes/ImageManager.php b/override/classes/ImageManager.php new file mode 100644 index 000000000..85607d256 --- /dev/null +++ b/override/classes/ImageManager.php @@ -0,0 +1,7 @@ +