Compress, Resize Image Before Upload in PHP

compress image php

Compress or resize image before upload is an important functionality for any web developer. Everyone knows that fast web page loading, so much important these days. If your website has a very high loading time then its affects performance very badly.

Generally, images took more time to load on a page, so it’s a good practice that you compress images before uploading them on the website.

To optimize the image, PHP gives you to easily implement compress / resize image functionality before upload. The image file size is reduced before uploading and it will reduce the storage uses of the server and reduce the web page loading time.

Example: Compress Image Before Upload using PHP

Let’s check it out the example of compress an image before upload in php. We will resize the image without losing image quality.

Step 1: Create File Upload Form – index.php

First, create an HTML form with file upload element.

Must add these attribute in <form> tag:

enctype=”multipart/form-data”
method=”POST”

index.php

Step-2: Compress and Upload Image with PHP

The compress / resize before upload file in PHP functionality is managed in the imageupload.php file.

imageupload.php

The above example code helps you to compress the image file without using any compression library. With this compression PHP script, you can resize different types of image files before uploading on the server.

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish