PHP5: Images

Posted on February 18, 2011

PHP5 only has a few new functions relating to graphics, but they’re useful ones.

First is image_type_to_extension(), which returns the file extension for a given image type (such as what’s returned by getimagesize()). So, for example, IMAGETYPE_PNG would return “png”. Nothing genius, but saves you the trouble of maintaining your own function for this.

Speaking of getimagesize(), that function now support icon (.ico) files. Now you can retrieve information about a favicon or other icon file.

Finally, there’s a big addition to the GD library for image manipulation: imagefilter(). This function includes a bunch of built-in filters you can apply to an image:

  • IMG_FILTER_NEGATE — reverses (inverts) all colors of the image
  • IMG_FILTER_GRAYSCALE — converts the image into grayscale
  • IMG_FILTER_BRIGHTNESS — changes the brightness of the image
  • IMG_FILTER_CONTRAST — changes the contrast of the image
  • IMG_FILTER_COLORIZE — like IMG_FILTER_GRAYSCALE, except you can specify the color
  • IMG_FILTER_EDGEDETECT — uses edge detection to highlight the edges in the image
  • IMG_FILTER_EMBOSS — embosses the image
  • IMG_FILTER_GAUSSIAN_BLUR — blurs the image using the Gaussian method
  • IMG_FILTER_SELECTIVE_BLUR — blurs the image
  • IMG_FILTER_MEAN_REMOVAL — uses mean removal to achieve a “sketchy” effect
  • IMG_FILTER_SMOOTH — makes the image smoother
  • IMG_FILTER_PIXELATE — applies pixelation effect to the image

These filters give us the ability to perform simple effects on images without having to write our own functions or use 3rd-party code. Hopefully they will continue to add on to this list of effects in the future. If you need a filter that doesn’t exist, you can try rolling your own using imageconvolution().

Leave a Reply