Skip to content

Commit

Permalink
Update to phpThumb 1.7.21-202307141720 (#16456)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maef authored Jul 25, 2023
1 parent ea9e84a commit 6f59896
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 59 deletions.
58 changes: 33 additions & 25 deletions core/model/phpthumb/phpthumb.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class phpthumb {
public $issafemode = null;
public $php_memory_limit = null;

public $phpthumb_version = '1.7.19-202210110924';
public $phpthumb_version = '1.7.21-202307141720';

//////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -314,6 +314,9 @@ public function __destruct() {
$this->purgeTempFiles();
}

public function __set(string $name, mixed $value): void {
}

// public:
public function purgeTempFiles() {
foreach ($this->tempFilesToDelete as $tempFileToDelete) {
Expand Down Expand Up @@ -1447,7 +1450,7 @@ public function ResolveFilenameToAbsolute($filename) {
$AbsoluteFilename = str_replace(DIRECTORY_SEPARATOR, '/', $AbsoluteFilename);
}
$resolvedAbsoluteFilename = $this->resolvePath($AbsoluteFilename, $this->config_additional_allowed_dirs);
if (!$this->config_allow_src_above_docroot && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($this->config_document_root))).'#', $resolvedAbsoluteFilename)) {
if (!$this->config_allow_src_above_docroot && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($this->config_document_root))).'#', (string)$resolvedAbsoluteFilename)) {
$this->DebugMessage('!$this->config_allow_src_above_docroot therefore setting "'.$AbsoluteFilename.'" (outside "'.$this->realPathSafe($this->config_document_root).'") to null', __FILE__, __LINE__);
return false;
}
Expand Down Expand Up @@ -1492,7 +1495,8 @@ public function ImageMagickWhichConvert() {
if (($cachedwhichconvertstring = @file_get_contents($IMwhichConvertCacheFilename)) !== false) {
$WhichConvert = $cachedwhichconvertstring;
} else {
$WhichConvert = trim(phpthumb_functions::SafeExec('which convert'));
$execResult = phpthumb_functions::SafeExec('which convert');
$WhichConvert = $execResult !== null ? trim($execResult) : null;
@file_put_contents($IMwhichConvertCacheFilename, $WhichConvert);
@chmod($IMwhichConvertCacheFilename, $this->getParameter('config_file_create_mask'));
}
Expand Down Expand Up @@ -1800,7 +1804,7 @@ public function ImageMagickThumbnailToGD() {
// some (older? around 2002) versions of IM won't accept "-resize 100x" but require "-resize 100x100"
$commandline_test = $this->ImageMagickCommandlineBase().' logo: -resize 1x '.phpthumb_functions::escapeshellarg_replacement($IMtempfilename).' 2>&1';
$IMresult_test = phpthumb_functions::SafeExec($commandline_test);
$IMuseExplicitImageOutputDimensions = preg_match('#image dimensions are zero#i', $IMresult_test);
$IMuseExplicitImageOutputDimensions = preg_match('#image dimensions are zero#i', (string)$IMresult_test);
$this->DebugMessage('IMuseExplicitImageOutputDimensions = '. (int) $IMuseExplicitImageOutputDimensions, __FILE__, __LINE__);
if ($fp_im_temp = @fopen($IMtempfilename, 'wb')) {
// erase temp image so ImageMagick logo doesn't get output if other processing fails
Expand Down Expand Up @@ -1986,8 +1990,8 @@ public function ImageMagickThumbnailToGD() {
}
}
list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra);
$nw = ((round($nw) != 0) ? round($nw) : '');
$nh = ((round($nh) != 0) ? round($nh) : '');
$nw = ((isset($nw) && round($nw) != 0) ? round($nw) : '');
$nh = ((isset($nh) && round($nh) != 0) ? round($nh) : '');
$commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($nw.'x'.$nh);
}

