JamesHeinrich / phpThumb

phpThumb() - The PHP thumbnail generator
Other
315 stars 98 forks source link

Error, if zc=1 and RU locale #138

Open AndreyMyagkov opened 5 years ago

AndreyMyagkov commented 5 years ago

Please use setLocale (LC_ALL, "en_US.UTF-8")

If outer script set RU locale phpThumb (zc=1) generated broken image

dmpol18 commented 5 years ago

Actually the issue lies in phpthumb class when it generates command line for ImageMagick.

In this chunk of code

$wAll = (int) max($this->w, $this->wp, $this->wl, $this->ws) - (2 * $borderThickness);
$hAll = (int) max($this->h, $this->hp, $this->hl, $this->hs) - (2 * $borderThickness);
$imAR = $this->source_width / $this->source_height;
$zcAR = (($wAll && $hAll) ? $wAll / $hAll : 1);
$side  = phpthumb_functions::nonempty_min($this->source_width, $this->source_height, max($wAll, $hAll));
$sideX = phpthumb_functions::nonempty_min($this->source_width,                       $wAll, round($hAll * $zcAR));
$sideY = phpthumb_functions::nonempty_min(                     $this->source_height, $hAll, round($wAll / $zcAR));

$thumbnailH = round(max($sideY, ($sideY * $zcAR) / $imAR));
if ($this->aoe == 1) {
    $commandline .= ' -'.$IMresizeParameter.' "'.$wAll.'x'.$hAll.'^"';
} else {
    $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($IMuseExplicitImageOutputDimensions ? $thumbnailH : '').'x'.$thumbnailH);
}

This part gives either float(1.5) or float(1,5) depending on setted locale. $imAR = $this->source_width / $this->source_height; Locale setting affects number format when it prints. Some details are here: http://mark-story.com/posts/view/php-floats-localization-and-landmines https://stackoverflow.com/questions/43345915/php-decimal-value-in-array-changes-automatically-from-point-to-comma-as-seperato

So one of the option could be using number_format function. Probably $imAR = number_format (($this->source_width / $this->source_height), 2, '.');

Resulting line looks like this convert '/path/test.jpg[0]' -flatten -density '150' -background '#FFFFFF' -thumbnail 'x2,0E+2' -gravity center -crop '201x201+0+0' +repage -quality '85' -interlace line jpeg:'/path/cache/pThumbbFkvI7' 2>&1 Pay attention to -thumbnail 'x2,0E+2' it also prints as -thumbnail 'x2.0E+2' You can use this locale for debugging setlocale(LC_ALL, 'ru_RU.UTF-8');

JamesHeinrich commented 5 years ago

Please submit a pull request with your suggested modifications and I'll merge it.