1,105,592 Community Members

PHP: Image resizing when uploaded

Member Avatar
kaizokupuffball
Light Poster
39 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi!
I got this image uploading script that will upload the user's choosen picture, and save it in the user's folder with the users nickname.

Now, when i got the picture uploaded i want to resize it to 3 exact sizes (3 versions of the image).

240x250 and 2 other sizes.

I don't really know where to start with this, so i thought i should ask here. I have been looking at some threads here, but i can't find some good explained ones.

My image uploading script looks like this:

<?php
			if ($_SESSION['username'])
			{
			
			include 'db_connect.php';
			define ("MAX_SIZE","2000");
		   	$username = $_SESSION['username'];
		   	
			//Checks the file extensions
			function getExtension($str){
					$i = strrpos($str,".");
					if (!$i) { return ""; }
					$I = strlen($str) - $i;
					$ext = substr($str,$i+1,$I);
					return $ext;
			}

			//If error, value change to 1, file will not be uploaded
			$errors=0;
						   
			if (isset($_POST['Submit']))
			{

				//Reads the image name
				$image=$_FILES['image']['name'];
			   
				//If empty
				if ($image == "") {
				echo 'Velg et bilde!';
				$errors = 1;
				}	
                                                       
				//If not empty
				if ($image)
				{
					//Get orgiginal name from client machine
					$filename = stripslashes($_FILES['image']['name']);
					//Get the extension
					$extension = getExtension($filename);
					$extension = strtolower($extension);

					//If file extension not known, error. If not, continue
					if (($extension != "jpg") && ($extension != "jpeg"))
					{
						//Error message
						echo 'Bare JPG og JPEG er tilatte filtyper!';
						$errors = 1;
													   
					}
					   
					else
					{
						if ($extension == "jpg") { $extension = "jpeg"; }
						//Get the size of the image in bytes
						//$_FILES['image']['tmp_name'] is the temporary filename of the file
						//in which the uploaded file was stored on the server
						$size=filesize($_FILES['image']['tmp_name']);

						//Compare the size with the maximum size we defined and print error if bigger
						if ($size > MAX_SIZE*1024)
						{
							echo 'Bildet er for stort! 2M er maks!';
							$errors=1;
						}
					   
						//Giving the uploaded files new name
						$imageName = "$username.$extension";
						$newName = "users/$username/$imageName";
						}}}
					   
					    if(!$errors) {
							if (!move_uploaded_file($_FILES['image']['tmp_name'], $newName))
							$errors=1;
						}

						//If no errors do this
						if (isset($_POST['Submit']) && !$errors)
						{
							echo 'Bildet er lastet opp og blir byttet!<br>Vær ops på at vi sjekker bildet senere.<br>Vis vi finner det utilpassende vil vi slette det.';
							$sql = "UPDATE members SET user_pic='http://www.something.net/$newName' WHERE username='$username'";
							$_SESSION['user_pic'] = $newName;									   
							$result = mysql_query($sql);
							mysql_close($conn);
						}
				//The form
				echo '
					<div class="box_square">
					<form id="changeuserpic" action="" method="POST" enctype="multipart/form-data">
					<img src="resources/byttbilde.png" border="0" width="173" height="27" style="margin-left:-8px;" /><br><br>
					<input class="f_input box_round" type="file" name="image" /><br><br>
					<input class="box_round f_submit" type="submit" name="Submit" value="Bytt bilde" />
					</form>
					</div>';
			}
			
			else 
			echo '<div class="box_square"><div class="form_error">Du må være logget inn for å se denne siden!<br><br>Logg inn <a href="">her</a>.</div></div>';
			die ();	
			?>

I was hoping someone had the time to explain the process and stuff.
Thx in advance!

Member Avatar
ddymacek
Posting Whiz
324 posts since Jun 2010
Reputation Points: 15 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 0 [?]
 
0
 

do you have gd image library for processing images? is so I have a script you could use

Member Avatar
kaizokupuffball
Light Poster
39 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

do you have gd image library for processing images? is so I have a script you could use

How do i check if my server have that installed?

Member Avatar
ddymacek
Posting Whiz
324 posts since Jun 2010
Reputation Points: 15 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 0 [?]
 
0
 
<?php
var_dump(gd_info());
?>

http://www.php.net/manual/en/function.gd-info.php
more than likely you have one of two libraries for image processing / creation
either gd library or ImageMagick I have code that works with GD.

Member Avatar
kaizokupuffball
Light Poster
39 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The site echoed this out:

