How to Resize Image Resize Upload in PHP

Sheeraz Gul Feb 02, 2024
  1. Use PHP gd Library to Resize Images on Upload
  2. Use the verot.net Library to Resize Images in PHP
  3. Conclusion
How to Resize Image Resize Upload in PHP

In PHP, we can resize the image on upload. PHP has a built-in library gd to perform image operations; we can create functions to resize the image on upload.

This tutorial demonstrates different ways of resizing the image in PHP.

Use PHP gd Library to Resize Images on Upload

PHP has a gd library to perform operations on images. Open your php.ini file and do as mentioned below:

;extension=php_gd2.dll

Search for gd in php.ini and remove the comment ; In PHP 8, it is gd, and the versions before PHP 8 will be gd2.

extension=php_gd2.dll

Restart the server, and you are good to go.

Example of uploading an image and resizing it:

test.php:

<!DOCTYPE html>
<html>
<head>
    <title>PHP Resize Image On upload</title>
</head>
<body>
<div>

    <form action="action.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadimage" /> 
		<br><br>
        <input type="submit" name="upload" value="Upload" />
    </form>

</div>
</body>
</html>

action.php:

<?php
if(isset($_POST["upload"])) {
    if(is_array($_FILES)) {
        $uploaded_file = $_FILES['uploadimage']['tmp_name']; 
        $upl_img_properties = getimagesize($uploaded_file);
        $file_name_id= rand(10,100);
		$new_file_name = "Resized Image_".$file_name_id;
        $folder_path = "upload/";
        $img_ext = pathinfo($_FILES['uploadimage']['name'], PATHINFO_EXTENSION);
        $image_type = $upl_img_properties[2];

        switch ($image_type) {
			//for PNG Image
            case IMAGETYPE_PNG:
                $image_type_id = imagecreatefrompng($uploaded_file); 
                $target_layer = image_resize($image_type_id, $upl_img_properties[0], $upl_img_properties[1]);
                imagepng($target_layer, $folder_path. $new_file_name. ".". $img_ext);
                break;
            //for GIF Image
            case IMAGETYPE_GIF:
                $image_type_id = imagecreatefromgif($uploaded_file); 
                $target_layer = image_resize($image_type_id, $upl_img_properties[0], $upl_img_properties[1]);
                imagegif($target_layer, $folder_path. $new_file_name.".". $img_ext);
                break;
            //for JPEG Image
            case IMAGETYPE_JPEG:
                $image_type_id = imagecreatefromjpeg($uploaded_file); 
                $target_layer = image_resize($image_type_id, $upl_img_properties[0], $upl_img_properties[1]);
                imagejpeg($target_layer, $folder_path. $new_file_name.".". $img_ext);
                break;

            default:
                echo "Please select a 'PNG', 'GIF'or JPEG image";
                exit;
                break;

        }

        //for the record move the uploaded file to the resized image directory
        move_uploaded_file($uploaded_file, $folder_path. "Original Uploaded Image_".$file_name_id.".". $img_ext);
        echo "Image is resized according to given Width and Height";
    }
}

function image_resize($image_type_id, $img_width, $img_height) {

    $target_width =1000;
    $target_height =1000;
    $target_layer= imagecreatetruecolor($target_width, $target_height);
    imagecopyresampled($target_layer, $image_type_id,0,0,0,0, $target_width, $target_height, $img_width, $img_height);
    return $target_layer;
}
?>

We can upload an image PNG, GIF, JPEG and resize it with the code above.

Output:

Image Resize Using PHP

Use the verot.net Library to Resize Images in PHP

verot.net provides a library for PHP image operations. Verot library makes image upload operations easy and provides different ways of uploading the image.

First, download the library from Verot class.upload.php. We downloaded the zip file. Extract the zip file in your root directory.

There will be two folders, src and test. src contains all the source code and test contains the code to upload and manipulate images on upload.

Open test folder and run index.html:

Verot Image Resize in PHP

On the index page, there are many options.

If you want to resize the image, go to image local manipulation. Put your image name and press process.

Output:

Verot Image Manipulation in PHP

Conclusion

As we can see, verot manipulated our image in many different sizes and saved the images in the tmp directory.

The upload.php in the test folder is doing all these operations. We can use code from upload.php for any particular activity.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Image