PHP – Image Resize


PHP – Image Resize

// Config Options
$method = “gd2”;                 // Which method you wish to use to generate the thumbnail
// only GD1 and GD2 are spported.
$new_size = “128”;               // Width of the thumbnail
$jpeg_qual = “80”;               // Quality of the generated thumbnail.
$file_mode = “0644”;             // File permissions of the thumbnail default setting is 0644.
// Chaning it may cause permission problems//
$SessionID = md5( uniqid( rand () ) );
$original_pic = “images/Restaurant/”.$restna.”/”.$_FILES[‘userfile’][‘name’];
$resized_pic = $pathname.$SessionID.$_FILES[‘userfile’][‘name’];
$newname1 =$SessionID.$_FILES[‘userfile’][‘name’];
$newname=list($imgdot,$imgdot1) = split(‘[..-]’, $newname1);
if($imgdot1==”gif” || $imgdot1==”GIF”)
{
$imgdot1=$_FILES[‘userfile’][‘name’];
}
else
{
$imgdot1=$newname1;
}
resize_image($original_pic, $resized_pic, $new_size, $method);
// Modified from Coppermine gallery
function resize_image($src_file, $dest_file, $new_size, $method) {
global $CONFIG, $method, $file_mode, $jpeg_qual;
$imginfo = getimagesize($src_file);
if ($imginfo == null)
return false;
// GD can only handle JPG & PNG images
if ($imginfo[2] != 2 && $imginfo[2] != 3 && ($method == ‘gd1’ || $method == ‘gd2’)){
//echo “Invalid filetype”;
return false;
}
// height/width
$srcWidth = $imginfo[0];
$srcHeight = $imginfo[1];
$ratio = max($srcWidth, $srcHeight) / $new_size;
$ratio = max($ratio, 1.0);
$destWidth = (int)($srcWidth / $ratio);
$destHeight = (int)($srcHeight / $ratio);
// Method for thumbnails creation
switch ($method) {
case “gd1” :
if (!function_exists(‘imagecreatefromjpeg’)) {
die(“PHP running on your server does not support the GD image library, check with your webhost if they can setup PHP with GD Library.”);
}
if ($imginfo[2] == 2)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img){
//echo “Invalid_image format!”;
return false;
}
$dst_img = imagecreate($destWidth, $destHeight);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
imagejpeg($dst_img, $dest_file, $jpeg_qual);
imagedestroy($src_img);
imagedestroy($dst_img);
break;
case “gd2” :
if (!function_exists(‘imagecreatefromjpeg’)) {
die(“PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed”);
}
if (!function_exists(‘imagecreatetruecolor’)) {
die(“PHP running on your server does not support GD version 2.x, please switch to GD version 1.x on the config page”);
}
if ($imginfo[2] == 2)
$src_img = imagecreatefromjpeg($src_file);
else
$src_img = imagecreatefrompng($src_file);
if (!$src_img){
// echo “invalid_image”;
return false;
}
$dst_img = imagecreatetruecolor($destWidth, $destHeight);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
imagejpeg($dst_img, $dest_file, $jpeg_qual);
imagedestroy($src_img);
imagedestroy($dst_img);
break;
}
// Set mode of uploaded picture
chmod($dest_file, octdec($file_mode));
// We check that the image is valid
$imginfo = getimagesize($dest_file);
if ($imginfo == null){
echo “resize_failed”;
@unlink($dest_file);
return false;
} else {
return true;
}
}
Advertisements

thanks 4 u comments..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s