Can any one help me how to resize images in php with out changing its original quality,that is if an image with size 100/100 when it reduced to 40/30 or any our required dimensions with out changing its quality .

Can any one help me how to resize images in php with out changing its original quality,that is if an image with size 100/100 when it reduced to 40/30 or any our required dimensions with out changing its quality .

Please check blow Pices of Code

$tmpFName =	$_FILES["UploadFloorPlane"]["tmp_name"];
$fType	 =	$_FILES["UploadFloorPlane"]["type"];
$fSize	 =	$_FILES["UploadFloorPlane"]["size"];
list($width, $height, $type, $attr) = getimagesize($tmpFName);
if($width > 0) {
$fType = image_type_to_mime_type($type);
$img_valid = "true";
} else 
$img_valid = "false";

if($img_valid == "true"){
if(($fType == 'image/jpeg') || ($fType == 'image/pjpeg')){
$pExt = ".jpg";
} 
elseif($fType == 'image/gif'){
$pExt=".gif";
} 
elseif($fType == 'image/png'){
$pExt=".png";
} 
else{ 
$result = -2;
}
$rand=rand(0,10000);
$Enlarge = "ImageName";
$thumb   = "ImageName_thumb";
$origImage = $Enlarge ;
copy($tmpFName,$origImage.$pExt);
chmod ($origImage.$pExt, 0755);  // octal; correct value of mode
$NormalThumbImgName = $Enlarge.$pExt;
$SmallThumbImgName = $thumb.$pExt;
$DestPath = getcwd();
$objThumbnail = new ThumbNail();
$objThumbnail->CreateThumb($SmallThumbImgName,$DestPath.$origImage.$pExt,$DestPath,40,30,false,false);

Download the below Url Class files
http://www.phpclasses.org/browse/file/4883.html


BaburajSns
Securenext Softwares Pvt

One more point...
By using our php codes..
Changing from 100X100 image to 40X50 image will result into same quality image...But vice versa will give corrupted image..

So make it note when your are going develop in your projects..

Thanks for your replay but where is the code iasked can you please tell me

$tmpFName = $_FILES["UploadFloorPlane"]["tmp_name"];
$fType = $_FILES["UploadFloorPlane"]["type"];
$fSize = $_FILES["UploadFloorPlane"]["size"];
list($width, $height, $type, $attr) = getimagesize($tmpFName);
if($width > 0) {
$fType = image_type_to_mime_type($type);
$img_valid = "true";
} else 
$img_valid = "false";

if($img_valid == "true"){
if(($fType == 'image/jpeg') || ($fType == 'image/pjpeg')){
$pExt = ".jpg";
} 
elseif($fType == 'image/gif'){
$pExt=".gif";
} 
elseif($fType == 'image/png'){
$pExt=".png";
} 
else{ 
$result = -2;
}
$rand=rand(0,10000);
$Enlarge = "ImageName";
$thumb = "ImageName_thumb";
$origImage = $Enlarge ;
copy($tmpFName,$origImage.$pExt);
chmod ($origImage.$pExt, 0755); // octal; correct value of mode
$NormalThumbImgName = $Enlarge.$pExt;
$SmallThumbImgName = $thumb.$pExt;
$DestPath = getcwd();
$objThumbnail = new ThumbNail();
$objThumbnail->CreateThumb($SmallThumbImgName,$DestPath.$origImage.$pExt,$DestPath,40,30,false,false);

Download the CreateThumb Class files function
http://www.daniweb.com/forums/post850887.html#post850887

Hi, I have made a function that will resize images and is as follows:

<?php
function resize_image($filename,$newwidth,$newheight) {
    if (!file_exists($filename) || !in_array(preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename)),array('png','jpg','jpeg','gif','xbm','xpm','wbmp'))) {
        //note .wbmp is 'wireless bitmap' and not 'windows bitmap'.
        return false;
        } else {
        $ext=preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename));
        if ($ext=='jpg') { $ext='jpeg'; }
        $ext='imagecreatefrom'.$ext;
        list($width, $height) = getimagesize($filename);
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = $ext($filename);
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        return $thumb;
        }
    }
$img = resize_image('file_from.gif','320','280');
//save image to file
imagejpeg ($img,'file.gif',100);
?>

Hope that helps ya as I would imagine that some of the previous posts would be a little confusing without the syntax highlighter.

I do not understand why we (the programmers) use unnecessary huge buggy codes of our own for resizing images??!! As we've phpThumb, custom image resizing functions are just a history. Then why do we manually write those codes again and make our script buggy!

