Question

I am maintaining an old website. Now my client wants to add more than 6000 products. The product images have different sizes. I have to apply batch process. I have to resize them all to a thumb size of 230x230. Is there any way to do if from PHP? If so how?

I have to read all content from different folders which are inside a main images folders. This images folder has 40 subfolders. Each folder name is the category name and the images inside it are products (image).

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

Solution

With php you can read the files inside a folder using

$images= glob("*"); // * will address all files
// or
$images= glob("*.jpg"); // this will address only jpg images

Then loop through $images

foreach ($images as $filename) {
   //resize the image
   if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality)){
     echo $filename.' resize Success!<br />';
   }
}

function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
    list($iWidth,$iHeight,$type)    = getimagesize($SrcImage);

    //if you dont want to rescale image

    $NewWidth=$MaxWidth;
    $NewHeight=$MaxHeight;
    $NewCanves              = imagecreatetruecolor($NewWidth, $NewHeight);

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

OTHER TIPS

You can easily do this with a shell script but if you must do it in PHP I would stick all the images into the same directory and loop through it with..

$img= new Imagick($srcpath);
$img->resizeImage($width,$height,Imagick::FILTER_BOX,1,true);

http://php.net/manual/en/function.imagick-resizeimage.php

You can easily resize single/multiple images with this library. there are only 4-5 lines of PHP code so you can manage easily.

Also, the ability to pass many other options in parameter like height, width, img_dir.

https://github.com/hsleonis/image-resizer


require_once ('class.imageresizer.php');

// Create thumbnails
$args = array(
    'height'    => 975,
    'width'     => 650,
    'is_crop_hard' => 1
);
$img = new ImageResizer($args);
$img->create();</code>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top