Saving images

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() and pass the filename as the first parameter. The format is determined from the extension.

  1. $img->saveToFile('/path/to/image.png');

Some formats support additional parameters. For example, you can set the quality of the image for JPEG, and compression level for PNG.

  1. // save to jpeg, quality=40
  2. $img->saveToFile('image.jpg', 40);
  3. // save to png, compression level = 6
  4. $img->saveToFile('image.png', 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 with the output() method. This will also write 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 output().

  1. // output a GIF image
  2. $img->output('gif');
  3.  
  4. // output a JPEG with quality=45
  5. $img->output('jpg', 45);

Retrieving as a string

You can retrieve the image data into a string variable by calling asString(). The first parameter must be the image format.

  1. $data = $img->asString('gif');

Additional parameters can be specified, just as with saveToFile():

  1. // will retrieve a jpeg, $quality=80
  2. $data = $img->asString('jpg', 80);

Format specifics: additional parameters

JPEG, PNG and GD2 formats support additional parameters:

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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Tumblr
  • Twitter