FruitNotes beta
Your All-in-One Online Notebook
FruitNotes Blogs | Home  
sample code to resize images in php using Image_Transform pear package
Last updated at (Fri Jan 04 2008 19:03:43)
Posted by: Nitin Gupta
0%




1) first thing would be to install pear package
for image transform
2) look into this package download and install


3) look into the example code towards the end, it uses this lib to do the image transformation

just change the image sizes and play with it.
e.g change 80, 80 to say 100, 120 in following
transformImg($path, $directory.'/new/'.$file, 80, 80);

to run it use on command prompt or call it via some url
php imgTrns.php

4) Put some images in $directory and you will see new images of modified width and height

 

Example code : save file as imgTrns.php

================


include_once 'Image/Transform.php';

$directory = "../images/gifts";
$directory_list = opendir($directory);

// and scan through the items inside
while (FALSE !== ($file = readdir($directory_list)))
{
// if the filepointer is not the current directory
// or the parent directory
if($file != '.' && $file != '..')
{
// we build the new path to scan
$path = $directory.'/'.$file;
if(stripos($file, 'icon')!==false)
{
transformImg($path, $directory.'/new/'.$file, 80, 80);
}
else
{
transformImg($path, $directory.'/new/'.$file, 400, 400);
}
}
}

function transformImg($src, $dest, $wt, $ht)
{
  $image = Image_Transform::factory('GD');
  if (PEAR::isError($image))
  {
   echo "Upload error1: " . $image->getMessage()."<br/>".$image->getDebugInfo();
   exit;
  }

  $imgFile = $image->load($src);
  if (PEAR::isError($imgFile))
  {
   echo "Upload error2: " . $imgFile->getMessage()."<br/>".$imgFile->getDebugInfo();
  }
  else
  {
    $image->fit($wt, $ht);
    $image->save($dest);
  }
}
?>

===========


Rate this blog

   Report Abuse


Comments


Leave your comment(s) below:
To start Your own Blog




Other Blogs
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
2007 FruitNotes.com - All Rights Reserved.