Email
 
 

 

Image manipulation with PHP and GD

Posted in PHP on 2012-07-15

Image Manipulation with PHP and the GD Library

 

Resizing images on the fly with PHP uses a PHP library called GD. GD must be installed on the server for image manipulation to work. GD is a graphic manipulation layer in PHP.

Please download this zip file to try resizing and manipulating images on the fly on your PHP enabled server.

Ok, so how to know if you have GD...

Simply copy the file php.php included in the download above to your webserver, then navigate to that page. You will see your PHP info in the browser. As an example, go to www.php-web-host.com/image-manipulation/php.php and you will see one of the sections, about a third of the way down is GD, and it tells you that GD is enabled.

If GD is enabled, you can copy the other two files to your webserver to run a test. The index.php and 1.jpg must be in the same directory for this file because I've hard coded paths. Obviously in your final product you would allow it to pass in files from different paths.

If you upload these two files and then navigate to index.php you will see the original image which is about 2Mb big. Under that you will see three copies of it, all different sizes.

These images are created by the GD library by calling these lines...

createThumb("1.jpg", "1_normal.jpg", $MaxImageDimension);
createThumb("1.jpg", "1_thumb.jpg", $MaxThumbDimension);
createThumb("1.jpg", "1_featured.jpg", $MaxFeaturedDimension);

The function is defined at the top of index.php.

The first argument is the name of the original file, the second argument is the name of the file it should create, and the third argument is the maximum dimension. In this function, it checks the original image to see if the height is bigger, or the width of the original image. It them scales the image to the MaxDimension specified, using this max on the larger of the two sides, and scaling the whole image proportionately.

If you then go to the files by FTP, you will see 4 separate images now in the folder, the original one, and the 3 smaller ones. You will also note their file sizes are acceptable for web use.

An example is here: www.php-web-host.com/image-manipulation/index.php

Obviously you would want to design your software in such a way that you are not creating images like this on the fly. You want to resize once when someone loads a new image, then use the new images in the <img src=""> tag.

Why resize images with GD and PHP?

Well there are two reasons. You can "fit" an image to your specific template using the width and height attributes in an image tag, BUT, that only resizes the display of the image, not the image itself. Think of looking at an object through a magnifying glass. You see a differently sized image, but the original object is still its original size.

This might pose 2 problems:

  1. Often times, images from a digital camera are quite big, more than 2 or 3 Mb in size. Using the image resizing functions of GD included here it resizes the actual image on the server, thus when a visitor to your site views this image, they download the phyiscally smaller one, saving your bandwidth and theirs.
  2. Using the width and height, the image is squashed, or stretched and this often looks quite amatuer. Using this resize function of GD, it not only resize the image, but scales it down proportionately, so the resultant image is a perfectly scaled down version (or scaled up) version of the original.

 

 

Your Questions or Experience?

Please ask questions or make comments about your experiences with large images, or resizing strategies below, we'd love to hear from you.

 

Comments

Add your comment

Display Name:
Web Address:
Email Address: (not displayed)

Comment

SSL Certificate Authority Paypal Verified We accept PayPal

 
 
  © Free, easy content management system
HOME | HOME / STUDENT WEB HOSTING | SMALL BUSINESS WEB HOSTING | BUSINESS WEB HOSTING | COMPANY WEB HOSTING | CORPORATE WEB HOSTING | WEB DEVELOPMENT | WEB HOSTING | SOFTWARE | RESELLERS | SEO | PACKAGE COMPARISON | GENERAL | AFFILIATES | RESELLER 50GB | RESELLER 100GB | RESELLER 150GB | RESELLER 200GB | BLOGS
 
Some policy info