Recent Posts

This Post Has Been Viewed 175 Times.

Create thumbnail using php and gd library

Posted by admin | Posted in PHP, scripts | Posted on 13-07-2009

0

The following PHP code will create thumbnail images on the fly and since it uses the PHP GD2 library, you will need an installation of PHP with at least GD 2.0.1 enabled.. This is a very simple script and very easy to customise.

<?php
header 
(“Content-type: image/jpeg”);
$image $_GET['thumimg'];

if(!isset($w) && !isset($h)){
 
$w 110//default width if $w is not set
 
$h 135//default height if $h is not set
}

$x = @getimagesize($image);// get image size
$sw $x[0];// width
$sh $x[1];// height
$im = @ImageCreateFromJPEG ($image) or // Read JPEG Image
$im false// If image is not JPEG


if (!$im)
 
readfile($image);// return the actual message if error occurs.
else {
// Create the resized image destination
   
$thumb = @ImageCreateTrueColor ($w$h);
// Copy from image source, resize it, and paste to image destination
   
@ImageCopyResampled ($thumb$im0000$w$h$sw$sh);
// Output resized image
   
@ImageJPEG ($thumb);
}
?>

save the above code as thumbimg.php.

How to use:
<–without specifying the width and height–>
<img src=”http://www.mysite.com/thumbimg.php?thumimg=example.jpg” />

<–with height and width–>
<img src=”http://www.mysitee.com/thumbimg.php?thumimg=example.jpg&w=100&h=100″ />

VN:F [1.5.7_846]
Rating: 10.0/10 (1 vote cast)
VN:F [1.5.7_846]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • Xerpi

Related posts:

  1. Water mark images using PHP 5 and GD Library

Write a comment

Spam Protected