Tuesday 15 November 2011

Thumbnail Genrator Function


/**This function used for the purpose of image resize just copy and paste
    *$imgType = $arr['upload_file']['type']; there need to change upload_file name with your given name
**/
public function resizeImage($originalImagepath&name,$toWidth,$toHeight,$smallimagepath&name,$arr)
{
ini_set("memory_limit", "50M");
$imgType = $arr['upload_file']['type'];

list($width, $height) = getimagesize($originalImage);

if($width < $toWidth){
$toWidth = $width;
}

if($height < $toHeight){
$toHeight = $height;
}

if($toWidth != 0){
$xscale=$width/$toWidth;
}

if($toHeight != 0){
$yscale=$height/$toHeight;
}

if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
} else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}

$imageResized = imagecreatetruecolor($new_width, $new_height);

if ($imgType =="image/gif"){
$imageTmp = imagecreatefromgif($originalImage);
$background = imagecolorallocate($imageResized, 0, 0, 0);
            ImageColorTransparent($imageResized, $background);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagegif($imageResized, $path);
} elseif($imgType =="image/png" || $imgType =="image/x-png") {
$imageTmp = imagecreatefrompng($originalImage);
imagealphablending($imageResized, false);
imagesavealpha($imageResized, true);
$background = imagecolorallocatealpha($imageResized, 255, 255, 255, 127);
imagecolortransparent($imageResized, $background);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng( $imageResized,$path);
}
else {
$imageTmp  = imagecreatefromjpeg($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($imageResized,$path);
}

return $imageResized;
}//End resizeImage Function there.....


How to use this function................................................
1. Use this function after move _uploaded_file(); 

2. Used as following.

                                $upload_path = "property_images/"; //My image folder where save
define("ROOT_PATH", $_SERVER['DOCUMENT_ROOT'].'/projectName/');
define("USER_IMAGE_PATH", ROOT_PATH."property_images/");// proper image folder where your image save.
$imageName = basename($_FILES['upload_file']['name']); // Get image name
$originalImg = USER_IMAGE_PATH.$imageName; //Get orignal image path and image name 
$upload_path = $upload_path . basename( $_FILES['upload_file']['name']);
$unique = rand();// for unique image name 
$smallImg = USER_IMAGE_PATH."small_".$unique."_".$imageName; //first image small size
$bigImg = USER_IMAGE_PATH."big_".$unique."_".$imageName;  // you want big if
$insertedName = "small_".$unique."_".$imageName;
move_uploaded_file($_FILES['upload_file']['tmp_name'], $upload_path);
$this->resizeImage($originalImg, 170, 130,$smallImg,$_FILES);
$this->resizeImage($originalImg, 376, 304,$bigImg,$_FILES);

It's Working if any problem image thumb is black then check resize()  function there is not get file type just print this $imgType = $arr['upload_file']['type']; and check value. 

No comments:

Post a Comment