Wednesday, August 20, 2008

Uploading image and creating thumnails

function UploadImage($temp_name, $name, $uploaddir)
{
$next_location = "../../uploaded_images/model/middle/";
if(is_uploaded_file($temp_name))
{
@move_uploaded_file($temp_name,$uploaddir.$name);
if(file_exists($uploaddir.$name))
$this->CreateThumb($name,$uploaddir,$next_location,100,100);
}
}

function CreateThumb($name1, $src_location, $dest_location, $new_w,$new_h)
{
$name = $src_location.$name1;
$filename = $dest_location.$name1;

$ext = explode(".",$name);
$count = count($ext);
$ext = $ext[$count-1];


switch($ext)
{
case "jpeg":
{
$src_img=imagecreatefromjpeg($name);
break;
}

case "jpg":
{
$src_img=imagecreatefromjpeg($name);
break;
}

case "gif":
{
$src_img=imagecreatefromgif($name);
break;
}
case "png":
{
$src_img=imagecreatefrompng($name);
break;
}
}

$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$ratio = ($old_x < $old_y) ? $new_h/$old_y : $new_w/$old_x;

$thumb_w = $old_x * $ratio;
$thumb_h = $old_y * $ratio;
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

switch($ext)
{
case "jpeg":
{
imagejpeg($dst_img,$filename);
break;
}

case "jpg":
{
imagejpeg($dst_img,$filename);
break;
}

case "gif":
{
imagegif($dst_img,$filename);
break;
}
case "png":
{
imagepng($dst_img,$filename);
break;
}
}

imagedestroy($dst_img);
imagedestroy($src_img);
}


or you can use this function

function MakeIcon($dirname, $filename, $filetype,$maxw,$maxh){
list($width, $height) = getimagesize($dirname.$filename);
$percent = $width/$height;
$x_percent=$maxh/$height;
$y_percent=$maxw/$width;

if($width<=$maxw && $height<=$maxh){
$newheight=$maxh;
$newwidth=$maxw;
}else if(($x_percent*$height)<$maxh){
$newheight=ceil($x_percent*$height);
$newwidth=$maxw;
}else{
$newwidth=ceil($y_percent*$width);
$newheight=$maxh;
}
$thumb = imagecreatetruecolor($newwidth, $newheight);
$iconame = "$filename";
switch($filetype){
case "image/jpeg":
$source = imagecreatefromjpeg($dirname.$filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
imagejpeg($thumb,$dirname.$iconame);
break;
case "image/gif":
$source = imagecreatefromgif($dirname.$filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
imagegif($thumb,$dirname.$iconame);
break;
case "image/pjpeg":
$source = imagecreatefromjpeg($dirname.$filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
imagejpeg($thumb,$dirname.$iconame);
break;
case "image/png":
$source = imagecreatefrompng($dirname.$filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
imagepng($thumb,$dirname.$iconame);
break;
}
//ini_set("memory_limit", "32MB");
return $iconame;
}

setting parameters now

$uploaddir = "./uploaded_images/event/";


$imgObj->UploadImage($temp_name, $name, $uploaddir);
$dest_location = "./uploaded_images/thumbs/";
if(file_exists($uploaddir.$name))
$imgObj->CreateThumb($name,$uploaddir,$dest_location,100,100);


and

$imgObj->UploadImage($temp_name, $name, $uploaddir,$_FILES['pic']['type'][$i]);
$dest_location = "./uploaded_images/thumbs/";
if(file_exists($uploaddir.$name))
$imgObj->CreateThumb($name,$uploaddir,$dest_location,150,100);

No comments: