Compress Images before upload in PHP

Hey Friends,

We as developers usually get complaints from our client that the Media files are consuming lot of space than they should on the server resulting in choking the bandwidth of the server which in turns make website slow to load the main reason for this is “Uploading of huge media files especially images by the users on the website”. Now there are 2 scenarios by avoiding uploading of huge images on the website.

  1. First is to put the size limitation while uploading the file which is not as user-friendly since the user will have to compress the image or find some other alternative solution.
  2. The smart option is to compress the size of the image to the figure required by you automatically while uploading which will result in a smaller image acquiring less space on the server.

So today we have a quick hack on how you can compress the size of the image and upload it to the server as well as your database.

So lets start

  1. First let us create a simple form which from where we will upload the files

Filename: index.php

[cc-code]

PHP Hurdles Image Compression

PHP Hurdles Image Compression Tutorial


imagecreatefrompng to obtain image identifier from temp location

$src = imagecreatefrompng($uploadedfile);}

else {

$src = imagecreatefromgif($uploadedfile);    //else if image is of gif use imagecreatefromgif to obtain image identifier from temp location

}

list($width,$height)=getimagesize($uploadedfile);     //Get with and height of the image

$newwidth=200;                                                         //Set the new width in pixels.

$newheight=($height/$width)*$newwidth;        //Calculate new height obtained in ratio with new width

$tmp=imagecreatetruecolor($newwidth,$newheight);          //get new height and width of the image with black color

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);    //Resize the image

$filename = time().’_’.$newwidth.’_’.$newheight.’.’.$ext;

$filename1 = “images/”. $filename;  //intoduce new file name to compressed image

imagejpeg($tmp,$filename1,100);   //Move image to specified location

imagedestroy($src);       //Destroy when work done

imagedestroy($tmp);

}

//Now upload new file to the database table

$res=mysqli_query($connection,”INSERT INTO image_data(image)VALUES(‘$filename’)”);

if($res>0)

{ echo “Uploaded”;}

else

{ echo “failed”;}

}

?>

[/cc]

So this is the simple way in which you can escape from putting burdern on your server space by using above quick snippet.

For source code of the above snippet Download Here

Hope this tutorial would help our fellow developers.

Because we believe Hurdles arn’t really Hurdles.

You can also view our other trending topic which can help you

Disable Past Dates Or Upcoming Dates In PHP Datepicker

Magento Server Migration

2 Comments

  1. Jim MacDiarmid said:

    Thank you for posting this. This is very helpful.

    July 1, 2017
    Reply
    • Saurabh Dubey said:

      Thank You, Jim, We are glad to help you out.

      July 1, 2017
      Reply

Leave a Reply

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