Simple Image Manipulation With Intervention Image

Posted on · Tagged in

A lot of web applications include some sort of photo uploading functionality. Whether it’s allowing a user to upload a personal avatar or to create and manage an entire photo gallery with watermarking features, it’s somewhat of a pain to code. PHP includes several built-in functions for accomplishing this using the GD library but they’re cumbersome at best. Luckily someone else feels your pain and has written a class that makes it a snap to work with images. ‘Intervention\Image‘ is written by Oliver Vogel and it integrates brilliantly with our favorite framework Laravel!

Installation like anything that uses Composer is a snap. Add this to the “require” section of your composer.json file.

{
    "require": {
        "intervention/image": "dev-master"
    }
}

Run

$ composer install  

To add use it in Laravel open the ‘config/app.php’ file. Add this to the $providers array.

<?php
'Intervention\Image\ImageServiceProvider'

Next add this to the $aliases array.

<?php
'Image' => 'Intervention\Image\Facades\Image'

This will allow you to reference the class as ‘Image’ and use all of it’s static methods.

One great feature of the ‘Intervention\Image‘ class is that it’s methods are chainable just like most Laravel classes. Uploading, resizing and saving an image to an new location can be accomplished in a single line.

<?php
// resizing an uploaded file
Image::make(Input::file('photo')->getRealPath())->resize(300, 200)->save('foo.jpg');

For more information on ‘Intervention\Image‘ and how it can be used, check out the official website.

Comments

comments powered by Disqus
Subscribe to my newsletter and get a free copy of my book, Aspect Oriented Programming in PHP.