开发者

Cut out a portion of an image (Not resize/thumbnail etc.)

Any way I can cut out a part of an existing image, and then save the result as a new image(f开发者_如何学JAVAile)?


imagecopy allows you to specify x,y,w,h arguments for the src image. Combine this with imagecreatetruecolor and you should easily be able to achieve what you want. There is even an example in the documentation for imagecopy:

// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);

// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);

Use imagejpeg or imagepng to save the image to a file.


You can use the gd functions for this (manual).

Load the source image (imagecreatefromstring() can be useful, so you don't have to specify the type of the image), create an output image (imagecreatetruecolor()) and use imagecopy() (if you don't want to resize it). At the end use imagepng() to output the image, or save it to a file.

Be warned that gd doesn't use image compression in memory, so your PHP process might require lots of RAM when creating a high resolution mosaic. Use imagefree() as soon as possible.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