How To Image Thumbnail Creation PHP

How To Image Thumbnail Creation PHP

How To Image Thumbnail Creation PHP

0 Sales

Free

This short tutorial explains how you use PHP to upload images and create image thumbnails. With our tutorial, you can learn how to create dynamic thumbnails and easily integrate this script into your project. You could also view the live working demo and download the complete script from the links above.


The script below is a complete solution for uploading images and creating thumbnails for images. We must build a PHP script with a feature at first. The code below is a function called cwUpload) (which will have 7 parameters. $file data accepts the name of the input field, $target folder accepts the path of the desired upload folder, $file name accepts the desired file name, $thumb accepts TRUE or FALSE, $thumb folder accepts the desired thumbnail folder path, $thumb width accepts thumbnail width, $thumb height accepts thumbnail height. If the thumbnail development is efficient, cwUpload() returns the thumbnail name, otherwise FALSE returns.


 

/**

*

* 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 that the upload button is clicked and that the image field is not blank. Now call the function cwUpload) (and set the value of all parameters. If you only want to upload the image, the first 3 parameters should be used. All parameters of the cwUpload) (function should be used to build upload and thumbnail. Leave the third parameter empty if you don't want to use the custom thumbnail name. Please follow the comment line for a 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 } ?>

LICENSE OF USE

You can use it for personal or commercial projects. You can't resell it partially or in this form.

PRODUCT INFO

Create Date : May 15, 2020

Updated Date : Jun 17, 2021

Ratings

Comments : 0

Downloads : 0