TinyPNG – Compress Images Using PHP

Are you facing a site speed problem? You should then consider optimizing your website images. Heavy images slow down the speed of your website. So what’s the solution? Optimize the images. TinyPNG is a popular service to compress images. In this post, I will show you how to compress images using TinyPNG and PHP.

For getting started, you need to get an API key by registering with your name and email address.

Next, open the command prompt in your project root directory and run the command:

composer require tinify/tinify

It will install the TinyPNG library in your project so you can call their APIs. Basically, you need to send your image to the TinyPNG service, in return they provide an optimized version of the image.

Note: TinyPNG allows you to compress 500 images free per month. For more than 500 images you need to pay for their service.

Compress Images On Upload

For developers, the best practice is optimizing images at the time of uploading them on the server. By doing this, you don’t need to invest extra time in compressing images separately.

Use the code below which will take the uploaded image, send it to TinyPNG, and store the optimized version of the image on the disk.

<?php
require_once("vendor/autoload.php");
 
\Tinify\setKey("TINYPNG_API_KEY");

if (isset($_POST['submit'])) {
    $source = \Tinify\fromFile($_FILES['image']['tmp_name']);
    $source->toFile($_FILES['image']['name']);
    echo "Image optimized successfully.";
}
?>

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

Run this code and you will see a compressed image stored on your server.

Compress Images in Bulk

It may be possible, you already have un-optimized images on your server and you now want them optimized in bulk.

As an example, let’s say your images are in an ‘uncompressed’ folder. And you want to store optimized images into the ‘compressed’ directory.

Create the index.php file and add the below code in it.

<?php
set_time_limit(0);
 
require_once("vendor/autoload.php");
 
\Tinify\setKey("TINYPNG_API_KEY");
 
$dir = 'uncompressed/';
$images = scandir($dir);
$images = array_diff($images, array('.', '..'));
 
foreach ($images as $image) {
    $source = \Tinify\fromFile($dir.$image);
    $source->toFile("compressed/".$image);   
}
 
echo "All images are compressed.";

Replace the placeholder TINYPNG_API_KEY with your actual key. Here, we are storing optimized images in a ‘compressed’ folder. If you want to replace the original images with the optimized ones then change the below line:

$source->toFile("compressed/".$image);

With

$source->toFile($dir.$image);

That’s it! Go ahead and run this code on the browser. It will compress your images and your site performance should improve. Share 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.

17 thoughts on “TinyPNG – Compress Images Using PHP

  1. Hi Man, One question, If I want to compress without composer? i.e. install lib manually . I follow the official documentation but dont run.

    Can you help me ?

    Chears

    1. On TinyPNG website they provided steps if one don’t want to use composer. However, i have sent a sample code on your email.

  2. Hi, Neat post. There is a problem with your site in internet explorer, would check this… IE still is the market leader and a big portion of people will miss your magnificent writing due to this problem.

    1. Hey which version of internet explorer you are using? I can see my site on internet explorer with no issues.

  3. Hello there! Do you know if they make any plugins to help with SEO?
    I’m trying to get my blog to rank for some targeted keywords but
    I’m not seeing very good gains. If you know of any please share.
    Cheers!

  4. I was recommended this blog by my cousin. I’m not sure whether this
    post is written by him as nobody else know such detailed about my problem.
    You’re amazing! Thanks!

Leave a Reply

Your email address will not be published.