Below is the script to resize an image. Hope it will help.
If you have questions, you can email me here alreymark
Instructions:
1. Name the script whatever you want be sure it is a php file.
2. based on the script below I have 3 directory naming thumbs1, thumbs2 and thumbs3. This directories will be the location of the resized image based on category. ($cat=1, 2, 3)
3. Choose an image, it should be in jpeg format and place it together with the script.
for example if you are in the root of your site with a directory "/test/", you should come up with this location.
http://your-site.com/test/
http://your-site.com/test/resizer.php
http://your-site.com/test/testing.jpeg
http://your-site.com/test/thumbs1/
http://your-site.com/test/thumbs2/
http://your-site.com/test/thumbs3/
4. This should done the work. :)
<?php
function retrieveFolder($cat){
switch($cat){
case 1: $myfolder="thumbs1";break;
case 2: $myfolder="thumbs2";break;
case 3: $myfolder="thumbs3";break;
}//end switch
return $myfolder;
}//end function
function retrieveDimension($cat,$w,$h){
$d=array();
switch($cat)
{
case 1://First thumb size
if($w>$h){
$d[0] = 208 ;
$d[1] = $h * 208 /$w;
}
if($h>$w){
$d[0] = $w * 230/$h;
$d[1] = 230;
}
break;
case 2://second thumb size
if($w>$h){
$d[0] = 96 ;
$d[1] = $h * 96 /$w;
}
if($h>$w){
$d[0] = $w *106/$h;
$d[1] = 106;
}
break;
case 3://3rd thumb size
if($w>$h){
$d[0] = 120;
$d[1] = $h * 120 /$w;
}
if($h>$w){
$d[0] = $w *127/$h;
$d[1] = 127;
}
break;
}
return $d;
}//end function
class ResizeImage{
function ResizeImage($filename,$image_cat){
$myfolder=retrieveFolder($image_cat);
if(getimagesize($filename)){
$final_filename=$myfolder."/".basename($filename);
echo "base ".$filename." final ".$final_filename."
";
list($width, $height) = getimagesize($filename);
$dimension=retrieveDimension($image_cat,$width,$height);
$new_width=$dimension[0];
$new_height=$dimension[1];
$image_p =imagecreatetruecolor($new_width, $new_height);
$image =imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p,$final_filename,72);
imagedestroy($image_p);
imagedestroy($image);
}//if(file_exists($filename)
$this->rawfile=$filename;
$this->picfile=$final_filename;
}//end Constructor
function getFile(){return $this->picfile;}
function destroy(){ unset($this);}
}//end Class
$image = new ResizeImage("testing.jpg", 1);
$image->destroy();
$image = new ResizeImage("testing.jpg", 2);
$image->destroy();
$image = new ResizeImage("testing.jpg", 3);
$image->destroy();
?>
1 comment:
Great man! it works well.
How about to resize image in gif and png
do you have one?..
Please post it.
Thanks.
Post a Comment