[*] Classes : images.inc.php functions are now all deprecated, use the new class ImageManager instead
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 '
|
||||
<div id="image" >
|
||||
'.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).'
|
||||
<p align="center">'.$this->l('File size').' '.(filesize($image) / 1000).'kb</p>
|
||||
<a href="'.self::$currentIndex.'&'.$this->identifier.'='.(int)($id).'&token='.$token.($id_image ? '&id_image='.(int)($id_image) : '').'&deleteImage=1">
|
||||
<img src="../img/admin/delete.gif" alt="'.$this->l('Delete').'" /> '.$this->l('Delete').'</a>
|
||||
@@ -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 '<img src="../img/admin/'.(isset($params['icon'][$tr[$key]]) ? $params['icon'][$tr[$key]] : $params['icon']['default'].'" alt="'.$tr[$key]).'" title="'.$tr[$key].'" />';
|
||||
|
||||
+53
-58
@@ -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.');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @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 '<img src="'._PS_TMP_IMG_.$cache_image.($disable_cache ? '?time='.time() : '').'" alt="" class="imgm" />';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -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'];
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user