PHP Programming/Images
Introduction
editPHP can create and modify dynamically some images, for example with the GD Graphics Library, included by default since PHP 3.0.
A new image creation must follow with a few steps:
- Memory loading of a new or an existing image.
- Optional colors loading to add.
- Optional components modifications (lines creation, points, fillings, texts addition...).
- Image restitution by posting its type into the header.
- Memory release.
Create a new image
editTo create an image ex nihilo, use the function:
imagecreatetruecolor($height, $width)
which creates in memory a new image, its height and width are defined in pixel, and restitutes a reference to the new image.
There is also another function for this, but it's not recommended as its colors amplitude is worst[1]:
imagecreate($height, $width)
To load in memory an image which had been saved on the disk:
imagecreatefrom<type>($path)
Example:
$img = imagecreatefrompng('image.png');
Other function:
imagecreatefromstring($text)
which creates an image from its text format, specified in parameter.
If an error occurs, theses functions return false
.
Work with the colors
editTo allocate a color, the RGB parameters must be defined:
$color = imagecolorallocate($image,$r,$g,$b)
To define a transparency into a PNG:
imagecolortransparent($image,$color)
where $color
is the result of imagecolorallocate
.
It's also possible possible to determine the transparency, between 0 and 127 (which represents the total transparency) with the function:
imagecolorallocatealpha($image,$r,$g,$b,$transparency)
Remark: the first allocated color defines the whole image background.
Once the image is created and colored, it becomes possible to apply the following operations to it:
- Draw some pixels (eg: create lines).
- Work on the existing pixels by designating zones.
Draw shapes
editTo draw a pixel, we use its coordinates (x, y below):
imagesetpixel(image, x, y, color)
To draw a line between two points:
imageline(image, x1, y1, x2, y2, color)
To create a rectangle from its diagonal:
imagerectangle(image, x1, y1, x2, y2, color)
To represent an ellipse from its center, its height and width:
imageellipse(image, x, y, h, l, color)
or by precising its arc by its angles in gradient (clockwise numbered):
imagearc(image, x, y, h, l, angle1, angle2, color)
Rework the existing pixels
editThe most used functions to rework images like photos, is certainly imagecopyresized
, which allows to copy a rectangular zone to paste it in another image[2]. Example:
imagecopyresized(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h);
where:
src_image
is the source image;dst_image
is the destination image;dst_x, dst_y
are the coordinates ofdst_image
;src_x, src_y
are the coordinates ofsrc_image
, beginning at the top left;dst_w, dst_h, src_w, src_h
are the source and destination rectangles widths and heights.
It becomes understandable after, that if dst_w
is equal to src_w
, and dst_h
to src_h
, the image rectangular portion will remain the same size. On the contrary we lengthen or enlarge.
The function imagecopyresampled
receives the same parameters as imagecopyresized
, but in case of resizing, the quality est improved.
Then it exists the function imagefilter
which allow numerous effects, such as grayscale, relief, or recoloration[3].
Print the output
editThe obtained image format ("png", "jpeg" or "gif") can be specified by the function called header
, via the content-type (by default text/html) with:
header("Content-type: image/<type>");
To visualize the image after, place it in parameter in a function depending on its type: imagepng
, imagejpeg
or imagegif
.
Then, liberate the memory with imagedestroy($image)
. This stage is optional but strongly recommended for huge images.
Example
editThe following code displays in the navigator, a 50 pixels red square into a 100 black one.
$image = imagecreatetruecolor(100, 100);
$color = imagecolorallocate($image, 255, 0, 0);
imagefilledrectangle($image,0,0,50,50,$color);
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
References
edit