Here is my function for this:
PHP Code:
function Create_ThumbNail($ImageType, $ImagePath, $rW, $rH, $img, $do="thumbnail"){
$full_picmain = $ImagePath . "/" . $img;
if($do != "thumbnail")
$tsrc = "$ImagePath/$img";
else
$tsrc = "$ImagePath/thumbnails/$img";
////////////// Starting of GIF thumb nail creation///////////
if($ImageType == "image/gif"){
$im = imagecreatefromgif($full_picmain);
$width = imagesx($im); // Original picture width is stored
$height = imagesy($im); // Original picture height is stored
if($width > $rW){$n_width = $rW;}
else{$n_width = $width;}
if($height > $rH){$n_height = $rH;}
else{$n_height = $height;}
$newimage = imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width,$height);
if(function_exists("imagegif")) {
header("Content-type: image/gif");
imagegif($newimage, $tsrc);
}
elseif(function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagejpeg($newimage,$tsrc);
}
chmod("$tsrc",0777);
}
////// starting of JPG thumb nail creation//////////
if($ImageType == "image/pjpeg" || $ImageType == "image/jpeg" || $ImageType == "image/jpg"){
$im = imagecreatefromjpeg($full_picmain);
$width = imagesx($im); // Original picture width is stored
$height = imagesy($im); // Original picture height is stored
if($width > $rW){$n_width = $rW;}
else{$n_width = $width;}
if($height > $rH){$n_height = $rH;}
else{$n_height = $height;}
$newimage = imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width,$height);
imagejpeg($newimage, $tsrc);
chmod("$tsrc",0777);
}
////// starting of JPG thumb nail creation//////////
if($ImageType == "image/png"){
$im = imagecreatefrompng($full_picmain);
$width = imagesx($im); // Original picture width is stored
$height = imagesy($im); // Original picture height is stored
if($width > $rW){$n_width = $rW;}
else{$n_width = $width;}
if($height > $rH){$n_height = $rH;}
else{$n_height = $height;}
$newimage = imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width,$height);
imagepng($newimage, $tsrc);
chmod("$tsrc",0777);
}
}
# Get image extention #
function GetImageExtention($type){
if($type == "image/jpeg" || $type == "image/jpg" || $type == "image/pjpeg")
return "jpg";
elseif($type = "image/png")
return "png";
elseif($type == "image/gif")
return "gif";
}
You can call this function like this:
PHP Code:
$PicFile = $_FILES['PrdImg'];
for($i = 0; $i < 10; $i++){
if(is_uploaded_file($PicFile['tmp_name'])){
if($PicFile['size'] < 1048576 && in_array($PicFile['type'], $obj->ArrImageType)){
$imgExt = $obj->GetImageExtention($PicFile['type']);
$PicName = "prjpic_" . substr(md5(time()), 0, 4) . "." . $imgExt;
$PicFullPathName = $picpath . "/" . $PicName;
if(move_uploaded_file($PicFile['tmp_name'], $PicFullPathName)){
$obj->Create_ThumbNail($PicFile['type'], $picpath, 100, 80, $PicName, "thumbnail"); //alternate option for thumbnail is resize
list($width,$height) = getimagesize($PicFullPathName);
if($width > 640){ //default size.
$ratio = (($width - 640) * 100) / $width;
$newheight = $height - ($ratio * $height / 100);
$obj->Create_ThumbNail($PicFile['type'], $picpath, 640, $newheight, $PicName, "resize"); //resizing the image
}
mysql_query("UPDATE tblproductimages SET ProductImagePath='$PicName' WHERE PictureID='$newpicid'") or die(mysql_error());
}
}
}//is picture uploaded
}//for
I have these functions in a lirbary function that's why i have used object to call the function. I think this will help you.
Feel free to ask if you could not get it.
Bookmarks