4 Best Libraries For Image Processing in PHP

PHP is often used for web development because it can generate HTML output. However, PHP can also create and manipulate image files. This is convenient because PHP can output image streams directly to a browser.

Image processing is a technique that alters the image data to change the appearance of the image. This can be done for a number of reasons, such as improving the image quality, altering the colors, or adding special effects.

For example, if you want to create a thumbnail of an image, you can use PHP to resize the image and output it directly to the viewer’s browser. If you want to add a watermark to an image, you can use PHP to overlay the image with your watermark. And if you want to create an animated GIF, you can use PHP to combine multiple images into one GIF file.

In this article, we will focus on PHP libraries that can be used to work with image

GD Library

The GD Graphics Library is a software library that was created by Thomas Boutell and others. Its native programming language is ANSI C, but it has interfaces for many other programming languages.

$file = 'php.png';
$image = imagecreatefrompng($file);

header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_WBMP));
image2wbmp($image); // output the stream directly
imagedestroy($image);

The GD library is used for image creation, manipulation, and GIF animation support. It supports many common image formats such as JPEG, PNG, and GIF.

It can be used to create charts, anti-robot security images, thumbnail images, or even build images from other images. GD can be used for tasks such as creating thumbnails or watermarks. GD is also frequently used for CAPTCHA generation. 

Imagick

Imagick is a native PHP extension that enables PHP developers to manipulate images using the ImageMagick API. ImageMagick is a software suite that can create, edit, and compose bitmap images as well as read, convert, and write images in a variety of formats.

function frameImage($imagePath, $color, $width, $height, $innerBevel, $outerBevel) {
    $imagick = new \Imagick(realpath($imagePath));

    $width = $width + $innerBevel + $outerBevel;
    $height = $height + $innerBevel + $outerBevel;

    $imagick->frameimage(
        $color,
        $width,
        $height,
        $innerBevel,
        $outerBevel
    );
    header("Content-Type: image/jpg");
    echo $imagick->getImageBlob();
}

It supports several common image formats. Formats that ImageMagick can read, convert, and write include, but are not limited to DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF.

The library offers a wide range of features such as cropping, rescaling, and filters. Imagick also has good performance due to its use of multiple threads for image processing tasks. 

Gmagick

Gmagick is a PHP extension that allows for creating, modifying, and obtaining metadata associated with images. It does this by using the GraphicsMagick API.

GraphicsMagick is an open-source program that is aiming to be the swiss army knife when it comes to image processing. As of now, it supports over 88 major formats which include important formats like DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF.

It offers most of the same features as Imagick but has better support for newer image formats such as WebP and JPEG2000.

The API consists of the Gmagick class – the main class, a GmagickDraw class – which can be seen as a drawing wand and a GmagickPixel class whose instances represent a single pixel of an image with its color and opacity.

In order to use this extension, one needs to have GraphicsMagick installed on their system.

ImageWorkshop

ImageWorkshop is a PHP5.3+ library that helps you to manage images based on the GD library. With ImageWorkshop, you can easily superimpose many layers or even layer groups, each layer having a background image.

$norwayLayer = ImageWorkshop::initFromPath('/path/to/images/norway.jpg');


$watermarkLayer = ImageWorkshop::initFromPath('/path/to/images/watermark.png');
$norwayLayer->addLayerOnTop($watermarkLayer, 12, 12, "LB");
$image = $norwayLayer->getResult();  

//display
header('Content-type: image/jpeg');
imagejpeg($image, null, 95); 
exit;

This allows for great flexibility and makes the class ideal for simple tasks like creating thumbnails or pasting watermarks, as well as more complex tasks.

Some of the things you can do with ImageWorkshop include pasting an image (or multiple) on another one, cropping, moving, resizing, rotating, superposing, and writing.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close