Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to Upload and Compress an Image using PHP

Image compression is very helpful to reduce the size of the image. Generally, the user does not optimize the image when uploading through the website. In this case, compress images before upload to optimize the image.

So, now I am show you how to compress image before upload using PHP.

Create a PHP form with an input field and submit button. This <form>
tag contain following contain:

  • method=”post”
  • enctype=”multipart/form-data

Note: enctype=”multipart/form-data” is compulsory for image upload precess via post method in form.

index.php

After the form submission, the file data is submitted to the upload.php file for further processing. Then, compress and upload images in PHP.

The upload.php file handles the image compression upload operations. I have used some PHP image functions like compressImage() that helps to compress and save image on the server using PHP.

If the file is submitted, then retrieve the file info using the PHP $_FILES method, compressed size, and upload image using compressImage() function. Then see the image upload status.

upload.php

Then, see the results:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.