Manipulating Images using the PHP GD Library 

 July 18, 2019

By  Jack Fisher

PHP is a very versatile programming language with many available libraries. A popular one, the GD Library, enables you to manipulate images. This post will take you through the process of manipulating images using the PHP GD library.

To begin any manipulation, one must access an image resource. Here are four functions to accomplish this:

  • imagecreate( int $width, int $height ) – creates a blank image resource of the specified size.
  • imagecreatefromgif( string $filename ) – creates an image resource from the specified GIF format file.
  • imagecreatefromjpeg( string $filename ) – creates an image resource from the specified JPEG format file.
  • imagecreatefrompng( string $filename ) – creates an image resource from the specified PNG format file.

All of these functions return ‘resources’, which are used in the GD Library as parameters.

Let us start off with an image called sunburst.jpg. To obtain the resource, use the following snippet:


<?php
	$img = imagecreatefromjpeg( 'sunburst.jpg' );
?>

We shall now proceed on to two more functions, imagesx( resource $image ) and imagesy( resource $image )imagesx returns the image’s width and imagesy returns the image’s height.

sunburst.jpg has the dimensions 549×359, so the following code:


	echo imagesx( $img ), ' ', imagesy( $img );

will print 549 359.

What good would this image be if we can’t save or display it? The following functions output the resource to the browser or file:

  • imagegif( resource $image [, string $filename ] )
  • imagejpeg( resource $image [, string $filename ] )
  • imagegif( resource $image [, string $filename ] )

For example, if we were to use these functions without the optional parameter $filename:


<?php
	$image = imagecreatefromjpeg( 'sunburst.jpg' );
	header( 'Content-type: image/jpeg' );
	imagejpeg( $image );
	imagedestroy( $image );
?>

sunburst.jpg would be displayed in the browser.

On the other hand, using a $filename:


<?php
	$image = imagecreatefromjpeg( 'sunburst.jpg' );
	imagejpeg( $image, 'sunburstcopy.jpg' );
	imagedestroy( $image );
?>

would output the image to sunburstcopy.jpg.

The function imagedestroy( resource $image ) frees up memory. Remember that each pixel of an image takes up one byte in memory; having several large images open can crash a server. As such, it is always good practice to free up this space.

We can now move to more advanced manipulation:

  • imagecopy( resource $dst_im, resource $src_im, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_w, int $src_h ) – copies an image or part of an image to another image.
  • imagecopymerge( resource $dst_im, resource $src_im, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_w, int $src_h, int $pct ) – copies an image or part of an image to another image with transparency.
  • imagecopy( resource $dst_im, resource $src_im, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_w, int $dst_h, int $src_w, int $src_h ) – copies an image or part of an image to another image with resampling.

Cool, eh? Have fun manipulating images.

Jack Fisher


Hey - I'm Jack. I've been goofing around with code since 2002, and I've come a long way from my first Geocities site. I started Lateral Code 12 years ago and have been blogging ever since.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe to our newsletter now!