Imagick and crop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
wattee
Forum Newbie
Posts: 12
Joined: Mon Nov 02, 2009 5:00 am

Imagick and crop

Post by wattee »

Hi,

Goal: A function that crops one image multiple times from different y locations (from up to down, only y cordinates) and then saves outputs in a folder
Problem: Im pretty sure my logic in this is messed up, it only ouputs 1 image.

My end goal on this would be slicing the image completely but for now i'd just want to get the function running and then go on with that.

Ok here's what's on my mind.
Say that i have an image of 1024 width and 768 height, when i transform this image into an imagick object i would want to crop this image continually from top until to the end of the image(down side) with 256 pixels a piece.

Here's my code so far:

Code: Select all

function sliceImage() {

 		$ycords = array(0,256,512,768);	
    	
		for ($i=0; $i<=count($ycords); $i++){
		$filename = "pictures/fname_" . $i . ".png"; 
	}

    		foreach ($ycords as $ystart) {
			
			$file = 'test.png';
			$pic = new Imagick($file);
		        $pic->setImageFormat('png');
			$pic->cropImage(256, 256, 0, $ystart);
			$pic->writeImage($filename);
		}

}

What am i doing wrong? is the foreach even appropriate to use in this? any alternatives?
Im really stuck on this, any help is greatly appreciated.
Thanks,
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Imagick and crop

Post by cpetercarter »

Think about this bit:

Code: Select all

for ($i=0; $i<=count($ycords); $i++){
                $filename = "pictures/fname_" . $i . ".png";
        }
It executes 4 times (because there are 4 elements in the $ycords array). Each iteration produces a value for the $filename variable which overrides the previous value. This is pointless, and I am sure it is not what you mean to do!
Then the second part of your code, the foreach loop, also runs 4 times. It writes an image 4 times and gives it the name stored in the variable $filename. Because the value of $filename does not change from one iteration of the foreach loop to the next, each image overwrites the previous one. So you end up with one image, not the four which you want.
I think that something like this would probably work for you:

Code: Select all

function sliceImage() {

                $ycords = array(0,256,512,768);
                $i = 0;
 
foreach ($ycords as $ystart) {
                       
                        $file = 'test.png';
                        $filename = "pictures/fname_" . $i . ".png";
                        $pic = new Imagick($file);
                        $pic->setImageFormat('png');
                        $pic->cropImage(256, 256, 0, $ystart);
                        $pic->writeImage($filename);
                        $i++;
                }

}
User avatar
wattee
Forum Newbie
Posts: 12
Joined: Mon Nov 02, 2009 5:00 am

Re: Imagick and crop

Post by wattee »

hey, thanks for the reply. solved
Post Reply