Skip to content
Permalink
Browse files

Thumbnail fix backporting

  • Loading branch information...
chrisgraham committed Aug 12, 2019
1 parent aeda9ff commit 1fdcfd8d385322151ccc4ca60ac032b5b8dbec21
Showing with 54 additions and 24 deletions.
  1. +1 −1 sources/hooks/systems/config/jpeg_quality.php
  2. +27 −2 sources/images.php
  3. +26 −21 sources/images2.php
@@ -51,6 +51,6 @@ public function get_details()
*/
public function get_default()
{
return '75';
return '90';
}
}
@@ -234,6 +234,14 @@ function _symbol_thumbnail($param)
if ($exp_dimensions[1] == '0') {
$exp_dimensions[1] = '1';
}
if ($exp_dimensions[0] == '-1') {
$exp_dimensions[0] = float_to_raw_string(round((floatval($exp_dimensions[1]) * $source_aspect)));
}
if ($exp_dimensions[1] == '-1') {
$exp_dimensions[1] = float_to_raw_string(round((floatval($exp_dimensions[0]) * (1 / $source_aspect))));
}
$destination_aspect = floatval($exp_dimensions[0]) / floatval($exp_dimensions[1]);
// We test the scaled sizes, rather than the ratios
@@ -245,26 +253,43 @@ function _symbol_thumbnail($param)
// Is it too wide, requiring cropping?
$scale = floatval($source_y) / floatval($exp_dimensions[1]);
$modify_image = intval(round(floatval($source_x) / $scale)) != intval($exp_dimensions[0]);
if ($modify_image) {
$algorithm = 'crop';
}
} else {
// Is the image too short, requiring padding?
$scale = floatval($source_x) / floatval($exp_dimensions[0]);
$modify_image = intval(round(floatval($source_y) / $scale)) != intval($exp_dimensions[1]);
if ($modify_image) {
$algorithm = 'pad';
}
}
} elseif ($source_aspect < $destination_aspect) {
// The image is taller than the output
if (($algorithm == 'crop') || ($algorithm == 'pad_vert_crop_vert')) {
// Is it too tall, requiring cropping?
$scale = floatval($source_x) / floatval($exp_dimensions[0]);
$modify_image = intval(round(floatval($source_y) / $scale)) != intval($exp_dimensions[1]);
if ($modify_image) {
$algorithm = 'crop';
}
} else {
// Is the image too narrow, requiring padding?
$scale = floatval($source_y) / floatval($exp_dimensions[1]);
$modify_image = intval(round(floatval($source_x) / $scale)) != intval($exp_dimensions[0]);
if ($modify_image) {
$algorithm = 'pad';
}
}
} else {
// They're the same, within the tolerances of
// floating point arithmetic. Just scale it.
$modify_image = false;
if ($source_x != intval($exp_dimensions[0]) || $source_y != intval($exp_dimensions[1])) {
$scale = floatval($source_x) / floatval($exp_dimensions[0]);
$modify_image = true;
} else {
$modify_image = false;
}
}
// We have a special case here, since we can "pad" an
@@ -539,7 +564,7 @@ function ensure_thumbnail($full_url, $thumb_url, $thumb_dir, $table, $id, $thumb
/**
* Resize an image to the specified size, but retain the aspect ratio.
*
* @param URLPATH $from The URL to the image to resize
* @param string $from The URL to the image to resize. May be either relative or absolute. If $using_path is set it is actually a path
* @param PATH $to The file path (including filename) to where the resized image will be saved
* @param integer $width The maximum width we want our new image to be (-1 means "don't factor this in")
* @param integer $height The maximum height we want our new image to be (-1 means "don't factor this in")
@@ -88,7 +88,7 @@ function _ensure_thumbnail($full_url, $thumb_url, $thumb_dir, $table, $id, $thum
/**
* (Helper for convert_image).
*
* @param URLPATH $from The URL to the image to resize
* @param string $from The URL to the image to resize. May be either relative or absolute. If $using_path is set it is actually a path
* @param PATH $to The file path (including filename) to where the resized image will be saved
* @param integer $width The maximum width we want our new image to be (-1 means "don't factor this in")
* @param integer $height The maximum height we want our new image to be (-1 means "don't factor this in")
@@ -114,27 +114,25 @@ function _convert_image($from, $to, $width, $height, $box_width = -1, $exit_on_e
// Load
$ext = get_file_extension($from);
if ($using_path) {
if (!check_memory_limit_for($from, $exit_on_error)) {
return false;
}
if ($ext == 'svg') { // SVG is pass-through
copy($from, $to);
fix_permissions($to);
sync_file($to);
if ((!check_memory_limit_for($from, $exit_on_error)) || ($ext == 'svg'/*SVG is pass-through*/)) {
if ($using_path) {
copy($from, $to);
fix_permissions($to);
sync_file($to);
}
return true;
}
$from_file = @cms_file_get_contents_safe($from);
$exif = function_exists('exif_read_data') ? @exif_read_data($from) : false;
} else {
$file_path_stub = convert_url_to_path($from);
if (!is_null($file_path_stub)) {
if (!check_memory_limit_for($file_path_stub, $exit_on_error)) {
return false;
}
if ($ext == 'svg') { // SVG is pass-through
copy($file_path_stub, $to);
fix_permissions($to);
sync_file($to);
if ((!check_memory_limit_for($from, $exit_on_error)) || ($ext == 'svg'/*SVG is pass-through*/)) {
if ($using_path) {
copy($file_path_stub, $to);
fix_permissions($to);
sync_file($to);
}
return true;
}
$from_file = @cms_file_get_contents_safe($file_path_stub);
@@ -186,6 +184,10 @@ function _convert_image($from, $to, $width, $height, $box_width = -1, $exit_on_e
$sx = imagesx($source);
$sy = imagesy($source);
// The typical case is to copy the full image from source to destination (PHP may be doing scaling, but that's irrelevant to this)
$copy_width = $sx;
$copy_height = $sy;
$red = null;
if (is_null($thumb_options)) {
@@ -273,7 +275,7 @@ function _convert_image($from, $to, $width, $height, $box_width = -1, $exit_on_e
$wrong_y = intval(round(floatval($sy) / $thumb_options['scale']));
// Handle cropping here
if (($thumb_options['type'] == 'crop') || (($thumb_options['type'] == 'pad_horiz_crop_horiz') && ($wrong_x > $width)) || (($thumb_options['type'] == 'pad_vert_crop_vert') && ($wrong_y > $height))) {
if ($thumb_options['type'] == 'crop') {
// See which direction we're cropping in
if (intval(round(floatval($sx) / $thumb_options['scale'])) != $width) {
$crop_direction = 'x';
@@ -312,6 +314,9 @@ function _convert_image($from, $to, $width, $height, $box_width = -1, $exit_on_e
$sx = intval(($width * $thumb_options['scale']));
$sy = intval(($height * $thumb_options['scale']));
$copy_width = intval(($width * $thumb_options['scale_to']));
$copy_height = intval(($height * $thumb_options['scale_to']));
// We start at the origin of our output
$dest_x = 0;
$dest_y = 0;
@@ -320,7 +325,7 @@ function _convert_image($from, $to, $width, $height, $box_width = -1, $exit_on_e
// too much)
$_width = $width;
$_height = $height;
} elseif ($thumb_options['type'] == 'pad' || (($thumb_options['type'] == 'pad_horiz_crop_horiz') && ($wrong_x < $width)) || (($thumb_options['type'] == 'pad_vert_crop_vert') && ($wrong_y < $height))) {
} elseif ($thumb_options['type'] == 'pad') {
// Padding code lives here. We definitely need to pad some excess space
// because otherwise symbols.php would not call us. Thus we need a
// background (can be transparent). Let's see if we've been given one.
@@ -400,8 +405,8 @@ function _convert_image($from, $to, $width, $height, $box_width = -1, $exit_on_e
// $sx and $sy are fine, since they cover the whole image
$source_x = 0;
$source_y = 0;
$_width = ($pad_axis == 'x') ? intval(round(floatval($sx) / $thumb_options['scale'])) : $width;
$_height = ($pad_axis == 'y') ? intval(round(floatval($sy) / $thumb_options['scale'])) : $height;
$_width = ($pad_axis == 'x') ? ($width - $pad_amount) : $width;
$_height = ($pad_axis == 'y') ? ($height - $pad_amount) : $height;
$dest_x = ($pad_axis == 'x') ? $pad_amount : 0;
$dest_y = ($pad_axis == 'y') ? $pad_amount : 0;
}
@@ -465,7 +470,7 @@ function _convert_image($from, $to, $width, $height, $box_width = -1, $exit_on_e
}
}
imagecopyresampled($dest, $source, $dest_x, $dest_y, $source_x, $source_y, $_width, $_height, $sx, $sy);
imagecopyresampled($dest, $source, $dest_x, $dest_y, $source_x, $source_y, $_width, $_height, $copy_width, $copy_height);
} else { // LEGACY Old GD version, no truecolor support
// Set the background if we have one
if (!is_null($thumb_options) && !is_null($red)) {
@@ -476,7 +481,7 @@ function _convert_image($from, $to, $width, $height, $box_width = -1, $exit_on_e
} else {
$dest = imagecreate($_width, $_height);
}
imagecopyresized($dest, $source, $dest_x, $dest_y, $source_x, $source_y, $_width, $_height, $sx, $sy);
imagecopyresized($dest, $source, $dest_x, $dest_y, $source_x, $source_y, $_width, $_height, $copy_width, $copy_height);
}
// Clean up

0 comments on commit 1fdcfd8

Please sign in to comment.
You can’t perform that action at this time.