Upload image and create thumbnail using PHP


This short tutorial will explain how you upload images and create thumbnail of the images using PHP. You can learn dynamic thumbnail creation with our tutorial and easily integrate this script at your project. Also you would be able to view the working live demo and download the full script from the above links.

upload-image-create-thumbnail-using-php

The below script is complete solution for upload image and create image thumbnail. At first we will create a PHP script that contains a function. The code below makes a function named cwUpload() that will get 7 parameters. $file_data accept input field name, $target_folder accept desire upload folder path, $file_name accept desire uploaded file name, $thumb accept TRUE or FALSE, $thumb_folder accept desire thumbnail folder path, $thumb_width accept thumbnail width, $thumb_height accept thumbnail height. cwUpload() function return the thumbnail name if the thumbnail creation is successful, otherwise return FALSE.

/**
*
* Author: CodexWorld
* Function Name: cwUpload()
* $field_name => Input file field name.
* $target_folder => Folder path where the image will be uploaded.
* $file_name => Custom thumbnail image name. Leave blank for default image name.
* $thumb => TRUE for create thumbnail. FALSE for only upload image.
* $thumb_folder => Folder path where the thumbnail will be stored.
* $thumb_width => Thumbnail width.
* $thumb_height => Thumbnail height.
*
**/
function cwUpload($field_name ''$target_folder ''$file_name ''$thumb FALSE$thumb_folder ''$thumb_width ''$thumb_height ''){
    
//folder path setup
    
$target_path $target_folder;
    
$thumb_path $thumb_folder;
    
    
//file name setup
    
$filename_err explode(".",$_FILES[$field_name]['name']);
    
$filename_err_count count($filename_err);
    
$file_ext $filename_err[$filename_err_count-1];
    if(
$file_name != ''){
        
$fileName $file_name.'.'.$file_ext;
    }else{
        
$fileName $_FILES[$field_name]['name'];
    }
    
    
//upload image path
    
$upload_image $target_path.basename($fileName);
    
    
//upload image
    
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
    {
        
//thumbnail creation
        
if($thumb == TRUE)
        {
            
$thumbnail $thumb_path.$fileName;
            list(
$width,$height) = getimagesize($upload_image);
            
$thumb_create imagecreatetruecolor($thumb_width,$thumb_height);
            switch(
$file_ext){
                case 
'jpg':
                    
$source imagecreatefromjpeg($upload_image);
                    break;
                case 
'jpeg':
                    
$source imagecreatefromjpeg($upload_image);
                    break;
                case 
'png':
                    
$source imagecreatefrompng($upload_image);
                    break;
                case 
'gif':
                    
$source imagecreatefromgif($upload_image);
                    break;
                default:
                    
$source imagecreatefromjpeg($upload_image);
            }
            
imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
            switch(
$file_ext){
                case 
'jpg' || 'jpeg':
                    
imagejpeg($thumb_create,$thumbnail,100);
                    break;
                case 
'png':
                    
imagepng($thumb_create,$thumbnail,100);
                    break;
                case 
'gif':
                    
imagegif($thumb_create,$thumbnail,100);
                    break;
                default:
                    
imagejpeg($thumb_create,$thumbnail,100);
            }
        }

        return 
$fileName;
    }
    else
    {
        return 
false;
    }
}

We will check if upload button is clicked and the image field is not blank. Now call the cwUpload() function and put all parameters value. If you want to only upload the image, you should use first 3 parameters. For upload and thumbnail creation you should be used all parameters of cwUpload() function. Leave the third parameter blank, if you do not want to use custom thumbnail name.Please follow the comment line for better understanding.

if(!empty($_FILES['image']['name'])){
    
    
//call thumbnail creation function and store thumbnail name
    
$upload_img cwUpload('image','uploads/','',TRUE,'uploads/thumbs/','200','160');
    
    
//full path of the thumbnail image
    
$thumb_src 'uploads/thumbs/'.$upload_img;
    
    
//set success and error messages
    
$message $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";
    
}else{
    
    
//if form is not submitted, below variable should be blank
    
$thumb_src '';
    
$message '';
}

Form HTML:

<form method="post" enctype="multipart/form-data">
    <input type="file" name="image"/>
    <input type="submit" name="submit" value="Upload"/>
</form>

The following code is used for display uploaded thumbnail image.

<?php if($thumb_src != ''){ ?>
<img src="<?php echo $thumb_src?>" alt="">
<?php ?>

4 Comments

  1. Aron Said...
  2. J. Collins Said...
  3. Chassidy Said...
  4. Openvz Said...

Leave a reply

Connect With CodexWorld