TinyPNG Compress Images Using PHP

Are you facing a site speed problem? You should then consider optimizing your site images. Heavy images make the site speed slow. So what’s the solution? Simple. Use the TinyPNG to compress the images. In this post, I show you how to compress images using TinyPNG.

I have built a PHP script that compresses the images in bulk using TinyPNG API. Use it and check the performance of your site. It will be better as we are reducing the image size.

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

I am assuming you have a large number of images that need to be compressed. So, create two folders ‘uncompressed’ and ‘compressed’. Keep all your images in an ‘uncompressed’ folder. We will store all optimized images inside the ‘compressed’ folder on the fly.

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.

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

Upon library installation, create index.php file and add the below code in it.




$dir = 'uncompressed/';
$images = scandir($dir);
$images = array_diff($images, array('.', '..'));
foreach ($images as $image) {
    $source = \Tinify\fromFile($dir.$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 original images with the optimized one then change the below line




That’s it! Go ahead and run your URL in the browser. It will compress your images and your site performance should improve.

I hope you understand how to compress images using TinyPNG API in PHP. 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.

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 ?


    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.

  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. Required fields are marked *