Compress Image size while Uploading with PHP

Kailash Singh

Compress Image size while Uploading with PHP

Published Mar 22,2019 by Kailash Singh

2 Comments     588 Views    


In this tutorial, we are going to learn about how to compress image size while uploading with PHP.

 

Step 1 :- Create compress_image table in database.

 

CREATE TABLE `codingmantra`.`compress_image` ( `id` INT(11) NOT NULL AUTO_INCREMENT , `image` VARCHAR(250) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;

 

Step 2 :- Create connection to database in php file.

 

The mysqli_connect() function opens a new connection to the MySQL server.

Syntax : - mysqli_connect(hostname, username, password, database);

 

<?php $con = mysqli_connect('localhost','root','','codingmantra'); ?>

 

Step 3 :- Create view to upload file.

 

The following HTML code below creates an uploader form. This form is having method attribute set to action, post and enctype attribute is set to multipart/form-data

 

<form method='post' action='' enctype='multipart/form-data'>
	<input type='file' name='image' class="form-control"><br>
	<input type='submit' value='Upload Image' class="btn btn-primary" name='upload'>    
</form>

 

Step 4 :- Create Images folder to store images

 

 

Step 5 :- write compress code 

 

Create a compressedImage() function to compress PNG, JPEG and GIF images.

 

The function takes 3 parameters :-
1. Source
2. Destination
3. File quality

 

Execute imagejpeg() method to store image to the destination.

The third parameter quality is optional. It takes value from 0 – 100 and the default value is 75.

 

<?php
    
   if(isset($_POST['upload'])){

       // Getting file name
       $filename = $_FILES['image']['name'];
         
       // Valid extension
       $valid_ext = array('png','jpeg','jpg');

			
	   $photoExt1 = @end(explode('.', $filename)); // explode the image name to get the extension
	   $phototest1 = strtolower($photoExt1);
			
	   $new_profle_pic = time().'.'.$phototest1;
			
       // Location
       $location = "images/".$new_profle_pic;

       // file extension
       $file_extension = pathinfo($location, PATHINFO_EXTENSION);
       $file_extension = strtolower($file_extension);

       // Check extension
       if(in_array($file_extension,$valid_ext)){  

            // Compress Image
            compressedImage($_FILES['image']['tmp_name'],$location,60);
				
		    //Here i am enter the insert code in the step ........

        }
	    else
        {
                echo "File format is not correct.";
        }
    }

    // Compress image
    function compressedImage($source, $path, $quality) {

            $info = getimagesize($source);

            if ($info['mime'] == 'image/jpeg') 
                $image = imagecreatefromjpeg($source);

            elseif ($info['mime'] == 'image/gif') 
                $image = imagecreatefromgif($source);

            elseif ($info['mime'] == 'image/png') 
                $image = imagecreatefrompng($source);

            imagejpeg($image, $path, $quality);

    }

 ?>

 

Step 6 :- Now we are storing the image in the database.

 

$sql = "INSERT INTO compress_image(image)VALUES ('".$new_profle_pic."')";
if (mysqli_query($con, $sql)) 
{
	echo "New record created successfully";
}

 

Hope this will help our developers.


2 Comments


Manish Kumar Apr 07, 2019

Nice Blog thanx

Elevenstech Apr 08, 2020

Thnak you so much