php图像调整代码和高CPU使用率

时间:2014-11-11 01:42:18

标签: php opendir

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

如何优化脚本?

请求帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

看看这个班级是否有任何区别:

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;

    }

}