Yes I know what I am saying. I was using my own codes for image resizing and one day I got problem with corrupted images and got no solution. Then I used phpThumb and the problem is fixed..

I do not understand why we (the programmers) use unnecessary huge buggy codes of our own for resizing images??!! As we've phpThumb, custom image resizing functions are just a history. Then why do we manually write those codes again and make our script buggy!

Yes I know what I am saying. I was using my own codes for image resizing and one day I got problem with corrupted images and got no solution. Then I used phpThumb and the problem is fixed..

To know more details please visit this

Because that's how programmers work. phpThumb didn't magically appear, there was some dumb guy/gal saying "Well I don't like that library, I'm going to write my own and name it phpThumb." It just so happens other people started using it. So, of course, there are still programmers that say "Well, I don't like that library I'm going to write my own."

Most of the time it's because in programmers' heads their code rocks and everyone else's sucks.

Hi @cwarn23 , i want ask something if you dont mind. I had look to your example code. It simple and nice ;) but it is useful for resizing image that will store to db as blob? I try to apply it, but not working :'(. Hope you can give an idea based on your experience. Appreciate your respon. Thanks in advance.

just in case, here my code

if ($submit == 'Simpan')
{
	global $strDesc;
	$max_size = 1000000000;
	$fileUpload = $HTTP_POST_FILES['fileUpload1']['tmp_name'] ;
	if($fileUpload != NULL)
	{
		  $fileUpload_name=$HTTP_POST_FILES['fileUpload1']['name'];
		  $fileUpload_size=$HTTP_POST_FILES['fileUpload1']['size'];
		  $fileUpload_type=$HTTP_POST_FILES['fileUpload1']['type'];
		  $fileUpload = $HTTP_POST_FILES['fileUpload1']['tmp_name'] ;
		
		  $fileHandle=fopen($fileUpload, "rb");
		  $fileContent=fread($fileHandle,filesize($fileUpload));
		  $fileContent=addslashes($fileContent);
		  fclose($fileHandle);
			
		$sqls="";		
		  if ($fileUpload_size > (1024*1024))
		  {
		  	echo "reduce";
				$fileContent=resize_image($fileUpload_name,'500','500');
				echo $fileContent;
				$sqls = "INSERT INTO image_store (image_1,description) VALUES ('$fileContent','$fileUpload_type')";    
		  }		
		  else
		  {
		  	echo "add biasa";
			$sqls = "INSERT INTO image_store (image_1,description) VALUES ('$fileContent','$fileUpload_type')";    
		  }
		  echo $sqls;
		 // if($sqls !="")
      	  //	$result = mysql_query($sqls);
				
		  echo '<span style="width: 600; height: 30; font-size: 20px; font-family: verdana; color: #FF0000; ">Berjaya!!!!.</span><br>';
	}
		
}
function resize_image($filename,$newwidth,$newheight) 
{
	echo $filename;
	if (!file_exists($filename) || !in_array(preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename)),array('png','jpg','jpeg','gif','xbm','xpm','wbmp','pjpeg'))) 
	{
		//note .wbmp is 'wireless bitmap' and not 'windows bitmap'.
		echo "false";
		return false;
	} 
	else 
	{
		echo "true";
		$ext=preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename));
		if ($ext=='jpg') { $ext='jpeg'; }
		$ext='imagecreatefrom'.$ext;
		list($width, $height) = getimagesize($filename);
		$thumb = imagecreatetruecolor($newwidth, $newheight);
		$source = $ext($filename);
		imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		return $thumb;
	}
}

Hi, I have made a function that will resize images and is as follows:

<?php
function resize_image($filename,$newwidth,$newheight) {
    if (!file_exists($filename) || !in_array(preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename)),array('png','jpg','jpeg','gif','xbm','xpm','wbmp'))) {
        //note .wbmp is 'wireless bitmap' and not 'windows bitmap'.
        return false;
        } else {
        $ext=preg_replace('/(.*)[.]([^.]+)/is','$2',basename($filename));
        if ($ext=='jpg') { $ext='jpeg'; }
        $ext='imagecreatefrom'.$ext;
        list($width, $height) = getimagesize($filename);
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = $ext($filename);
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        return $thumb;
        }
    }
$img = resize_image('file_from.gif','320','280');
//save image to file
imagejpeg ($img,'file.gif',100);
?>

Hope that helps ya as I would imagine that some of the previous posts would be a little confusing without the syntax highlighter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.