Saturday, March 12, 2011


  Image Resize

How to resize image using php?

We can resize image using PHP script without using any designing soft ware

Demo View
   Original Image



   Resize Image



File Name : resize.php
<?php 
    //image file type  
header("Content-type: image/jpeg");
// by http://phpupul.blogspot.com
//
// proportional on-the-fly thumb generator from JPG images
//
// usage example: 
// <img src= "resize.php?src=images/pic.jpg&wmax=150&hmax=100" />
//
// parameters:  src = source image
//              wmax = max width
//              hmax = max height
// note: if source image is smaller than desired thumbnail, it will not be resized!


$src  = $_REQUEST['src'];
$wmax = $_REQUEST['wmax'];
$hmax    = $_REQUEST['hmax'];
$bgcol = '#FF0000';
$quality  =90;

switch(exif_imagetype($src)){
 case '1':
  $source = imagecreatefromgif($src);
 break;
 case '2':
  $source = imagecreatefromjpeg($src);
 break;
 case '3':
  $source = imagecreatefrompng($src);
 break;
 default:
  $source = imagecreatefromjpeg($src);
 break;
}

$orig_w = imagesx($source);
$orig_h = imagesy($source);

if ($orig_w>$wmax || $orig_h>$hmax)
{
$thumb_w=$wmax;
$thumb_h=$hmax;
if ($thumb_w/$orig_w*$orig_h>$thumb_h) 
$thumb_w=round($thumb_h*$orig_w/$orig_h); 
else 
$thumb_h=round($thumb_w*$orig_h/$orig_w);
} else
{
$thumb_w=$orig_w;
$thumb_h=$orig_h;
}
if (!@$bgcol)
{
$thumb=imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($thumb,$source,
                  0,0,0,0,$thumb_w,$thumb_h,$orig_w,$orig_h);
}
else
{
$thumb=imagecreatetruecolor($wmax,$hmax);
imagefilledrectangle($thumb,0,0,$wmax-1,$hmax-1,intval($bgcol,16));
imagecopyresampled($thumb,$source,
                  round(($wmax-$thumb_w)/2),round(($hmax-$thumb_h)/2),
                  0,0,$thumb_w,$thumb_h,$orig_w,$orig_h);
}
if (!@$quality) $quality=90;
imagejpeg($thumb,"",$quality);
imagedestroy($thumb);
?>
 

No comments:

Post a Comment