Cut out part of the image (do not resize / thumbnail, etc.)

In any case, can I cut out part of an existing image and then save the result as a new image (file)?

+4
source share
2 answers

imagecopy allows imagecopy to specify the arguments x, y, w, h for the src image. Combine this with imagecreatetruecolor and you can easily achieve what you want. The documentation for imagecopy even has an example:

 // 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 in a file.

+5
source

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

Download the original image ( imagecreatefromstring() can be useful, so you do not need to specify the type of image), create an output image ( imagecreatetruecolor() ) and use imagecopy() (if you do not use, you want to resize it). In the end, use imagepng() to output the image or save it to a file.

We will warn you that gd does not use image compression in memory, so it may take a lot of RAM to create a PHP mosaic when creating a PHP process. Use imagefree() as soon as possible.

+1
source

Source: https://habr.com/ru/post/1311954/


All Articles