PHP image upload and resize it before uploading tutorial

PHP image upload and resize it before uploading tutorial

In this post we will give you information about PHP image upload and resize it before uploading tutorial. Hear we will give you detail about PHP image upload and resize it before uploading tutorialAnd how to use it also give you demo for it if it is necessary.

In this PHP Tutorial, I will let you know how to upload image in the folder and one of the most common actions in this example, you will know how to resize images and generate thumbnail of uploaded images.


In this article, we will learn about image processing like resizing an image, generating thumbnails and also creating a new image with desired image format and image size using PHP code. We are going to write an easy PHP code to upload an image, accept user-submitted image file data and use it to resize the image.

In today’s era of web based application, displaying images, sharing data and documents are an important part. Almost all the web based applications come with an option to upload an image, but we can’t restrict the user to upload an image with a specific format or with a specific file size.

However, uploading an image with a large file size will ultimately slow down the performance of the web application as it will take a longer time to load. Therefore, the uploaded image must be resized or a thumbnail should be generated of that image to keep the performance of the application at it’s best.


So, here we are going to create a thumbnail or a resized image from the uploaded original image in different formats like JPG, JPEG, PNG and GIF using PHP.


index.php

In this step, I will have simple HTML form with enctype="multipart/form-data" form to select a file and upload to the server.

<!DOCTYPE html>
<html>
<head>
<title>PHP resize and upload image tutorial</title>
</head>
<body>

    <form action="resize.php"method="post"enctype="multipart/form-data">
        <inputtype="file"name="image"/> 
        <inputtype="submit"name="submit"value="Submit"/>
    </form>

</body>
</html>



resize.php

Now I will write the PHP script to handle the image upload process and with the help of some predefined methods in PHP, I will resize the image and generate the thumbnail accordingly.

<?php


if(isset($_POST["submit"])) {
    if(is_array($_FILES)) {


        $uploadedFile=$_FILES['file']['tmp_name']; 
        $sourceProperties=getimagesize($uploadedFile);
        $newFileName=time();
        $dirPath="uploads/";
        $ext=pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $imageType=$sourceProperties[2];


        switch ($imageType) {


            case IMAGETYPE_PNG:
                $imageSrc= imagecreatefrompng($uploadedFile); 
                $tmp= imageResize($imageSrc,$sourceProperties[],$sourceProperties[1]);
                imagepng($tmp,$dirPath.$newFileName."_thump.".$ext);
                break;           

            case IMAGETYPE_JPEG:
                $imageSrc= imagecreatefromjpeg($uploadedFile); 
                $tmp= imageResize($imageSrc,$sourceProperties[],$sourceProperties[1]);
                imagejpeg($tmp,$dirPath.$newFileName."_thump.".$ext);
                break;
            
            case IMAGETYPE_GIF:
                $imageSrc= imagecreatefromgif($uploadedFile); 
                $tmp= imageResize($imageSrc,$sourceProperties[],$sourceProperties[1]);
                imagegif($tmp,$dirPath.$newFileName."_thump.".$ext);
                break;

            default:
                echo"Invalid Image type.";
                exit;
                break;
        }


        move_uploaded_file($uploadedFile, $dirPath.$newFileName.".".$ext);
        echo"Image Resize Successfully.";
    }
}


functionimageResize($imageSrc,$imageWidth,$imageHeight) {

    $newImageWidth=200;
    $newImageHeight=200;

    $newImageLayer=imagecreatetruecolor($newImageWidth,$newImageHeight);
    imagecopyresampled($newImageLayer,$imageSrc,,,,,$newImageWidth,$newImageHeight,$imageWidth,$imageHeight);

    return$newImageLayer;
}
?>

Label :

PHP

File

HTML

How To

Web Development

Hope this code and post will helped you for implement PHP image upload and resize it before uploading tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  64  =  72

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US