ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

性能改进PHP GD调整大小并修剪图像背景,保持正确的比例

2019-09-01 08:31:16  阅读:482  来源: 互联网

标签:php image image-resizing image-processing gd


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

问题:从$this-> _trimBackground()函数获取新修剪的图像尺寸后,是否真的有必要进行第一次图像复制作业,以便首先通过带有新修剪尺寸的imagecopy重新创建图像(并在之后再次调整大小)?或者,是否可以将此作业与以下调整大小部分imagecopyresampled合并?

是否还有其他可能的性能改进,我不知道?欢迎各种表现建议!

功能1:

/**
 * 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;

}

功能2:

/**
 * 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,image,image-resizing,image-processing,gd
来源: https://codeday.me/bug/20190901/1782086.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有