-1

I have the following php code which resizes images in cbimages folder and creates thumbnail images in another folder 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;
        }
    }
}
?>

The script is running fine at my local development environment with ubuntu 14.04 and runs fast. On my ubuntu 14.04 server the cup usage shoots upto 100% and takes time to complete.

It is taking too much time to run the script and CPU usage shoots upto 100%. Everytime i start this script it starts resizing all the images..even those resized.

How can i optimize the script ??

Help requested. Thanks in advance.

user3790186
  • 239
  • 6
  • 21
  • Disabled functions are not relevant. See what extensions are active or missing. Compare `php_info()` from both servers. – Havenard Nov 11 '14 at 01:45
  • @Havenard No other differences... – user3790186 Nov 11 '14 at 01:58
  • Compare the `php.ini` files. Do you have a compare tool? – Havenard Nov 11 '14 at 02:28
  • whats it do without the loop? personally I would suggest using the SPL filesystem or Directory iterator, or even scandir instead of openDir and all that jaz, chances are if you don't have GD you'll know instantly, most likely you have an infinite loop – ArtisticPhoenix Nov 11 '14 at 02:29
  • It is resizing..But it is taking time to complete it and cpu usage is 100% and page displays only after process finished. I need to optimize it... – user3790186 Nov 11 '14 at 02:53
  • 1
    Creating thumbnails with php is a real pain, even in a local environment. I will seek between my libraries a class that I have for that and post it in as an answer to see if it is faster, but I doubt it. –  Nov 11 '14 at 03:14

1 Answers1

0

See if this class makes any difference:

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;

    }

}