PHPBuilder - Using the WideImage Library in PHP



RSS Twitter
Articles

Using the WideImage Library in PHP

by: Octavia Andreea Anghel
|
January 22, 2016

Introduction

WideImage is an object-oriented library for image manipulation that provides a simple way of loading, manipulating and saving images in the most common image formats. It requires PHP 5.2+ with GD2 extension WideImage supports all formats that are natively supported by the GD extension on the server (this currently includes GIF, PNG, JPG, GD, GD2, WBMP, XBM, XPM). It also supports BMP (read/write) and TGA (read only).

 

Loading an image

Before performing different operations on the images using the WideImage library, we need first to load an image - and for that we use the load() method that loads an image from a file, URL, HTML input file field, binary string, or a valid image handle. The load() method has the following syntax:

 

static WideImage_Image load( mixed $source)

 


This function checks the input and decides whether to use WideImage::loadFromHandle(), WideImage::loadFromFile(), WideImage::loadFromUpload() or WideImage::loadFromString():

 

 

Parameters: mixed $source File name, url, HTML file input field name, binary string, or a GD image resource.

 

Note: WideImage supports cross-format conversion; you can easily load an image in one format, then save it in another:

 

WideImage::load('file.jpg')->saveToFile('converted.png'); 

 

Smart coordinates

Smart coordinates were implemented to make operations that involve coordinates, dimensions, and positioning easier. They can be useful with any operation that requires coordinates as parameters, like resize, crop, etc.

 

   •  Numeric coordinates: $resized = $image->resize(200, 100);

   •  Percent string: $resized = $image->resize('50%', '25%');

   •  Alignment labels: $cropped = $image->crop('center', 'center', 200, 250);

   •  Complex coordinates (are the combination of the above ones): $cropped = $image->crop('100%-70', '100%-50', 60, 40);

 

Resizing an image

For resizing an image using the WideImage library, we use the resize() method, with the following syntax:

WideImage_Image resize( [mixed $width = null], [mixed $height = null], [string $fit = 'inside'], [string $scale = 'any']) - Resize the image to given dimensions. 

 

The below example resizes the test.jpg image, using the numeric and percent coordinates and saves the new images into a file. To save an image to file use the saveToFile() method:

 

<?php

include "C:\wamp\www\lib\WideImage.php";

//loading the original image
$image = WideImage::load("test.jpg");

//resizing the test.jpg from 320x186 to 200x100
$resized = $image->resize(200, 100);

//saving the resized image to the file
$resized->saveToFile("small1.jpg");

//resizing the test.jpg to 30%
$resized = $image->resize('30%');

//saving the resized image to the file
$resized->saveToFile("small2.jpg");

//outputting the small.jpg resized image
$resized->output('jpg', 100);
?> 

 

The result of running the above listing is:

 

 

Crop an image

For cropping an image using the WideImage library, we use the crop() method, with the following syntax:

 

WideImage_Image crop( [mixed $left = 0], [mixed $top = 0], [mixed $width = '100%'], [mixed $height = '100%']) - and returns a cropped rectangular portion of the image. 

 

The next listing crops a 200×250 rectangle part from the center of our test.jpg image:

 

<?php

include "C:\wamp\www\lib\WideImage.php";

//loading the original image
$image = WideImage::load("test.jpg");

//Crop a 200×250 rectangle from the exact center of the image 
$cropped = $image->crop('50%-100', '50%-125', 200, 250);
$cropped = $image->crop('center', 'center', 200, 250);

//Saving the cropped image to file
$cropped->saveToFile("cropped.jpg");

//Outputting the cropped.jpg image
$cropped->output('jpg', 100);
?> 

 

The output of this listing is:

 

 

Merging two images

For merging an image using the WideImage library, use the merge() method, with the following syntax:

 

WideImage_Image merge( WideImage_Image $overlay, [mixed $left = 0], [mixed $top = 0], [int $pct = 100])- This method lays the overlay (watermark) on the image. 

 

Next example merge the test.jpg image and snowflake.jpg by laying the snowflake.jpg onto the test.jpg on different position and opacity:

 

<?php

include "C:\wamp\www\lib\WideImage.php";

//loading the two images test.jpg and snowflake.png
$image = WideImage::load("test.jpg");
$watermark = WideImage::load("snowflake.png");

//merging the above images by laying the snowflake.png onto the test.jpg 
$new = $image->merge($watermark, 'center', 'bottom – 10', 70);
//$new = $image->merge($watermark, "right - 10", "bottom - 10", 50);

//saving the new image
$new->saveToFile("new.jpg");

//outputting the new image
$new->output('jpg', 100);
?> 

 

The output of this listing is:

 

 

The output image of merging the two above images is:

 

 

Adding shapes and text on an image

Using WideImage library we can also draw text and shapes on the image using the Canvas object, returned by the getCanvas() method.

 

In the next example we will add six different colored circles and an Arial text with the dimension 16 placed on the bottom of image and centered:

 

