Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[-] BO : fix issue with image resize when original is smaller than target #3289

Merged
merged 4 commits into from
Jun 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions classes/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,23 @@ public static function checkImageMemoryLimit($image)
/**
* Resize, cut and optimize image
*
* @param string $src_file Image object from $_FILE
* @param string $dst_file Destination filename
* @param int $dst_width Desired width (optional)
* @param int $dst_height Desired height (optional)
* @param string $file_type
* @param bool $force_type
* @param int $error
* @param int $width
* @param int $height
* @param int $quality
* @param string $src_file Image object from $_FILE
* @param string $dst_file Destination filename
* @param int $dst_width Desired width (optional)
* @param int $dst_height Desired height (optional)
* @param string $file_type
* @param bool $force_type
* @param int $error
* @param int $tgt_width
* @param int $tgt_height
* @param int $quality
* @param int $src_width
* @param int $src_height
* @return bool Operation result
*/
public static function resize($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg', $force_type = false, &$error = 0, &$width = null, &$height = null, $quality = 5)
public static function resize($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg',
$force_type = false, &$error = 0, &$tgt_width = null, &$tgt_height = null, $quality = 5,
&$src_width = null, &$src_height = null)
{
if (PHP_VERSION_ID < 50300)
clearstatcache();
Expand Down Expand Up @@ -229,8 +233,8 @@ public static function resize($src_file, $dst_file, $dst_width = null, $dst_heig
if (!ImageManager::checkImageMemoryLimit($src_file))
return !($error = self::ERROR_MEMORY_LIMIT);

$width = $dst_width;
$height = $dst_height;
$tgt_width = $dst_width;
$tgt_height = $dst_height;

$dest_image = imagecreatetruecolor($dst_width, $dst_height);

Expand Down
28 changes: 19 additions & 9 deletions controllers/admin/AdminImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1048,51 +1048,61 @@ protected static function copyImg($id_entity, $id_image = null, $url, $entity =
if (!ImageManager::checkImageMemoryLimit($url))
return false;

$orig_tmpfile = $tmpfile;

// 'file_exists' doesn't work on distant file, and getimagesize makes the import slower.
// Just hide the warning, the processing will be the same.
if (Tools::copy($url, $tmpfile))
{
$last_width = $last_height = 0;
$tgt_width = $tgt_height = 0;
$src_width = $src_height = 0;
$error = 0;
ImageManager::resize($tmpfile, $path.'.jpg', null, null, 'jpg', false, $error, $last_width, $last_height);
ImageManager::resize($tmpfile, $path.'.jpg', null, null, 'jpg', false, $error, $tgt_width, $tgt_height, 5,
$src_width, $src_height);
$images_types = ImageType::getImagesTypes($entity, true);

if ($regenerate)
{
$previous_path = null;
$path_infos = array();
$path_infos[] = array(PHP_INT_MAX, PHP_INT_MAX, $tmpfile);
$path_infos[] = array($tgt_width, $tgt_height, $path.'.jpg');
foreach ($images_types as $image_type)
{
$tmpfile = self::get_best_path($image_type['width'], $image_type['height'], $path_infos);

ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'], $image_type['height'], 'jpg', false, $error, $last_width, $last_height);

$path_infos[] = array($last_width, $last_height, $path.'-'.stripslashes($image_type['name']).'.jpg');
if (ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'],
$image_type['height'], 'jpg', false, $error, $tgt_width, $tgt_height, 5,
$src_width, $src_height))
{
// the last image should not be added in the candidate list if it's bigger than the original image
if ($tgt_width <= $src_width && $tgt_height <= $src_height)
$path_infos[] = array($tgt_width, $tgt_height, $path.'-'.stripslashes($image_type['name']).'.jpg');
}
if (in_array($image_type['id_image_type'], $watermark_types))
Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
}
}
}
else
{
unlink($tmpfile);
@unlink($orig_tmpfile);
return false;
}
unlink($tmpfile);
unlink($orig_tmpfile);
return true;
}

private static function get_best_path($tgt_width, $tgt_height, $path_infos)
{
$path_infos = array_reverse($path_infos);
$path = '';
foreach($path_infos as $path_info)
{
list($width, $height, $path) = $path_info;
if ($width >= $tgt_width && $height >= $tgt_height)
return $path;
}
return '';
return $path;
}

public function categoryImport()
Expand Down