Resize a Photo >>

Today we will implement hows to resize photo into a particular width and height (or keeping aspect ratio)

See the live example from:

You need to install php-gd library to do this. To functions, imagecreatetruecolor and *imagecopyresampled *will do the resizing.

Those who have not seen the Online Photo Editing tool yet, have a look at:

Here is the function:

function Resize($image,$newwidth,$newheight=0) { $oldwidth = imagesx($image); $oldheight= imagesy($image); if($newheight==0) // if the height is not specified //....calculate the relative height $newheight= $newwidth * $oldheight / $oldwidth ; $newimage= imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($newimage, $image, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight); return $newimage; }

Here the $image is the object of the image which is created from the function* imagecreatefromgif*($pathtophoto), if the image is in GIF format.

Other functions are imagecreatefromjpeg and imagecreatefrompng for JPEG and PNG respectively .

Download Full source code for this example from:

In the previous part we implemented browse photos area.

In that for browsing picture files, we used full size image, instead of thumbnails. Here new example , in which browse photos is implemented with image thumbnails using resizing (150 px)

The difference is , instead of calling img tag’s src to the orginal image path, called through resize.php?img=pathtoimage , passed the image path as parameter to resize function.

See live example:

Take the html and see the difference from old browse implementation:

Which is fast ??

Download the full source code for browsing with image thumbnail from:

Keep reading for the coming series,part 4 , Orkut Like Photo Cropping >>

Subscribe to this website feed now:


Sajith M.R