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.


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>
<title>PHP resize and upload image tutorial</title>

    <form action="resize.php"method="post"enctype="multipart/form-data">



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.


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

        $ext=pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

        switch ($imageType) {

            case IMAGETYPE_PNG:
                $imageSrc= imagecreatefrompng($uploadedFile); 
                $tmp= imageResize($imageSrc,$sourceProperties[],$sourceProperties[1]);

            case IMAGETYPE_JPEG:
                $imageSrc= imagecreatefromjpeg($uploadedFile); 
                $tmp= imageResize($imageSrc,$sourceProperties[],$sourceProperties[1]);
            case IMAGETYPE_GIF:
                $imageSrc= imagecreatefromgif($uploadedFile); 
                $tmp= imageResize($imageSrc,$sourceProperties[],$sourceProperties[1]);

                echo"Invalid Image type.";

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

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




Label :




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