Jump to content

Imagick and crop


wattee

Recommended Posts

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:

 

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,

Link to comment
https://forums.phpfreaks.com/topic/202353-imagick-and-crop/
Share on other sites

ok, fixing like this altough outputs the same image per filename, how would i go about fixing this?

function picture() {
           	$fail = 'pildid/bunny.png';
         	$pic = new Imagick($fail);
		 	$pic->setImageFormat('png');   
		 	return $pic;
}

function slice() {

	$ycords = array(0,256,512,768);   


   foreach ($ycords as $ystart) {
		 $pic = picture();	 

	   for ($i=0; $i<count($ycords); $i++){	   	
	         $pic->cropImage(256,256,0,$ystart);  
	         $failinimi = "bunny/bunny_" . $i . ".png";    
	         $pic->writeImages($failinimi, false);
	        }
    }
}

slice();

Link to comment
https://forums.phpfreaks.com/topic/202353-imagick-and-crop/#findComment-1061111
Share on other sites

Untested, but try this

 

function slice() {
$ycords = array(0,256,512,768);
$pic = picture();
for ($i=0; $i<count($ycords); $i++){
	$pic->cropImage(256,256,0,$ycords[$i]);
	$failinimi = "bunny/bunny_" . $i . ".png";
	$pic->writeImages($failinimi, false);
}
}

Link to comment
https://forums.phpfreaks.com/topic/202353-imagick-and-crop/#findComment-1061186
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.