Using crop & +repage (split image and save as separate)

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
jeffreyg
Posts: 14
Joined: 2014-02-10T03:42:50-07:00
Authentication code: 6789

Using crop & +repage (split image and save as separate)

Post by jeffreyg »

Hello,

I'm trying to make 4 images from 1 image:
Image

I used ImageMagick and it works fine. But when I try to type it in Imagick it doesn't work anymore. I get 1 image thats 50x50 pixels, instead of 4 images.

This is my ImageMagick code for testing:

Code: Select all

exec("convert input.jpg -crop 50%x50% +repage output_%d.jpg");
This is my Imagick code:

Code: Select all

$img = new Imagick('input.jpg');
$img->cropImage(50% , 50% , 0, 0);
$img->setImagePage(50, 50, 0, 0);
$img->writeImage('output_%d.jpg');
$img->destroy();


I got 2 known issues with this code which I can't solve:

1. It doesn't allow me to use the %. No matter what I try, with ' and " putting around it and without. It just doesn't work. The workaround that I (can) use is calculating the size of the input image and manually take 50% of both the height and width. But I don't want a ''human hand'' to be involved in this.
2. The +repage that I use in ImageMagick doesn't seem to be working when I use setImagePage with Imagick. I also tried resetImagePage(); but that didn't work either.

After I got the 4 images I want to save them separate.

I already tried a bunch of options and looked around on this forum and some Imagick tutorial sites but I couldn't find a solution that fits my problem..

I'm using ImageMagick 6.8.1-8 and a compatible Imagick version. Besides that, I use the code in PHP version 5.4.7.
Last edited by jeffreyg on 2014-02-17T09:10:13-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Using crop & +repage (split image and save as separate)

Post by Bonzo »

I would guess a percentage crop is not supported - Imagick only supports some of Imagemagicks options.
The workaround that I (can) use is calculating the size of the input image and manually take 25% of both the height and width. But I don't want a ''human hand'' to be involved in this.
Where does the 25% come into your example?

Untested code as I do not use or have access to Imagick; I wonder if it will save all the crops or just the one?

Code: Select all

$size = getimagesize('input.jpg ');

$crop_width = round($size[0]/2);
$crop_height = round($size[1]/2);

$img = new Imagick('input.jpg');
$img->cropImage($crop_width , $crop_height , 0, 0);
$img->setImagePage(50, 50, 0, 0);
$img->writeImage('output_%d.jpg');
$img->destroy();
jeffreyg
Posts: 14
Joined: 2014-02-10T03:42:50-07:00
Authentication code: 6789

Re: Using crop & +repage (split image and save as separate)

Post by jeffreyg »

Thanks for the reply.

I tried your code but it only gives me 1 new image. So your right about that it saves 1 crop and not all 4. I got the top left crop.

And about the 25% I meant 50%, I edited it, thanks.
Post Reply