Showing posts with label GD. Show all posts

PHP Image Class

by Lasantha Samarakoon on Saturday, February 19, 2011

Today I am going to give you something that I have created using PHP. It is a simple PHP class that can be used to manipulate images easily in an object oriented way. I created this by using basic functions in PHP GD library.

Download

You can download the ZIP archive containing PHP classes by clicking here.

License

I would like to distribute this using GNU/GPL license, so you can add more functionality into it and enhance my basic product.

Functions

This class allows you to manipulate JPEG, GIF and PNG files. It allows image resizing, cropping, merging, rendering, saving and applying filters.

Documentation

Open image

new Image(path-to-file)

Example:

$img = new Image('/home/user/foo.jpg');

You can specify the relative or absolute path as the parameter. Note that this method may throw FileNotFoundException, whenever the method fails to locate the file on the target location.

Resize image

resize(width, height, [unit = ‘px’, [keep-aspect-ratio = false]])

Example:

$img->resize(800, 600, 'px', true);

Resize method allows you to resize image file. Unit can be either ‘px’ or ‘%’.

Crop image

crop(x, y, width, height)

Example:

$img->crop(100, 50, 600, 500);

Crop method allows cropping the image. It takes 4 parameters x coordinate, y coordinate, width and the height.

Merge images

merge(image-object, [x = 0, [y = 0, [opacity = 100]]])

Example:

$anotherImg = new Image('/home/user/bar.jpg');
$img->merge($anotherImg, 100, 100, 75);

Merge method can be used to merge to image objects. That means this method results merger of 2 images. Again it takes 4 parameters; Image object, x coordinate, y coordinate and opacity value. Opacity ranges from 0 to 100, which 0 means transparent and 100 means full opaque.

Save image

save(destination, [file-type = ‘jpg’])

Example:

$img->save('/home/user/foo_edit.png', 'png');

This class can be used to save the resulting image on a storage device. This requires a parameter ‘file-type’ which takes jpg, gif or png as values.

Render image

render([file-type = ‘jpg’])

Example:

$img->render('gif');

Render method allows rendering resulting image on the browser without saving it on a storage device. This requires a parameter ‘file-type’ which takes jpg, gif or png as values.

Filters

Available filters to apply on images are as follows.

  • negative()
  • grayscale()
  • detectEdges()
  • emboss()
  • gaussianBlur()
  • selectiveBlur()
  • brightness(value)
  • contrast(value)
  • colorize(r, g, b, [alpha = 0])
  • sketchy()
  • smooth(value)
  • sephia()

Dynamic progress bar with PHP GD

by Lasantha Samarakoon on Wednesday, February 24, 2010

Today I’m going to show you something regarding GD library of PHP. The GD library is the main library using for Graphics in PHP. By using functions of this library, it is possible to manipulate images with PHP.

In this post, I will show you how to create a dynamic progress bar with PHP. In here the “dynamic” means you can parse 2 values, (i.e. maximum value and the current value) and the PHP script will calculate and render the progress bar dynamically on the screen. The output will come in the format of PNG image file. Hence it is very easy to use in you HTML file.

That’s all about the explanation. So let’s go for codes…

<?php
// filename: progressbar.php
// author  : lasantha samarakoon

// set the type of data (Content-Type) to PNG image
header("Content-Type: image/png");

// extract GET global array
extract($_GET);

// set defaults
if(! isset($max)) $max = 100;
if(! isset($val)) $val = 100;

// this method prepare blank true color image with given width and height
$im = imagecreatetruecolor(400, 20);

// set background color (light-blue)
$c_bg = imagecolorallocate($im, 222, 236, 247);
// set foreground color (dark-blue)
$c_fg = imagecolorallocate($im, 27, 120, 179);

// calculate the width of bar indicator
$val_w = round(($val * 397) / $max);

// create a rectangle for background and append to the image
imagefilledrectangle($im, 0, 0, 400, 20, $c_bg);
// create a rectangle for the indicator and appent to the image
imagefilledrectangle($im, 2, 2, $val_w, 17, $c_fg);

// render the image as a PNG
imagepng($im);

// finally destroy image resources
imagedestroy($im);
?>

OK… that is the PHP script. Now it’s time to display it in the browser. First create a HTML file to display the image. In the IMG tag of the HTML file, type the location of your PHP script and specify 2 values, maximum and current as follows.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Progress Bar</title>
</head>

<body>
 
    <img src="progressbar.php?max=100&val=70" />
 
</body>
</html>

That’s all, and you can see the output…