• Manipulating Images in PHP: Easy GD functions to make editing images easier.

    Having done quite a bit of delving into using GD to do tasks such as resize images, rotate images and crop images I’ve found it’s quite a long process to code something that should be rather simple. To make it easier I’ve created a set of functions to do these simple tasks, simply…

    Get The Functions

    Download the functions here: GD Functions V1.0

    Using The Functions

    There are currently 3 functions included that are quite flexible and they allow you to:

    • Resize Images
    • Rotate Images
    • Crop Images

    This is a pretty standard set of functions but they are very easy to use, and they do all the heavy GD lifting for you.

    In order to use the functions you must first include the GD Functions into your page.

     

     

    resizeImage(file, width, height, [newFile, [forceResize, [quality]]]);

    Can resize an image in PHP.

    File: The address to the file you wish to resize. eg. “../storage/my-image.jpg”

    Width: The max width (or new width) for the image to be resized to. eg. 125

    Height: The max height (or new height) for the image to be resized to. eg. 125

    NewFile: (optional, default:file) If set, it will create a new file at this address instead of replacing the old image file.eg. “../storage/my-new-image.jpg”

    ForceResize: (optional, default:false) If set to true will force the image to resize (in ratio) to the new height. eg. true

    Quality: (optional, default:90) The quality of the image being exported. Integer between 0-100, 100 being the best quality. eg. 80

     

     

    rotateImage(file, Angle, [newFile]);

    Can rotate an image to any angle in PHP.

    File: The address to the file you wish to rotate. eg. “../storage/my-image.jpg”

    Angle: The angle you would like to rotate the image to. Integer between 0-360. eg. 90

    NewFile: (optional, default:file) If set, it will create a new file at this address instead of replacing the old image file.eg. “../storage/my-new-image.jpg”

     

     

    cropImage(file, X1, Y1, X2, Y2, [width, [height, [newFile]]]);

    Can crop a portion of an image, then resize that portion to a specified width/height in PHP.

    File: The address to the file you wish to crop. eg. “../storage/my-image.jpg”

    X1: The X co-ordinate for the top left corner of the position you wish to cut. Integer. eg. 10

    Y1: The Y co-ordinate for the top left corner of the position you wish to cut. Integer. eg. 10

    X2: The X co-ordinate for the bottom right corner of the position you wish to cut. Integer. eg. 100

    Y2: The Y co-ordinate for the bottom right corner of the position you wish to cut. Integer. eg. 100

    Width: (optional, default:cropWidth) The width you’d like the cropped image to be. If not set it will default to the width of the area being cropped. Integer. eg. 50

    Height: (optional, default:cropHeight) The height you’d like the cropped image to be. If not set it will default to the height of the area being cropped. Integer. eg. 50

    NewFile: (optional, default:file) If set, it will create a new file at this address instead of replacing the old image file.eg. “../storage/my-new-image.jpg”

     

     

    Notes

    • Requires the PHP GD extension installed to work (comes with most hosting set-ups)
    • Supports JPEG, PNG & GIF files.

    Tags:

5 Comments


  1. This is an excellent and ingenious Bravo and thank you very much for sharing!

  2. TechAdviser says:

    Great script. Simple and easy and it has everything what you need for image editing. Thanks for sharing.

  3. thanks you for sharing, this is really useful!

  4. great tip! im gonna implement it on my nex php project.

Leave a comment