开发者

Cropping images and saving them

I have this .png:

Cropping images and saving them

I want to crop a 16x16 square and save it as another .png with some name, then I need to continue with the next 16 pixels (to开发者_如何学编程 the right and then to the next row) until I complete the entire image, so I will have a total of 256 .png with the individual sprites from the original image.

Is this possible with GD? Am I wasting my time? Is there any other way to do this than taking them with Photoshop and saving them individualy like a crazy?

Thanks :)


This code will copy the first line into 16 little images.

<?php
set_time_limit(0);
for($f1=0;$f1<16;$f1++){
 cropImg(0,16 * $f1,'r0mzR.png','row1'.$f1.'.png');
}

function cropImg($x,$y,$f,$n){
 $image = imagecreatefrompng($f); 
 $crop = imagecreatetruecolor(16,16);
 imagecopy ($crop, $image, 0, 0, $x, $y,16, 16 );
 imagepng($crop,$n);
}
?>

To get the second line just duplicate the second loop like this

 for($f2=0;$f2<16;$f2++){
  cropImg(16,16 * $f2,'r0mzR.png','row2'.$f2.'.png');
 }

And the function cropImg takes 4 parameters first the x location, the y location, the original file name and the output file name.

PS: <3 minecraft

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