<?php 

 include "C:\wamp\www\lib\WideImage.php";
 
 //loading the initial image
 $img = WideImage::load('test.jpg');

 //Returns the canvas object that can be used to draw text and shapes on the image
 $canvas = $img->getCanvas();
 
 //drawing six circles of different colors and positions on the image
 $canvas->filledellipse(100, 100, 20, 20, $img->allocateColor(255, 127, 255));
 $canvas->filledellipse(200, 50, 20, 20, $img->allocateColor(123, 0, 255));
 $canvas->filledellipse(250, 150, 20, 20, $img->allocateColor(250, 243, 22));
 $canvas->filledellipse(50, 50, 20, 20, $img->allocateColor(22, 250, 113));
 $canvas->filledellipse(50, 150, 20, 20, $img->allocateColor(255, 0, 0));
 $canvas->filledellipse(275, 100, 20, 20, $img->allocateColor(255, 150, 80));
 
 //adding the '2016' text on the bottom of the image with the font Arial and size 16
 $canvas->useFont('C:\wamp\www\demo\fonts\arial.ttf', 16, $img->allocateColor(176, 253, 248));
 $canvas->writeText('middle-10', 'bottom-3', '2016');
 
 //saving the new image
 $img->saveToFile('new3.jpg');
?> 

 

The output image after the adding circles and text is:

 

 

Rotating and mirroring an image

To rotate, mirroring or flipping an image you need to use the below methods:

 

WideImage_Image rotate( int $angle, [int $bgColor = null], [bool $ignoreTransparent = true]) - Rotate the image for angle $angle clockwise.
WideImage_Image mirror( ) - Returns a mirrored copy of the image
WideImage_Image flip( ) - Returns a flipped (mirrored over horizontal line) copy of the image 

 

Next listing shows the effect of the above three methods:

 

<?php

include "C:\wamp\www\lib\WideImage.php";

//loading the initial image
$image = WideImage::load("test.jpg");

//rotating an image
$new4=$image->rotate(15);
$new4->saveToFile("new4.jpg");

//flip an image
$new6=$image->flip();
$new6->saveToFile("new6.jpg");

//mirror an image
$new5=$new4->mirror();
$new5->saveToFile("new5.jpg");
?> 

 

The output of the above listing is:

 

 

Round corners

To round corners of an image you should use the roundCorners() method:

 

WideImage_Image roundCorners( int $radius, [int $color = null], [int $smoothness = 2], [int $corners = 255]) - Returns an image with round corners 

 

Next listing round different kind of corners: diagonal corners, right corners, deep and colored corners, of the test.jpg initial image, save the new image to file and also output them:

 

<?php

include "C:\wamp\www\lib\WideImage.php";

//loading the initial image
$rounded = WideImage::load("test.jpg");

//rounding corners of different sizes, colors and positions
$new = $rounded->roundCorners(100, $rounded->allocateColor(255, 255, 0), 10);
//$new = $rounded->roundCorners(150, $rounded->allocateColor(255, 255, 255), 10);
//$new = $rounded->roundCorners(30, $rounded->allocateColor(255, 0, 0), 10);
//$right_corners = $image->roundCorners(50, $image->allocateColor(255, 255, 255), 2, WideImage::SIDE_RIGHT);
//$right_corners->saveToFile("new.jpg");
//$diagonal_corners = $image->roundCorners(40, $image->allocateColor(255, 0, 0), 2, WideImage::SIDE_TOP_LEFT + WideImage::SIDE_BOTTOM_RIGHT);
//$ diagonal_corners ->saveToFile("new.jpg");

$new->saveToFile("new.jpg");

$new->output('jpg', 100);
?> 

 

The output of the above listing is:

 

 

Adding different noises over an image

Another operation that can be applied over an image, using WideImage library is to adding noise. You can apply three kind of noises: salt&pepper, mono and color, by setting as argument into the addNoise() method, which has the following syntax:

 

WideImage_Image addNoise( int $amount, string $type)- Adds noise to the image: the $amount argument sets the number of noise pixels to add, and the string $type represents one of the three type of noises: salt&pepper, mono and color.

 

In the next example we will exemplify those three noises, using different noise pixels:

 

<?php

include "C:\wamp\www\lib\WideImage.php";

  //loading the initial image
$image = WideImage::load("test.jpg");

//adding noises on the loaded image
$new=$image->addNoise(240, 'salt&pepper');
$new=$image->addNoise(350, 'mono');
$new=$image->addNoise(600,'color');

$new->saveToFile("new6.jpg");
?> 

 

The output of the listing is:

 

 

Applying filters on an image

The last operations over the images, shown in this article, is to apply two filters: Negative and Grayscale. To apply them we need to use the asNegative() and asGrayscale() methods. The next examples apply the two filters on our initial image, test.jpg, using the above two functions:

 

<?php

include "C:\wamp\www\lib\WideImage.php";

//loading the initial image
$image = WideImage::load("test.jpg");

//applying the NEGATIVE filter over the image
$new=$image->asNegative();
$new->saveToFile("new7.jpg");

//applying the GRAYSCALE filter over the image
$new=$image->asGrayscale();
$new->saveToFile("new8.jpg");
?> 

 

The output of the listing is:

 


Applying the NEGATIVE and GRAYSCALE filter to the test.jpg image

 

Conclusion

During this article you have learned how to use the most common types of operations that can be applied over images using the WideImage library.

 

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Octavia Andreea Anghel

Comment:



Comment:

(Maximum characters: 1200). You have characters left.