Compress Images for Website using reSmush.it in PHP

Images make your website beautiful and add a better user experience for visitors. However, images can also affect the site’s performance and make your site speed insanely slow. And we all know slower websites can’t succeed on the Internet.

In this article, we show you how to compress images for websites using reSmush.it in PHP. Using this service users can reduce the size of their website images. It will help in improving page speed. The reSmush.it is a free image compressor tool. Users can compress as many images as they wish. There is no such limit on the number of images.

At the time of writing this article, reSmush.it compresses more than 7 billion images so far and still counting. This number proves it’s a quite popular service among users.

Getting Started

Let’s create a simple form where the user can upload the image. Upon submitting the image, using PHP we will send the image to the reSmush.it services and receives the optimized version of our image.

<form method="post" enctype="multipart/form-data">
    <p><input type="file" name="image" accept="image/*" /></p>
    <input type="submit" name="submit" value="Submit">
</form>

Here we added a file input and submit button. We also used an attribute accept="image/*" which allows you to select only image files.

Compress Images for Website in PHP

Now, we have to interact with the reSmush.it API. We are going to write a code that performs the following steps.

  • Upload the image on our server.
  • Send the uploaded image to the reSmush.it API endpoint.
  • Receives the path of an optimized image.
  • Store this optimized version on our server by replacing the original image.

Before proceeding make sure you have enabled the cURL extension on your server.

<?php
if (isset($_POST['submit'])) {
 
    //allowed file types
    $arr_file_types = ['image/png', 'image/gif', 'image/jpg', 'image/jpeg'];
 
    if (!(in_array($_FILES['image']['type'], $arr_file_types))) {
 
        die('Only image is allowed!');
    }
 
    if (!file_exists('uploads')) {
        mkdir('uploads', 0777);
    }
 
    move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/' . $_FILES['image']['name']);
 
    // optimize image using reSmush.it
    $file = getcwd(). '/uploads/' . $_FILES['image']['name'];
    $mime = mime_content_type($file);
    $info = pathinfo($file);
    $name = $info['basename'];
    $output = new CURLFile($file, $mime, $name);
    $data = array(
        "files" => $output,
    );
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://api.resmush.it/?qlty=80');
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
       $result = curl_error($ch);
    }
    curl_close ($ch);
 
    $arr_result = json_decode($result);
 
    // store the optimized version of the image
    $ch = curl_init($arr_result->dest);
    $fp = fopen(getcwd(). '/uploads/'. $name, 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
 
    echo "File uploaded successfully.";
}
?>

In the above code, I set the quality value to ’80’ which is recommended. You can adjust this number to more or less. We are storing images inside the uploads directory. In your case, you can adjust this directory.

I hope you understand how to compress images for the website in PHP using reSmush.it. I would like to hear your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

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