|
Resource Center
: PHP
: <PHP Imaging support & Image manipulation - An overview of possibilities>
|
|
PHP Imaging support & Image manipulation - An overview of possibilities
Posted by: Floresense Team
Colors: Like in any drawing application one will need to create a color object or pen before drawing in a particular color, PHP also has the function imagecolorallocate() to create a color object.
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// hexadecimal way
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00); where $im is the image resource on which we are going to use the color.
Color Alpha / transparency: We use the method imagecolorallocatealpha() to set a color as transparent, or to set the opacity of a layer of the image.
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75); The last parameter of imagecolorallocatealpha '75' mentions the opacity level of the objects drawn with $yellow. For example a circle drawn over a photograph with $yellow as color, would show the photo through the circle.
Sample code that generated above image: (use any image file in place of celebration1.jpg) <?php
//create image from jpg file
$im = imagecreatefromjpeg("Celebration1.jpg");
if ($im)
{
// allocate colors with alpha values
$yellow = imagecolorallocatealpha($im, 255, 255, 0, 55);
// drawing circle over image
$yellow_x = 60; $yellow_y = 110; $radius = 100;
imagefilledellipse($im, $yellow_x, $yellow_y, $radius, $radius, $yellow);
// don't forget to output a correct header!
header('Content-type: image/jpeg');
// and finally, output the result
imagejpeg($im);
imagedestroy($im);
}
?>In this sample we use the ellipse function with same width and height to draw a circle. Since this is possible, php doesnt have a separate method for drawing circles as of version v.3.x (new versions might have though they might internally only use the ellipse drawing code.. it doesnt matter).
Drawing:
In the above sample we used a drawing function, imagefilledellipse. Many more drawing methods are there. imagearc imageellipse imageline imagepolygon imagerectangle, and so on..
For documentation on trying other image functions in php check http://www.php.net/manual/en/ref.image.php, or http://www.php.net/manual/en/ for the full english php documentation.
Image resizing: Image resizing is done with help of imagecopyresized() method.
This method actually copies an image from a source to a destination object.
1: We load the source image from a file using imagecreatefrom... methods. 2: We create a new blank small size image using imagecreatetruecolor().
We use this instead of imagecreate(), because we lose the colors if we created destination image with imagecreate(). <?php
//load image
$src_im = imagecreatefromjpeg("reef.jpg");
//get size
$src_width = imagesx($src_im);
$src_height = imagesy($src_im);
//fix resize factor... to 30% of image size both width and height.
$resize_factor = .3;
$new_width = $src_width * $resize_factor;
$new_height = $src_height * $resize_factor;
//since we apply the same resize factor for both widht and height,
// it ensures proportionate resize
//create new blank image for holding resized image
$thumb_im = imagecreatetruecolor($new_width,$new_height);
//we use imagecreatetruecolor instead of imagecreate to preserve colors
//during resize.
// Resize
imagecopyresized($thumb_im, $src_im, 0, 0, 0, 0,
$new_width, $new_height,
$src_width, $src_height);
header('Content-type: image/jpeg');
imagejpeg($thumb_im);
imagedestroy($im);
?>
Interesting thing is, using the methods imagecopy and imagecopyresized we can resize even "any" rectangular portion of an image, and it need not be always the full image.
Advertisement
|