ANIL KUMAR PANIGRAHI 's Blog

Thumbnail image creation using php code

Posted by: Anil Kumar Panigrahi in: ● June 3, 2010

In this post explains how to create thumbnail image using php code.

function make_thumb($img_name,$filename,$new_w,$new_h)
{
//get image extension.
$ext=getExtension($img_name);
//creates the new image using the appropriate function from gd library
if(!strcmp(“jpg”,$ext) || !strcmp(“JPG”,$ext) || !strcmp(“jpeg”,$ext) || !strcmp(“JPEG”,$ext))
$src_img=imagecreatefromjpeg($img_name);

if(!strcmp(“gif”,$ext) || !strcmp(“GIF”,$ext))
$src_img=imagecreatefromgif($img_name);

if(!strcmp(“png”,$ext) || !strcmp(“PNG”,$ext))
$src_img=imagecreatefrompng($img_name);

//gets the dimmensions of the image
$old_x=imagesx($src_img);
$old_y=imagesy($src_img);

$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;

if($ratio1>$ratio2) {
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}
else {
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
// we create a new image with the new dimmensions
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

// resize the big image to the new created one
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

// output the created image to the file. Now we will have the thumbnail into the file named by $filename
if(!strcmp(“png”,$ext))
imagepng($dst_img,$filename);
if(!strcmp(“gif”,$ext))
imagegif($dst_img,$filename);
else
imagejpeg($dst_img,$filename);

//destroys source and destination images.
imagedestroy($dst_img);
imagedestroy($src_img);
}

?>

To find out the extension of image with following function:

function getExtension($str) {
$i = strrpos($str,”.”);
if (!$i) { return “”; }
$l = strlen($str) – $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

Call the particular thumbnail function like :

make_thumb(‘original.jpg’,'thumb.jpg’,150,250);
echo “Thumbnail created successfully…!!!”;

Share on Twitter

3 Comments to "Thumbnail image creation using php code"

1 | iPad Demonstration / Review Thing Part 1 | Apple On The Longtail

3 de June de 2010 to ● 6:58 pm

[...] Thumbnail image creation using php code « ANIL KUMAR PANIGRAHI 's Blog [...]

2 | Kicker

10 de June de 2010 to ● 11:04 pm

Greatings, ЎGracias por el artнculo. Cada vez que quieres leer.
Gracias

Kicker

Write Comment

About Me

  Anil Kumar Panigrahi
  Software Engineer (PHP)
  Blogger & Founder of Anil Labs
  Baruva, India

You can share with me . . .

My Own Contributions

1) First contribution for codeigniter framework ( multi language support using google translate API ). we can get files from http://github.com/nyros/codeigniter_multilanguage


2) Audio and Video Streaming using FFMpeg and PHP -complete doc file : http://www.docstoc.com/docs/8300349/Audio-and-Video-Streaming

Twitter Updates

Follow me @anil2u
  • http://www.google.co.uk/ logo is changing with mouse move . . . - posted on 07/09/2010 10:57:33
  • True enemies are real friends . . . - posted on 06/09/2010 10:15:29
  • Just watched nice movie " Madrasapattinam ". - posted on 05/09/2010 16:20:48
  • We will always be thankful to our teacher for all the hard work and efforts they have put in, for educating us.- Happy teachers Day . . . :) - posted on 05/09/2010 05:39:13
  • Working on shipping cost functionality (UPS and USPS) for oscommerce shopping cart - posted on 01/09/2010 09:40:03