views:

176

answers:

3

Hi! I have many (about 1000) images (printscreens), and I need to crop these images (all cropped images are in the same region in the full image).

How can I do this in php? Or maybe GIMP is supporting some macro scripts to do this?

Thanks in advance.

+2  A: 

You can do it in PHP using the GD image functions.

Your script might look something like this (not tested):

$it = new RecursiveDirectoryIterator('./screenshots');
foreach ($it as $file)
{
    if (!preg_match('/\.jpe?g$/i', $file->getFilename()))
     continue;

    $src = imagecreatefromjpeg($file->getRealPath());
    $dest = imagecreatetruecolor(1000, 1000);

    imagecopyresampled($desc, $src, 0, 0, X_OFFSET, Y_OFFSET, 1000, 1000, WIDTH, HEIGHT);
    imagejpeg($dest, './resized/' . $file->getFilename());
}
Greg
A: 

To do this with PHP you'll need a GD image manipulation library. Then you can use imagecopy() function to crop you image and many others GD functions to manipulate the image in whatever way you need.

n1313
+2  A: 

Except using GD, as Greg and n1313 proposed, you can also use ImageMagick for that, e.g. similar to Greg's solution, but with

$original_image = new Imagick($file->getRealPath());
$dest_image = $original_image->clone();
$dest_image->cropImage($width, $height, $x, $y);
$dest_image->writeImage('./resized/' . $file->getFilename());

You might check for exceptions when creating the new image (i.e. image cannot be opened) or returned false on cropImage and writeImage (image cannot be cropped or cannot be written).

Residuum