Monday, July 21, 2008

Image re sizing on the fly - PHP

Hi,
you can re size your uploading image using php script given below:


Class ImageResizeUpload {

function ImageResizeUpload($upload_folder,$thumb_max_height,$thumb_max_width,$quality,$file_max_size,$allowed_extensions) {
$this->upload_path = $upload_folder;
$this->thumb_max_height = $thumb_max_height;
$this->thumb_max_width = $thumb_max_width;
$this->quality = $quality;
$this->file_max_size = $file_max_size;
$this->allowed_extensions = $allowed_extensions;
$this->rand_image_tag = rand(10000,99999);
}

function fileExtension($filename) {
return end(explode(".",$filename));
}

function isValidFormat() {
foreach(explode("|",$this->allowed_extensions) as $value) {
if($this->fileExtension()==$value) { //We have a match, it is valid!
$valid_extension="true";
break;
}
}
if(isset($valid_extension) && $valid_extension=="true") { return true; } else { return false; }
}

function isValidSize($tmp_filename) {
if(filesize($this->tmp_filename)<=($this->file_max_size*1024)) {
return true;
} else {
return false;
}
}

function fileName($filename,$type, $id) {
if($type==0) { return $id."_".$filename; } else { return $id."_".$filename; }
}

function calculateThumbSize($tmp_filename) {
$image_size = getimagesize($tmp_filename);
//check to see if we even need to resize it
if($image_size[0] > $this->thumb_max_width || $image_size[0] > $this->thumb_max_width) {
//We do, so lets do it!
if($image_size[0] > $image_size[1]) {
//width is greater than height so make width our max width and height proportional
$width = $this->thumb_max_width;
$height = ($image_size[1] * $width) / $image_size[0];
} else {
//height is greater than width
$height = $this->thumb_max_height;
$width = ($image_size[0] * $height) / $image_size[1];
}
} else {
//No need to resize, just return same h/w
$width = $image_size[0];
$height = $image_size[1];
}
return array("width"=>$width,"height"=>$height);
}

function resizeCopyImage($filename,$tmp_filename, $id) {
//echo $id;
$thumb_size = $this->calculateThumbSize($tmp_filename);
//switch ($this->fileExtension($filename)) {
// case "jpg" || "jpeg":
$file_extension = $this->fileExtension($filename);
$file_extension = strtolower($file_extension);
if($file_extension == 'jpg')
$src_img = imagecreatefromjpeg($tmp_filename);
elseif($file_extension == 'gif')
$src_img = imagecreatefromgif($tmp_filename);
elseif($file_extension == 'png')
$src_img = imagecreatefrompng($tmp_filename);
elseif($file_extension == 'bmp')
$src_img = imagecreatefromwbmp($tmp_filename);

$dest_img = imagecreatetruecolor($thumb_size['width'],$thumb_size['height']);
imagecopyresampled($dest_img,$src_img,0,0,0,0,$thumb_size['width'],$thumb_size['height'],imagesx($src_img),imagesy($src_img));
$new_thumb_filename = $this->upload_path."/".$this->fileName($filename,1, $id);
@chmod($new_thumb_filename,0777);

if($file_extension == 'jpg')
imagejpeg($dest_img,$new_thumb_filename,$this->quality);
elseif($file_extension == 'gif')
imagegif($dest_img,$new_thumb_filename,$this->quality);
elseif($file_extension == 'png')
imagepng($dest_img,$new_thumb_filename,$this->quality);
elseif($file_extension == 'bmp')
imageWBMP($dest_img,$new_thumb_filename,$this->quality);

return true;
}

function moveUploadedFile($filename,$tmp_filename) {
$new_filename = $this->upload_path."\\".$this->fileName($filename,0);
move_uploaded_file($tmp_filename,$new_filename);
// @chmod($new_filename,0755);
return true;
}

}


$path="../photo";
$image=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$rand=rand(1, 1000000);
$upload_image = new ImageResizeUpload($path,100,100,100,800,"jpg|jpeg|png");
$upload_image->resizeCopyImage($image,$tmp,$rand);
$image= $rand. '_' . $image;

I think this code will help you.

1 comment:

Karthik said...
This comment has been removed by the author.