WideImage supports saving images to files, outputting the image directly to the browser, and retrieving them as a string. With string output, you can easily capture image data and write it for example to a database. See here for the list of supported formats with explanations.
To a file
To save an image to a file, just call saveToFile() method and pass the filename as the first parameter. The format is determined from the extension.
Some formats support additional parameters. For example, you can set the quality of the image for JPEG, and compression level for PNG.
// save to jpeg, quality=40
// save to png, compression level = 6
Besides compression level, PNG format also allows you to specify the filter parameter, which can be any combination of PNG_FILTER_XXX constants.
Outputting to the browser directly
WideImage can output an image directly to browser, along with two headers: Content-length and Content-type. You have to pass the image type as the parameter, and the length is then calculated automatically. If you want to send any additional headers, you must do so manually before calling the output() method.
// output a GIF image
// output a JPEG with quality=45
Retrieving as a string
You can retrieve the image data into a string variable by calling the asString() method. The first parameter must be the image format.
$data = $img->asString('gif');
Additional parameters can be specified, just as with saveToFile():
// will retrieve a jpeg, $quality=80
$data = $img->asString('jpg', 80);
Format specifics: additional parameters
JPEG, PNG and GD2 formats support additional parameters:
- JPEG: quality, see imagejpeg()
- PNG: compression (this isn’t quality!) and filters, see imagepng()
- GD2: chunk size and type, see imagegd2()
Note: don’t be confused if you notice that the compression level parameter of imagepng() has been incorrectly named “quality” in the PHP manual. PNG format has no quality the way JPEG has it (smaller file size ~= uglier image). It’s a lossless image format, so no detail is lost (= the quality remains the same) regardless of the compression level.