php图像大小调整代码和高cpu使用率


php image resize code and high cpu usage

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

<?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%。每次我启动这个脚本时,它都会开始调整所有图像的大小。。即使是那些调整了大小的。

如何优化脚本??

请求帮助。提前谢谢。

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

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;
    }
}