Intervention Image, image manipulation with PHP

High level PHP library for image manipulation, which can work with either the GD Library or Imagick. Examples of use, for uploading and manipulation of images.

In this article we are going to explain one of the most useful libraries for working with images in PHP applications. We will see some practical examples of common use cases, such as uploading images or their manipulation to create thumbnails.

Basically it is a library that offers us a complete API to perform various types of manipulations on graphic files, dimension changes, zoom, rotation, watermarks, application of filters with various effects, file optimization, etc.

It is a versatile option, since it is capable of working with the two image manipulation libraries available in PHP, the GD Library or Imagick, so it can work in most environments. The advantage of working with Intervention Image is that we have a common and simple interface, at a higher level, which allows us to be more productive, since it saves dealing with the complexities of libraries and also offers us methods of performing expressively and with less code all kinds of actions that we may need in the applications.

In this article we are going to explain how to use the library for working with images in PHP, but you can also consult its documentation in the .

Installation of Intervention Image

We can use Intervention Image in PHP 5.4 or higher. It also requires the PHP Fileinfo extension (it is common for it to be mounted on any server compatible with PHP) and one of the two image libraries that PHP supports natively: GD Library 2.0 or higher, or Imagick PHP version 6.5.7 on ahead.

The easiest way to integrate the Intervention Image library into a PHP project is via Composer. We achieve this with the command:

php composer.phar require intervention/image Note: If you have the composer executable in your path, you could use the simplified “composer require intervention/image” command. You can learn more about this dependency manager in the .

Once you have Intervention Image installed in your project, all you have to do is use the autoload of composer classes, so that the library is available in your application code.

require ‘vendor/autoload.php’;

Quick example with Intervention Image

We are going to see a first simple example of using the library, to take our first steps. Starting from an image that is previously in our file system, we will create a thumbnail and save it with another file name.

// Autoload from composer require ‘vendor/autoload.php’; // We declare that we are going to use the library, through its namespace use Intervention\Image\ImageManager; // We create an instance of the image manager through the ImageManager class // In this case we are using the Imagick PHP extension $intervention = new ImageManager(array(‘driver’ => ‘imagick’)); // Create an image from an existing file $image = $intervention->make(‘images/img1.jpg’); // We resize this image $image->resize(500, 400); // save the image to the file system, with another name $image->save(‘images/img-thumbnail1.jpg’, 60);

This code could be simplified a bit by concatenating the methods on the image, summarizing the last three steps in a single line of code.

$intervention->make(‘images/img1.jpg’)->resize(500, 400)->save(‘images/img-thumbnail.jpg’, 60);

In this case we have made a single transformation on the image, a change of dimensions, but if we concatenate several calls to different methods on the library, we will be able to do various effects and transformations in a simple way and with really little code.

As you have seen, the resize() method is useful when we know what dimensions we want for the resulting image. However, if the aspect ratio is not that of the original image, it will be warped, stretched or shrunk, leaving it a little messy. We could calculate the target dimensions we want ourselves, so that the proportions are saved, but there are a couple of very useful methods to make this work happen automatically.

$image = $intervention->make(‘images/img1.jpg’); $image->widen(500)->save(‘images/img-thumbnail500.jpg’, 60);

This will create a version of the image that is 500px wide and free height so that it will scale to the height needed for the image to maintain proportions. In a similar way to the widen() method, there is a method called heighten() that receives the dimension in the vertical, resizing with the same proportions and that fixed height.

Note: As an alternative to winden() or heighten() there is a fit() method that allows fixed dimensions, trimming if necessary, intelligently, to get thumbnails at fixed sizes. Later in this article you will see an example.

Use the GD library or Imagick

As mentioned, Intervention Image is capable of working with the two image libraries available in PHP: GD Library or Imagick PHP. Both libraries are valid, although it is much more recommended to use Imagick for several reasons:

  • Imagick PHP offers higher performance
  • Imagick allows working with larger files
  • GD Library has some limitations, so some one-off methods of Intervention Image might not work.

The thing is that not all PHP servers support Imagick for image manipulation, while the GD library is a much more common extension. Be that as it may, it is something that has to do with your server and it depends on the administrator to configure it or not. From a programming point of view, if you want to use the GD library you simply have to instantiate the ImageManager instantiation method indicating that the driver is “gd”.

$intervention = new ImageManager(array(‘driver’ => ‘gd’));

In order not to have problems with images that come directly from modern digital cameras, which usually have high weights, around 5 MB or more, we have to preferably use the Imagick library, indicating that value in the corresponding driver.

$intervention = new ImageManager(array(‘driver’ => ‘imagick’));

It is the only difference that we will have in the code of our application. The rest of the Intervention Image API is common to both drivers. Therefore, it would be a good idea to easily create an environment variable, to store the proper driver for our server. This way we will not have to change the implementation of our code when changing from one server to another with support for one or another library.

Imaging alternative with static methods

In case it helps, there are alternative static methods to build the images. These methods dispense with instantiating the ImageManager class object and sometimes create code that may be more clear to us.

You just have to use a different class from the namespace and create the images in a different way.

use Intervention\Image\ImageManagerStatic as Image; Image::configure(array(‘driver’ => ‘imagick’)); $img = Image::make($_FILES);

From here, all image manipulation tasks are performed in the same way.

Get information from an image

With this library we can easily obtain data about the image with which we are working. For this we use various useful methods. Let’s see it with an example.

$image = $intervention->make(‘images/img1.jpg’); $size = $image->filesize(); $width = $image->width(); $height = $image->height(); echo “Weight: $size, width: $width, height: $height”;

This will tell us the size in bytes of the image, as well as the height in pixels and the width in pixels.

Effects and filters on images

Something very interesting, which can open the doors of various application ideas, is the possibility of applying effects to images. There are simple methods to apply various effects easily. You will find filters such as sharpening the image, converting to black and white, changing the brightness, contrast, etc.

Image sharpening is very useful to increase the quality of the image, after creating a thumbnail, to improve its sharpness.

$image->widen(350)->sharpen()->save(‘images/focused350.jpg’);

The result of this image, with its unfocused version, so that you can see the difference, can be seen below.

We can also create our own custom filters by creating sequences of transformations that we want. These filters are created with classes created by ourselves, which must implement the “FilterInterface” interface. See the documentation for the InterventionImage filter() method for more information.

Image upload and thumbnail creation in PHP

The Intervention Image library does not allow uploading an image from the client’s computer, as this functionality really belongs to PHP. But once the image is uploaded, we can perfectly make the transformations we want, creating the copies of the images that are necessary on the server, with the original size, specific dimensions, thumbnails, etc.

Let’s see a simple example of a page that uploads images, to get an idea of ​​how this step would be performed. We will really see that the actions are practically the same as we have known, with the exception that we are bringing the image from a different location.

This list corresponds to the upload.php file, which calls itself to be able to upload the image selected in the form.

Image upload with PHP and manipulation with Intervention Image

Image:

‘imagick’)); $img = $intervention->make($_FILES); $img->fit(350, 150)->sharpen(15)->save(‘images/upload.jpg’, 75); echo ‘Uploaded image’; } ?>

There are several considerations that should be made about this example:

  • The first corresponds to security. In this example we are not taking into account any security factors, such as checking that the image is really a graphic file, that it has the correct weight, etc.
  • We are assuming that the format of the…
See also  Swap CSS style sheet with jQuery
Loading Facebook Comments ...
Loading Disqus Comments ...