Expand Down Expand Up @@ -2019,7 +2023,7 @@ public function ImageMagickThumbnailToGD() {
if (($this->ra % 90) != 0) {
if (preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat)) {
// alpha-capable format
$commandline .= ' -background rgba(255,255,255,0)';
$commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('rgba(255,255,255,0)');
} else {
$commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('#'.($this->bg ? $this->bg : 'FFFFFF'));
}
Expand Down Expand Up @@ -2188,9 +2192,9 @@ public function ImageMagickThumbnailToGD() {

case 'lvl':
@list($band, $method, $threshold) = explode('|', $parameter);
$band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*');
$method = ((strlen($method) > 0) ? (int) $method : 2);
$threshold = ((strlen($threshold) > 0) ? min(max((float) $threshold, 0), 100) : 0.1);
$band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper((string)$band)) : '*');
$method = ((strlen((string)$method) > 0) ? (int) $method : 2);
$threshold = ((strlen((string)$threshold) > 0) ? min(max((float) $threshold, 0), 100) : 0.1);

$band = preg_replace('#[^RGBA\\*]#', '', strtoupper($band));

Expand Down Expand Up @@ -2395,6 +2399,10 @@ public function ImageMagickThumbnailToGD() {
}
}
}
// ImageMagic also support quality for webp - but only for version 7.0.8-68 and above
if (($outputFormat=='webp') && $this->q && $this->ImageMagickSwitchAvailable(['quality'])) {
$commandline .= ' -quality '.phpthumb_functions::escapeshellarg_replacement($this->thumbnailQuality);
}
$commandline .= ' '.$outputFormat.':'.phpthumb_functions::escapeshellarg_replacement($IMtempfilename);
if (!$this->iswindows) {
$commandline .= ' 2>&1';
Expand All @@ -2403,8 +2411,8 @@ public function ImageMagickThumbnailToGD() {
$IMresult = phpthumb_functions::SafeExec($commandline);
clearstatcache();
if (!@file_exists($IMtempfilename) || !@filesize($IMtempfilename)) {
$this->FatalError('ImageMagick failed with message ('.trim($IMresult).')');
$this->DebugMessage('ImageMagick failed with message ('.trim($IMresult).')', __FILE__, __LINE__);
$this->FatalError('ImageMagick failed with message ('.trim((string)$IMresult).')');
$this->DebugMessage('ImageMagick failed with message ('.trim((string)$IMresult).')', __FILE__, __LINE__);
if ($this->iswindows && !$IMresult) {
$this->DebugMessage('Check to make sure that PHP has read+write permissions to "'.dirname($IMtempfilename).'"', __FILE__, __LINE__);
}
Expand Down Expand Up @@ -2630,7 +2638,7 @@ public function AntiOffsiteLinking() {
}
}

if ($allow && $this->config_nohotlink_enabled && preg_match('#^(f|ht)tps?\://#i', $this->src)) {
if ($allow && $this->config_nohotlink_enabled && preg_match('#^(f|ht)tps?\://#i', (string)$this->src)) {
$parsed_url = phpthumb_functions::ParseURLbetter($this->src);
//if (!phpthumb_functions::CaseInsensitiveInArray(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) {
if (!$this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) {
Expand Down Expand Up @@ -2894,8 +2902,8 @@ public function ApplyFilters() {
case 'lvl': // autoLevels
@list($band, $method, $threshold) = explode('|', $parameter, 3);
$band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*');
$method = ((strlen($method) > 0) ? (int) $method : 2);
$threshold = ((strlen($threshold) > 0) ? (float) $threshold : 0.1);
$method = ((strlen((string)$method) > 0) ? (int) $method : 2);
$threshold = ((strlen((string)$threshold) > 0) ? (float) $threshold : 0.1);

$phpthumbFilters->HistogramStretch($this->gdimg_output, $band, $method, $threshold);
break;
Expand Down Expand Up @@ -3002,8 +3010,8 @@ public function ApplyFilters() {
case 'over': // Overlay
@list($filename, $underlay, $margin, $opacity) = explode('|', $parameter, 4);
$underlay = (bool) ($underlay ? $underlay : false);
$margin = ((strlen($margin) > 0) ? $margin : ($underlay ? 0.1 : 0.0));
$opacity = ((strlen($opacity) > 0) ? $opacity : 100);
$margin = ((strlen((string)$margin) > 0) ? $margin : ($underlay ? 0.1 : 0.0));
$opacity = ((strlen((string)$opacity) > 0) ? $opacity : 100);
if (($margin > 0) && ($margin < 1)) {
$margin = min(0.499, $margin);
} elseif (($margin > -1) && ($margin < 0)) {
Expand Down Expand Up @@ -3477,15 +3485,15 @@ public function SetOrientationDependantWidthHeight() {
public function ExtractEXIFgetImageSize() {
$this->DebugMessage('starting ExtractEXIFgetImageSize()', __FILE__, __LINE__);

if (preg_match('#^http:#i', $this->src) && !$this->sourceFilename && $this->rawImageData) {
if (preg_match('#^http:#i', (string)$this->src) && !$this->sourceFilename && $this->rawImageData) {
$this->SourceDataToTempFile();
}
if (null === $this->getimagesizeinfo) {
if ($this->sourceFilename) {
if ($this->getimagesizeinfo = @getimagesize($this->sourceFilename)) {
$this->source_width = $this->getimagesizeinfo[0];
$this->source_height = $this->getimagesizeinfo[1];
$this->DebugMessage('getimagesize('.$this->sourceFilename.') says image is '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__);
$this->source_width = $this->getimagesizeinfo[0];
$this->source_height = $this->getimagesizeinfo[1];
$this->DebugMessage('getimagesize('.$this->sourceFilename.') says image is '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__);
} else {
$this->DebugMessage('getimagesize('.$this->sourceFilename.') failed', __FILE__, __LINE__);
}
Expand Down Expand Up @@ -3682,8 +3690,8 @@ public function SetCacheFilename() {
$broad_directory_name = strtolower(md5($this->rawImageData));
$this->cache_filename .= '_raw'.$broad_directory_name;
} else {
$this->DebugMessage('SetCacheFilename() _src set from md5($this->sourceFilename) "'.$this->sourceFilename.'" = "'.md5($this->sourceFilename).'"', __FILE__, __LINE__);
$broad_directory_name = strtolower(md5($this->sourceFilename));
$this->DebugMessage('SetCacheFilename() _src set from md5($this->sourceFilename) "'.$this->sourceFilename.'" = "'.md5((string)$this->sourceFilename).'"', __FILE__, __LINE__);
$broad_directory_name = strtolower(md5((string)$this->sourceFilename));
$this->cache_filename .= '_src'.$broad_directory_name;
}
if (!empty($_SERVER['HTTP_REFERER']) && $this->config_nooffsitelink_enabled) {
Expand Down Expand Up @@ -3717,8 +3725,8 @@ public function SetCacheFilename() {
$ParametersString .= '_'.$key.substr(md5($this->$key), 0, 4);
}
}
if ($this->thumbnailFormat == 'jpeg') {
// only JPEG output has variable quality option
if (in_array($this->thumbnailFormat, ['jpeg','webp'])) {
// only JPEG and WEBP output has variable quality option
$ParametersString .= '_q'. (int) $this->thumbnailQuality;
}
$this->DebugMessage('SetCacheFilename() _par set from md5('.$ParametersString.')', __FILE__, __LINE__);
Expand Down
57 changes: 32 additions & 25 deletions core/model/phpthumb/phpthumb.filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public function Blur(&$gdimg, $radius=0.5) {
// Move copies of the image around one pixel at the time and merge them with weight
// according to the matrix. The same matrix is simply repeated for higher radii.
for ($i = 0; $i < $radius; $i++) {
imagecopy ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1); // up left
imagecopymerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50.00000); // down right
imagecopymerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left
imagecopymerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25.00000); // up right
imagecopymerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left
imagecopymerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25.00000); // right
imagecopymerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20.00000); // up
imagecopymerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 16.666667); // down
imagecopymerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50.000000); // center
imagecopy ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1); // up left
imagecopymerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50); // down right
imagecopymerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33); // down left
imagecopymerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25); // up right
imagecopymerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33); // left
imagecopymerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25); // right
imagecopymerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20); // up
imagecopymerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 17); // down
imagecopymerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50); // center
imagecopy ($gdimg, $imgBlur, 0, 0, 0, 0, $w, $h);
}
return true;
Expand Down Expand Up @@ -287,8 +287,10 @@ public function Crop(&$gdimg, $left=0, $right=0, $top=0, $bottom=0) {
if (($bottom > 0) && ($bottom < 1)) { $bottom = round($bottom * $oldH); }
$right = min($oldW - $left - 1, $right);
$bottom = min($oldH - $top - 1, $bottom);
$newW = $oldW - $left - $right;
$newH = $oldH - $top - $bottom;
$newW = (int)($oldW - $left - $right);
$newH = (int)($oldH - $top - $bottom);
$left = (int)$left;
$top = (int)$top;

if ($imgCropped = imagecreatetruecolor($newW, $newH)) {
imagecopy($imgCropped, $gdimg, 0, 0, $left, $top, $newW, $newH);
Expand Down Expand Up @@ -362,8 +364,8 @@ public function DropShadow(&$gdimg, $distance, $width, $hexcolor, $angle, $alpha
//for ($i = 0; $i < $width; $i++) {
for ($i = 0; $i < 1; $i++) {
if (!isset($PixelMap[$x][$y]['alpha']) || ($PixelMap[$x][$y]['alpha'] > 0)) {
if (isset($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']) && ($PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha'] < 127)) {
$thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + $Offset['x']][$y + $Offset['y']]['alpha']);
if (isset($PixelMap[$x + (int)$Offset['x']][$y + (int)$Offset['y']]['alpha']) && ($PixelMap[$x + (int)$Offset['x']][$y + (int)$Offset['y']]['alpha'] < 127)) {
$thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + (int)$Offset['x']][$y + (int)$Offset['y']]['alpha']);
imagesetpixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
}
}
Expand Down Expand Up @@ -1018,7 +1020,7 @@ public function SourceTransparentColorMask(&$gdimg, $hexcolor, $min_limit=5, $ma
for ($y = 0; $y < $height; $y++) {
$currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
$colorDiff = phpthumb_functions::PixelColorDifferencePercent($currentPixel, $targetPixel);
$grayLevel = min($cutoffRange, max(0, -$min_limit + $colorDiff)) * (255 / max(1, $cutoffRange));
$grayLevel = (int)min($cutoffRange, max(0, -$min_limit + $colorDiff)) * (255 / max(1, $cutoffRange));
$newColor = imagecolorallocate($gdimg_mask, $grayLevel, $grayLevel, $grayLevel);
imagesetpixel($gdimg_mask, $x, $y, $newColor);
}
Expand Down Expand Up @@ -1127,16 +1129,20 @@ public function WatermarkText(&$gdimg, $text, $size, $alignment, $hex_color='000
}
$lineheight = min(100.0, max(0.01, (float) $lineheight));

$metaTextArray = array(
'^Fb' => $this->phpThumbObject->getimagesizeinfo['filesize'],
'^Fk' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1024),
'^Fm' => round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1048576),
'^X' => $this->phpThumbObject->getimagesizeinfo[0],
'^Y' => $this->phpThumbObject->getimagesizeinfo[1],
'^x' => imagesx($gdimg),
'^y' => imagesy($gdimg),
'^^' => '^',
);
$metaTextArray = array();
if (is_array($this->phpThumbObject->getimagesizeinfo)) {
if (array_key_exists('filesize',$this->phpThumbObject->getimagesizeinfo)) {
$metaTextArray['^Fb'] = $this->phpThumbObject->getimagesizeinfo['filesize'];
$metaTextArray['^Fk'] = round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1024);
$metaTextArray['^Fm'] = round($this->phpThumbObject->getimagesizeinfo['filesize'] / 1048576);
}
$metaTextArray['^X'] = $this->phpThumbObject->getimagesizeinfo[0];
$metaTextArray['^Y'] = $this->phpThumbObject->getimagesizeinfo[1];
}
$metaTextArray['^x'] = imagesx($gdimg);
$metaTextArray['^y'] = imagesy($gdimg);
$metaTextArray['^^'] = '^';

$text = strtr($text, $metaTextArray);

$text = str_replace(array(
Expand Down Expand Up @@ -1430,7 +1436,8 @@ public function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment='*',
$watermark_source_width = imagesx($img_watermark);
$watermark_source_height = imagesy($img_watermark);
$watermark_opacity_percent = max(0, min(100, $opacity));
$margin_y = (null === $margin_y ? $margin_x : $margin_y);
$margin_x = (null === $margin_x ? 0 : $margin_y); // if null fill 0 (or 5?)
$margin_y = (null === $margin_y ? $margin_x : $margin_y);
$watermark_margin_x = ((($margin_x > 0) && ($margin_x < 1)) ? round((1 - $margin_x) * $img_source_width) : $margin_x);
$watermark_margin_y = ((($margin_y > 0) && ($margin_y < 1)) ? round((1 - $margin_y) * $img_source_height) : $margin_y);
$watermark_destination_x = 0;
Expand Down
Loading

0 comments on commit 6f59896

Please sign in to comment.