gpt4 book ai didi

php - 性能改进 PHP GD 调整大小和修剪图像背景以保持正确的比例

转载 作者:可可西里 更新时间:2023-11-01 00:47:30 25 4
gpt4 key购买 nike

我使用 PHP GD 库开发了一个图像大小调整和修剪类。我用了skibulks image trim script在第一步裁剪图片背景,在第二步将图片缩放到需要的尺寸(保持原始比例)。

问题:是否真的有必要做第一个imagecopy$this->_trimBackground() 获取新的裁剪图像尺寸后的作业通过 imagecopy 重新创建图像的函数使用新的修剪尺寸(然后再次调整大小)? 或者,是否可以将此作业与以下调整大小的部分合并 imagecopyresampled

还有其他我不知道的可能的性能改进吗?欢迎提出任何性能建议!

函数一:

/**
* Resize image file
*
* @param string $filepath the image filepath
* @param integer $width the width to resize
* @param integer $height the height to resize
* @return (image blob|boolean status)
* @throws Asset_Model_Image_Exception
*/
private function _resizeImageByFilepathAndReturn($filepath, $width, $height) {

list($imageWidth, $imageHeight, $imageType) = getimagesize($filepath);

switch($imageType) {
case IMAGETYPE_GIF:
$gdImage = imagecreatefromgif($filepath);
break;
case IMAGETYPE_JPEG:
$gdImage = imagecreatefromjpeg($filepath);
break;
case IMAGETYPE_PNG:
$gdImage = imagecreatefrompng($filepath);
break;
default:
return false;
}

if($box = $this->_trimBackground($gdImage)) {

$gdTrimmed = imagecreatetruecolor($box['w'], $box['h']);
imagecopy($gdTrimmed, $gdImage, 0, 0, $box['l'], $box['t'], $box['w'], $box['h']);

$imageWidth = $box['w'];
$imageHeight = $box['h'];
$gdImage = $gdTrimmed;

unset($gdTrimmed);

}

if($imageWidth <= $width && $imageHeight <= $height) {

$fwidth = $imageWidth;
$fheight = $imageHeight;

} else {

$wscale = $width / $imageWidth;
$hscale = $height / $imageHeight;
$scale = min($wscale, $hscale);
$fwidth = $scale * $imageWidth;
$fheight = $scale * $imageHeight;

}

$gdThumbnail = imagecreatetruecolor($width, $height);

imagefill($gdThumbnail, 0, 0, 0x00FFFFFF);

imagecopyresampled($gdThumbnail, $gdImage, ($width - $fwidth) / 2, ($height - $fheight) / 2, 0, 0, $fwidth, $fheight, $imageWidth, $imageHeight);

ob_start();
imagejpeg($gdThumbnail, null, 90);
$image = ob_get_contents();
ob_end_clean();

imagedestroy($gdImage);
imagedestroy($gdThumbnail);

return $image;

}

函数二:

/**
* Trim image background
*
* @param $gdImage image ressource
*/
private function _trimBackground($gdImage){

$hex = imagecolorat($gdImage, 0,0);

$width = imagesx($gdImage);
$height = imagesy($gdImage);

$bTop = 0;
$bLft = 0;
$bBtm = $height - 1;
$bRt = $width - 1;

for(; $bTop < $height; ++$bTop) {
for($x = 0; $x < $width; ++$x) {
if(imagecolorat($gdImage, $x, $bTop) != $hex) {
break 2;
}
}
}

if($bTop == $height) {
return false;
}

for(; $bBtm >= 0; --$bBtm) {
for($x = 0; $x < $width; ++$x) {
if(imagecolorat($gdImage, $x, $bBtm) != $hex) {
break 2;
}
}
}

for(; $bLft < $width; ++$bLft) {
for($y = $bTop; $y <= $bBtm; ++$y) {
if(imagecolorat($gdImage, $bLft, $y) != $hex) {
break 2;
}
}
}

for(; $bRt >= 0; --$bRt) {
for($y = $bTop; $y <= $bBtm; ++$y) {
if(imagecolorat($gdImage, $bRt, $y) != $hex) {
break 2;
}
}
}

$bBtm++;
$bRt++;

return array('l' => $bLft, 't' => $bTop, 'r' => $bRt, 'b' => $bBtm, 'w' => $bRt - $bLft, 'h' => $bBtm - $bTop);

}

最佳答案

imagecopy() 将你的 $gdImage 的一部分复制到 $gdTrimmed,几行之后用 $gdTrimmed 覆盖 $gdImage。

Is it really necessary to do the first imagecopy?

这是你应该问自己的问题。

使用 imagedestroy() 函数代替 unset() 可能会大大提高您的性能。这是关于 imagedestroy() 的有用评论:

Reusing an image variable does NOT clear the old data out of memory! You must use imagedestroy() to clear the data out. (I don't know if unset() works as well).

Also note that the image data in memory is raw, so don't base how much memory you are using based on the original filesize of the compressed image (such as jpeg or png).

关于php - 性能改进 PHP GD 调整大小和修剪图像背景以保持正确的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14661673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com