Jump to content

[SOLVED] Resizing images with imagecopyresampled


Lefteris

Recommended Posts

Hello all,

 

I have a problem. I can't resize images with imagecopyresampled. Here is my code :

 

....
....
//if the size of the image is greater than 200 kbs
			if($_FILES['userpic']['size'] > 204800)
				die("Please upload an image with size lower than 200 Kbs");
			//get its dimensions
			$dimensions = getimagesize($_FILES['userpic']['tmp_name']);

			//if the dimensions don't fit then resize it!
			if($dimensions['width'] > 150 || $dimensions['height'] > 150)
			{	

				imagecopyresampled($_FILES['userpic']['tmp_name'],$_FILES['userpic']['tmp_name'],0,0,0,0,150,150,$dimensions['width'],$dimensions['height']);
			}

			$fcontents = file_get_contents($_FILES['userpic']['tmp_name']);

			//store the image in the db
			$sql->updatetable("users","userImage","'".mysql_real_escape_string($fcontents)."'","username","'".$username."'");
			//and its type
			$sql->updatetable("users","userImageType","'".$extension."'","username","'".$username."'");
...
...

 

Shouldn't this work? It is not working. Actually ... it worked a little ... but the resizing was completely out of proportion. It should get resized to 150x150 but it got resized to 280x212 pixels. Any idea why? Also what is the appropriate way to resize an image? Thank in advance.

Sorry for bumping my own topic but since it got only 4 views I assume it got buried due to lots of new topics and none got a change to see it. So ... anyone got any idea how to properly resize an image with php? Why is my way wrong?

Thanks a lot friend, but I still have a problem. Turned out I had errors in the above code so it never went to the imagecopyresampled part. In particular I never checked the width and height of the image correctly. I tried to do it as I had done but it did not accept $_FILES['userpic']['tmp_name'] as either a destination image or a source image for the image copy resampled.

 

So I then tried it your way, like in the script you linked but a lot simpler since I just want to resize everything to 150x150. The problem is ... how do you store this image in the database? I used to store images in the database by doing something like that : $fcontents = file_get_contents($img_dst); and then  adding it with mysql_real_escape_string($fcontents)

 

But now img_dst is not a filename somewhere in the server, it is an image resource. How do you store it in the database? Moreover I can't check to see if the resizing took place if I don't store it in the DB since I already have headers on the top of the page and can't see the image with a simple imagejpeg($img_dst).

 

Here is the code :

//get its dimensions
			$dimensions = getimagesize($_FILES['userpic']['tmp_name']);
			$imgwidth = $dimensions[0];
			$imgheight = $dimensions[1];
			//if the dimensions don't fit then resize it!
			if($imgwidth != 150 || $imgheight != 150)
			{	
				$img_dst=imagecreatetruecolor(150,150);
				$img_src = imagecreatefrompng($_FILES['userpic']['tmp_name']);
				//resize using this ultra cool function! Really cool! 
				imagecopyresampled($img_dst,$img_src,0,0,0,0,150,150,$imgwidth,$imgheight);	
			}

Storing the image itself is not a good idea. I have had nothing but problem doing it. I prefer to store the image name or the name and path in the database and just use html to display the picture when needed.

 

In any case, to store your image you would have to first save the image to the drive, then insert it into the database, then delete the file.

 

You can save the image by adding a parameter to imagejpeg

 

$path = "absolute/path/with/name.jpg";
imagejpeg($img_dst, $path);

 

Ray

Aha I see. Thanks a lot, makes sense that I would have to save it somewhere. Sounds like a lot of trouble but I feel really organized by having user images in the db instead of in a file. I will try to implement it and post back when I do.

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.