PHP Image Compression before Uploading

PHP Image Compression before Uploading

Today, We want to share with you PHP Image Compression before Uploading.
In this post we will show you php image compression library, hear for php reduce image size before upload we will give you demo and example for implement.
In this post, we will learn about image compression algorithm in php with an example.

Reduce Image Size before Uploading in PHP

  1. index.php
  2. upload.js
  3. compress_upload.php

Step 1: Make Image Upload Form

First I shall make image HTML input File upload form HTML in index.php root file. We will Manage image upload in compress_upload.php when form submitted on Server Side.

<div class="container pakainfo">
<h2>PHP Image Compression before Uploading</h2>
<form method="post" name="upload_form" id="upload_form" enctype="multipart/form-data" action="compress_upload.php">
<label>Choose Images to Upload</label>

<div class="file_uploading hidden">
<label> </label>
<img src="uploading.gif" alt="Loading......" />
</div>
</form>
<div id="img_upload_display"></div>
</div>

Step 2: Handle Image Upload Form Submit using jQuery

As we will handle form submit using jQuery Form plugin, so we need to include plugin files.


Step 3: Handle Image Upload Form Submit using jQuery

We will handle image upload form submit using jQuery change event.

$(document).ready(function(){
$('#image_file').on('change',function(){
$('#upload_form').ajaxForm({
target:'#img_upload_display',
beforeSubmit:function(e){
$('.file_uploading').show();
},
success:function(e){
$('.file_uploading').hide();
},
error:function(e){
}
}).submit();
});
});

Step 4: Handle Image Upload and Image Compress

And then Lastly Full Source code, I shall Manage image upload with original file To compress functionality in compress_upload.php. I have made simple PHP compressImage() function to compress all these Types “JPG, GIF or PNG” of the image files.

I have simple passed original image good quality value in image step by step compress basic PHP methods.

The original image high quality value get can be change simple same to image quality need requirement.In this example source code, I am uploading original image file and after that this images compress that image file and here extension Like as renamed with “min-“ in same folders.


PHP or jQuery Image Compression Examples

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Image Compression, Resize & Optimization.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   How to create Controller in Laravel?
  2.   PHP Laravel 6 SweetAlert jQuery Ajax Delete Rows Example
  3.   Laravel Drop hascolumn if exists in Migration
  4.   PHP Sending Mail Contact Form with jQuery AJAX
  5.   jQuery Ajax Call to Laravel Script with JSON Return
  6.   PHP Stripe api Send Accept Payment Integration
  7.   Full form of Domain Name Extensions
  8.   PHP MySQL LIKE operator string Function
  9.   Create Dynamic Sitemap Using PHP with Mysql
  10.   File Upload - Auto Resize Compress-Reduce image using PHP