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.

$allowedfiletypes = array('jpeg','jpg');
$uploadfolder = 'uploads/';
$thumbnailheight = 100; //in pixels
$thumbnailfolder = $uploadfolder.'thumbs/';

//check to see if the form has been submitted
if (array_key_exists('action',$_POST) && is_string($_POST['action']) && (strip_tags($_POST['action']) == 'upload')) {
    echo '<p>Uploading image... ';
    if (empty($_FILES['uploadimage']['name'])) {
        echo '<strong>Error: File not uploaded!</strong></p>';
    } else {
        $uploadfilename = $_FILES['uploadimage']['name'];
        $fileext = strtolower(substr($uploadfilename,strrpos($uploadfilename,'.')+1)); //get the uploaded file extension
        if (!in_array($fileext,$allowedfiletypes)) {
        	echo '<strong>Error: Invalid file extension!</strong></p>';
        } else {
            $fulluploadfilename = $uploadfolder.$uploadfilename;
            if (move_uploaded_file($_FILES['uploadimage']['tmp_name'], $fulluploadfilename)) { //try to move the uploaded file to its final home
                echo $uploadfilename.' has been uploaded succesfully.</p>';
                $im = imagecreatefromjpeg($fulluploadfilename);
                if (!$im) { echo '<p><strong>Error: Could not open image to create thumbnail!</strong></p>'; }
                else {
                    $imw = imagesx($im); // uploaded image width
                    $imh = imagesy($im); // uploaded image height
                    $nh = $thumbnailheight; // thumbnail height
                    $nw = round(($nh / $imh) * $imw); //thumnail width
                    $newim = imagecreatetruecolor($nw, $nh);
                    imagecopyresampled($newim,$im, 0, 0, 0, 0, $nw, $nh, $imw, $imh);
                    $thumbfilename = $thumbnailfolder.$uploadfilename;
                    imagejpeg($newim, $thumbfilename) or die('<p><strong>Error: Could not save thumnbail!</strong></p>');
                    echo '<p><img src="'.$thumbfilename.'"></p>';
            } else {
            	echo '<strong>Error: Could not save file: '.$fulluploadfilename.'</strong></p>';

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data">';
echo 'JPEG Image File:<br>';
echo '<input type="file" name="uploadimage"><br><br>';
echo '<input type="submit" value="Upload Image">';
echo '<input type="hidden" name="action" value="upload">';
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.



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.