array(12) { ["GD Version"]=> string(27) "bundled (2.0.34 compatible)" ["FreeType Support"]=> bool(true) ["FreeType Linkage"]=> string(13) "with freetype" ["T1Lib Support"]=> bool(false) ["GIF Read Support"]=> bool(true) ["GIF Create Support"]=> bool(true) ["JPEG Support"]=> bool(true) ["PNG Support"]=> bool(true) ["WBMP Support"]=> bool(true) ["XPM Support"]=> bool(true) ["XBM Support"]=> bool(true) ["JIS-mapped Japanese Font Support"]=> bool(false) }

Member Avatar
ddymacek
Posting Whiz
324 posts since Jun 2010
Reputation Points: 15 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 0 [?]
 
0
 

excellent, the script i have is rather long, there are two files, posted here:
http://www.sendspace.com/file/s7r6dz
the first one class.upload.php is to be included in another script so you can use it's functions. I did not write the upload class but rather found it when I had the same issue you are having. the second script is an example of how to use the upload script there is also an example within class.upload.php itself. I left some notes in save.php for you please read before use this will not work without a few alterations that I am leaving up to you. enjoy.

Member Avatar
kaizokupuffball
Light Poster
39 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you!
I will check this out now. :)

Member Avatar
ddymacek
Posting Whiz
324 posts since Jun 2010
Reputation Points: 15 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 0 [?]
 
0
 

I did not include a form or any way to post to this. So, change the action in your form to post to action='save.php' which in turn save.php will call the upload script an have all of its functions available to you.

Member Avatar
sv3tli0
Junior Poster in Training
81 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 18 [?]
Skill Endorsements: 0 [?]
 
0
 

I have one old script which worked for me in the past :

function make_thumb($img_name,$filename,$thumb_w,$thumb_h)
 {
 	$ext=getExtension($img_name);
 	if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) $src_img=imagecreatefromjpeg($img_name);
  	if(!strcmp("png",$ext)) $src_img=imagecreatefrompng($img_name);
  	if(!strcmp("gif",$ext)) $src_img=imagecreatefromgif($img_name);

 	$old_x=imageSX($src_img); $old_y=imageSY($src_img);
 	
 	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
 	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
 	if(!strcmp("png",$ext)) imagepng($dst_img,$filename); 
 	elseif(!strcmp("gif",$ext)) imagegif($dst_img,$filename); 
 	else imagejpeg($dst_img,$filename); 
	imagedestroy($dst_img); imagedestroy($src_img);   }

 function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; }

make_thumb(OLD_IMAGE, NEW_IMAGE, NEWWIDTH, NEWHEIGHT);  //OLD_IMAGE and NEW_IMAGE are exact file location and names as './img/picture.png'

The last line is the function called to create the image.
Know that this function will resize the ima to exact width/height.
If you need anti blur effect you will need to make a logic calculation inside the function. The second function is for the file extension .. its little stupied but how ever :)

Member Avatar
kaizokupuffball
Light Poster
39 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How do i set the exact values for the width and height, Sv3tli0?

Member Avatar
sarithak
Junior Poster
185 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

HI...

This is only for JPEG...
First u should upload the image and then get the properties of that original image.
Look into the below code... easy to understand..

$add=""; //Path of Original Uploaded Image
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored

$n_width=200;
$n_height=134;
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$t_add);    //$t_add is where thumbnail image stored!
chmod("$t_add",0777);
Member Avatar
sarithak
Junior Poster
185 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

HI...

This is only for JPEG...
First u should upload the image and then get the properties of that original image.
Look into the below code... easy to understand..

$add=""; //Path of Original Uploaded Image
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored

$n_width=200;
$n_height=134;
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$t_add);    //$t_add is where thumbnail image stored!
chmod("$t_add",0777);
Member Avatar
sarithak
Junior Poster
185 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey..

My code same as like @sv3tli0... this is for all types of images... Previously i didnt check sv3tli0's code..

Member Avatar
kaizokupuffball
Light Poster
39 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thx, i will look into it! :)

Member Avatar
kaizokupuffball
Light Poster
39 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I got it working, but i found out that the image quality is getting pretty crappy if i change the height and width so it does not have the orginal ratio..

Can i modify the script to only change the width, but keep the height ratio? Almost like a resize percentage, but the width is specified?

//Resizing and saving image

$t_add = "users/$username/$username-big.$extension";

//Path of Original Uploaded Image
$add=$newName; 
							$im=ImageCreateFromJPEG($add);

//Store orginal width
$width=ImageSx($im);

//Store orginal height
$height=ImageSy($im);

							 
$n_width=246;
$n_height=176;
							$newimage=imagecreatetruecolor($n_width,$n_height);				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$t_add);

//$t_add is where thumbnail image stored!
chmod("$t_add",0777);

//End resize and save
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: