Simple PHP Image Upload Form

This little chunk of PHP allows a user to upload a JPEG image and then resizes that image to generate a thumbnail automatically. With a little work, you can change the file types allowed, formula for resizing the thumbnails, etc. but I wanted to keep this simple so only JPEGs are allowed and everything is resized based on height.

1.   $allowedfiletypes = array('jpeg','jpg');
2.   $uploadfolder = 'uploads/';
3.   $thumbnailheight = 100; //in pixels
4.   $thumbnailfolder = $uploadfolder.'thumbs/';
5.   
6.   //check to see if the form has been submitted
7.   if (array_key_exists('action',$_POST) && is_string($_POST['action']) && (strip_tags($_POST['action']) == 'upload')) {
8.   	echo '<p>Uploading image... ';
9.   	if (empty($_FILES['uploadimage']['name'])) {
10.  		echo '<strong>Error: File not uploaded!</strong></p>';
11.  	} else {
12.  		$uploadfilename = $_FILES['uploadimage']['name'];
13.  		$fileext = strtolower(substr($uploadfilename,strrpos($uploadfilename,'.')+1)); //get the uploaded file extension
14.  		if (!in_array($fileext,$allowedfiletypes)) {
15.  			echo '<strong>Error: Invalid file extension!</strong></p>';
16.  		} else {
17.  			$fulluploadfilename = $uploadfolder.$uploadfilename;
18.  			if (move_uploaded_file($_FILES['uploadimage']['tmp_name'], $fulluploadfilename)) { //try to move the uploaded file to its final home
19.  				echo $uploadfilename.' has been uploaded succesfully.</p>';
20.  				$im = imagecreatefromjpeg($fulluploadfilename);
21.  				if (!$im) { echo '<p><strong>Error: Could not open image to create thumbnail!</strong></p>'; }
22.  				else {
23.  					$imw = imagesx($im); // uploaded image width
24.  					$imh = imagesy($im); // uploaded image height
25.  					$nh = $thumbnailheight; // thumbnail height
26.  					$nw = round(($nh / $imh) * $imw); //thumnail width
27.  					$newim = imagecreatetruecolor($nw, $nh);
28.  					imagecopyresampled($newim,$im, 0, 0, 0, 0, $nw, $nh, $imw, $imh);
29.  					$thumbfilename = $thumbnailfolder.$uploadfilename;
30.  					imagejpeg($newim, $thumbfilename) or die('<p><strong>Error: Could not save thumnbail!</strong></p>');
31.  					echo '<p><img src="'.$thumbfilename.'"></p>';
32.  				}
33.  			} else {
34.  				echo '<strong>Error: Could not save file: '.$fulluploadfilename.'</strong></p>';
35.  			}
36.  		}
37.  	}
38.  }
39.  
40.  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data">';
41.  echo 'JPEG Image File:<br>';
42.  echo '<input type="file" name="uploadimage"><br><br>';
43.  echo '<input type="submit" value="Upload Image">';
44.  echo '<input type="hidden" name="action" value="upload">';
45.  echo '</form>';

After you've uploaded it, here's how you can easily resize your image on the fly.

Make sure that you're setting the permissions on your upload correctly. Having an unwriteable folder there is a common stumbling block.


Comments

Loading…

This post was first published on January 17th, 2009 and last updated on December 23rd, 2015 by Robert James Reese in the following categories: Images and PHP. Before using any of the code or other content in this post, you must read and agree to our terms of use.