【问题标题】:php image resize code and high cpu usagephp图像调整大小代码和高cpu使用率
【发布时间】:2014-11-11 01:42:17
【问题描述】:

我有以下 php 代码,它可以调整 cbimages 文件夹中的图像大小并在另一个文件夹 cbimages/thumbs/ 中创建缩略图图像

<?php
//Maximize script execution time
ini_set('max_execution_time', 0);

//Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory    = '/var/www/example.com/public_html/cbimages/'; //Source Image Directory End with Slash
$DestImagesDirectory    = '/var/www/example.com/public_html/cbimages/thumbs/'; //Destination Image Directory End with Slash
$NewImageWidth      = 150; //New Width of Image
$NewImageHeight     = 150; // New Height of Image
$Quality        = 80; //Image Quality


//Open Source Image directory, loop through each Image and resize it.
if($dir = opendir($ImagesDirectory)){
    while(($file = readdir($dir))!== false){

        $imagePath = $ImagesDirectory.$file;
        $destPath = $DestImagesDirectory.$file;
        $checkValidImage = @getimagesize($imagePath);

        if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
        {
            //Image looks valid, resize.
            if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
            {
                echo $file.' - Resize Success!<br />';
                /*
                Now Image is resized, may be save information in database?
                */
            }else{
                echo $file.' - Resize Failed!<br />';
            }
        }
    }
    closedir($dir);
}

//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
    list($iWidth,$iHeight,$type)    = getimagesize($SrcImage);
    $ImageScale             = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
    $NewWidth               = ceil($ImageScale*$iWidth);
    $NewHeight              = ceil($ImageScale*$iHeight);
    $NewCanves              = imagecreatetruecolor($NewWidth, $NewHeight);

    switch(strtolower(image_type_to_mime_type($type)))
    {
        case 'image/jpeg':
        case 'image/png':
        case 'image/gif':
            $NewImage = imagecreatefromjpeg($SrcImage);
            break;
        default:
            return false;
    }

    // Resize Image
    if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
    {
        // copy file
        if(imagejpeg($NewCanves,$DestImage,$Quality))
        {
            imagedestroy($NewCanves);
            return true;
        }
    }
}
?>

该脚本在我使用 ubuntu 14.04 的本地开发环境中运行良好并且运行速度很快。在我的 ubuntu 14.04 服务器上,杯子的使用率高达 100%,需要时间才能完成。

运行脚本花费了太多时间,CPU 使用率高达 100%。每次我启动这个脚本时,它都会开始调整所有图像的大小..即使是那些调整大小的。

如何优化脚本??

请求帮助。提前致谢。

【问题讨论】:

  • 禁用的功能不相关。查看哪些扩展是活动的或缺少的。比较两台服务器的php_info()
  • @Havenard 没有其他区别...
  • 比较php.ini 文件。有比较工具吗?
  • 没有循环怎么办?我个人建议使用 SPL 文件系统或目录迭代器,甚至使用 scandir 代替 openDir 和所有那些 jaz,如果你没有 GD,你可能会立即知道,很可能你有一个无限循环
  • 用 php 创建缩略图确实很痛苦,即使在本地环境中也是如此。我会在我的图书馆之间寻找一个我为此准备的课程,并将其作为答案发布,看看它是否更快,但我对此表示怀疑。

标签: php opendir


【解决方案1】:

看看这个类是否有什么不同:

class thumb{

    var $image;
    var $type;
    var $width;
    var $height;

    //read image method
    function loadImage($name){

        //get image dimensions
        $info = getimagesize($name);

        if($info){

            $this->width = $info[0];
            $this->height = $info[1];
            $this->type = $info[2];

            //create new image
            switch($this->type){
                case IMAGETYPE_BMP:
                    $this->image = imagecreatefromwbmp($name);
                    break;
                case IMAGETYPE_JPEG:
                    $this->image = imagecreatefromjpeg($name);
                    break;
                case IMAGETYPE_GIF:
                    $this->image = imagecreatefromgif($name);
                    break;
                case IMAGETYPE_PNG:
                    $this->image = imagecreatefrompng($name);
                    break;
                default: $this->image = false;
            }

            return $this->image;

        }
        else{return false;}

    }

    //store the image
    function saveImage($name, $quality = 100){

        switch($this->type){
            case IMAGETYPE_BMP:
                $go = imagewbmp($this->image, $name);
                break;
            case IMAGETYPE_JPEG:
                $go = imagejpeg($this->image, $name, $quality);
                break;
            case IMAGETYPE_GIF:
                $go = imagegif($this->image, $name);
                break;
            case IMAGETYPE_PNG:
                $pngquality = floor(($quality - 10) / 10);
                $go = imagepng($this->image, $name, $pngquality);
                break;
            default: $go = false;
        }

        return $go;

    }

    //resize image dimensions proportionally
    function resizeImage($x, $y){

        //get resizing properties
        $wd = $x;
        $hg = $y;

        if($this->width >= $this->height){$hg = round($this->height / ($this->width / $x));}
        else{$wd = round($this->width / ($this->height / $y));}

        if($wd < $x){
            $wd = $x;
            $hg = round($this->height / ($this->width / $x));
        }
        else{
            if($hg < $y){
                $hg = $y;
                $wd = round($this->width / ($this->height / $y));
            }
        }

        //create image based on properties
        $image = imagecreatetruecolor($wd, $hg);

        //make image copy based on properties
        $go = imagecopyresampled($image, $this->image, 0, 0, 0, 0, $wd, $hg, $this->width, $this->height);

        //refresh image with new dimensions on success
        if($go){
            $this->width = imagesx($image);
            $this->height = imagesy($image);
            $this->image = $image;
        }

        return $go;

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2011-06-12
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多