wattee Posted May 20, 2010 Share Posted May 20, 2010 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 More sharing options...
litebearer Posted May 20, 2010 Share Posted May 20, 2010 you aren't incrementing the file name in the foreach loop, therefore you keep over-writing the resulting images Link to comment https://forums.phpfreaks.com/topic/202353-imagick-and-crop/#findComment-1061107 Share on other sites More sharing options...
wattee Posted May 20, 2010 Author Share Posted May 20, 2010 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 More sharing options...
litebearer Posted May 20, 2010 Share Posted May 20, 2010 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 More sharing options...
wattee Posted May 24, 2010 Author Share Posted May 24, 2010 hey, thanks for the reply. solved Link to comment https://forums.phpfreaks.com/topic/202353-imagick-and-crop/#findComment-1062553 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.