Jump to content

4.0 - Working with images

Working with an image handling suite to resize uploaded images, build thumbnails, apply watermarks and perform other similar image-handling tasks has been improved in 4.0 to be easier than ever. It is important to know that many of the built-in helpers in 4.0 will automatically handle image-related tasks for you, such as building thumbnails when appropriate. In the instances where you need more control or cannot simply make use of an existing helper, however, you will find working with images is as simple as can be.

First and foremost, it should be noted that ImageMagick support is now suite-wide. In past versions of our software, only IP.Gallery could make use of ImageMagick. The image handling configuration options are now part of the core framework and choosing to use ImageMagick as your image handling suite will apply to all image handling suite-wide, including attachment thumbnails, screenshots in IP.Downloads, image uploads in IP.Gallery and everywhere else that makes use of our core image handling classes.

As was mentioned above, you will typically work with images by requesting a user upload one through a form. In this case, using the "Upload" form helper is the simplest route you can go for this purpose, and when doing so all image handling is done for you automatically. You need only pass an option to the helper to specify that the upload must be an image, and optionally you may restrict the maximum dimensions of the image.

If you need finer-grained control or if you need to perform other manual image handling tasks, you simply use IPSImage for this.

The following represents an actual test script that takes an image (named a.png), resizes it to 200x200 proportionately, and writes it to disk as b.png. The script then takes the same a.png file, resizes it to 200x200 but this time crops the image, and writes it to disk as c.png. This script honors the ACP configuration and will use GD or ImageMagick, depending upon what is selected, and will adjust the JPG quality and PNG compression levels based upon the ACP preferences.

<?php

require_once '../../init.php';

$resized    = IPSImage::create( file_get_contents( "a.png" ) );
$resized->resizeToMax( 200, 200 );
file_put_contents( "b.png", (string) $resized );

$cropped    = IPSImage::create( file_get_contents( "a.png" ) );
$cropped->crop( 200, 200 );
file_put_contents( "c.png", (string) $cropped );


Now, typically you should not expect to write directly to disk, and instead should use IPSFile::create() to store a file (which, out of the box, can store files on disk, in the database, in Amazon S3, or on a remote server using FTP). The above test script was merely written to facilitate easier testing of the image handling scripts specifically during development, but they highlight just how easy it is to work with images in 4.0.

If we wanted to apply a watermark to the first image we need only call the watermark() method.

<?php

require_once '../../init.php';

$resized    = IPSImage::create( file_get_contents( "a.png" ) );
$resized->resizeToMax( 200, 200 );
$resized->watermark( IPSImage::create( file_get_contents( "watermark.gif" ) ) );
file_put_contents( "watermarked-image.png", (string) $resized );


We hope you find the new easier to use image handling routines beneficial while developing within the IPS framework.


×
×
  • Create New...