Image Processing with GD
header("Content-Type: $mimeType"); and
image____ to generate only an image to the output, be sure to output nothing else, note even a blank line after
?>. (That can be a difficult 'bug' to track down -- you get no image and no clue as to why.) The general advice is to not include ?> at all here.
Creating an image
To create a blank image, use the
$img is now a resource variable for an image resource with
$height pixels. Note that width counts from left to right, and height counts from top to bottom.
Image resources can also be created from image creation functions, such as:
Image resources may be freed later when there are no more references to them. However, to free the memory immediately (this may be important if you are processing many large images), using
imagedestroy() on an image when it is no longer used might be a good practice.
Converting an image
Images created by image conversion does not modify the image until you output it. Therefore, an image converter can be as simple as three lines of code:
Image Cropping and Resizing
If you have an image and want to create a new image, with new dimensions, you can use
first create a new
image with desired dimensions:
and store the original image into a variable. To do so, you may use one of the
createimagefrom* functions where * stands for:
Now, copy all (or part of) original image (src_img) into the new image (dst_img) by
dst_* dimensions, use the below image:
Now, if you want to copy entire of source (initial) image, into entire of destination area (no cropping):
An image can be created using
image* functions, where
* is the file format.
They have this syntax in common:
Saving to a file
If you want to save the image to a file, you can pass the filename, or an opened file stream, as
$to. If you pass a stream, you don't need to close it, because GD will automatically close it.
For example, to save a PNG file:
fopen, make sure to use the
b flag rather than the
t flag, because the file is a binary output.
Do not try to pass
fopen("php://temp", $f) or
fopen("php://memory", $f) to it. Since the stream is closed by the function after the call, you will be unable to use it further, such as to retrieve its contents.
Output as an HTTP response
If you want to directly return this image as the response of the image (e.g. to create dynamic badges), you don't need to pass anything (or pass
null) as the second argument. However, in the HTTP response, you need to specify your content type:
$mimeType is the MIME type of the format you are returning. Examples include
Writing into a variable
There are two ways to write into a variable.
Using OB (Output Buffering)
Using stream wrappers
You may have many reasons that you don't want to use output buffering. For example, you may already have OB on. Therefore, an alternative is needed.
stream_wrapper_register function, a new stream wrapper can be registered. Hence, you can pass a stream to the image output function, and retrieve it later.
In this example, the
GlobalStream class writes any input into the reference variable (i.e. indirectly write to the global variable of the given name). The global variable can later be retrieved directly.
There are some special things to note:
- A fully implemented stream wrapper class should look like this, but according to tests with the
__callmagic method, only
stream_closeare called from internal functions.
- No flags are required in the
fopencall, but you should at least pass an empty string. This is because the
fopenfunction expects such parameter, and even if you don't use it in your
stream_openimplementation, a dummy one is still required.
- According to tests,
stream_writeis called multiple times. Remember to use
.=(concatenation assignment), not
=(direct variable assignment).
<img> HTML tag, an image can be directly provided rather than using an external link: