Showing posts with label gd library. Show all posts
Showing posts with label gd library. Show all posts

Saturday, April 2, 2011

Rotate Image - PHP (GD Library Elaborated)

Hi,

GD library is such a strong architecture, that you can do anything using it. If you want to rotate a simple jpeg file using PHP, this is the code that is needed:


<?php
$im=imagecreatefromjpeg("img.jpg");
$white=imagecolorallocate($im,255,255,255);
$im=imagerotate($im,20,$white);

header("content-type: image/jpeg");
imagejpeg($im);

The image transformation from original to rotated image is as follows respectively:                                      
       


Explanation:

Thursday, February 24, 2011

Watermark on image


Hi,

With GD library in PHP, you can easily add a watermark to any image. The idea of this is to merge two image on top of one another. This possibly is the one of the easiest things that can be achieved by GD library. But watermark literally means a small semi transparent image will be placed on another one. For this you need to have a semi transparent image. Which will preferably be a PNG file.

Use the following code to achieve watermarking on image:

$stamp = imagecreatefrompng(PATH_OF_THE_SEMITRANSPARENT_IMAGE);
$im = imagecreatefromjpeg(PATH_OF_THE_ACTUAL_IMAGE);

Wednesday, February 23, 2011

Convert an image to gray scale

PHP provides a very strong support for image manipulation. You can convert any image into its grey scale equivalent easily. The only thing you need to know apart from the given code is how to generate a black and white version of a particular color.

What I mean to say is, take any color for example. May be red. We have to know the rgb equivalent of it. In our case the rgb equivalent of red is rgb(255,0,0). Now the grey scale equivalent of that is as follows:

Grey Scale:
Red = (255 + 0 + 0) /3
Green = (255 + 0 + 0) /3
Blue = (255 + 0 + 0) /3
that is
  rgb(85,85,85)= grey scale of red

Anyways, here's the code ;)