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 in 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
- How to Optimize Image On Upload in PHP
- Upload and Compress Multiple Images in PHP
- A Guide to Upload and Compress Images in Laravel
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
I suggest one more powerful plugin to optimize image and speedup your site,
Just see here: https://wordpress.org/plugins/way2enjoy-compress-images/
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
On TinyPNG website they provided steps if one don’t want to use composer. However, i have sent a sample code on your email.
Hello Sajid
Plz share sample code of tinypng with php
I have added a link for sample code in the post content. Please download the sample code.
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.
Hey which version of internet explorer you are using? I can see my site on internet explorer with no issues.
Wonderful website. A lot of useful info here. I’m sending it to several friends ans
also sharing in delicious. And obviously,
thank you to your sweat!
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!
You can give a try to Yoast SEO. I am using it on my site.
https://wordpress.org/plugins/wordpress-seo/
Hello, i believe that i saw you visited my blog
so i got here to go back the prefer?.I’m attempting to to find issues
to improve my web site!I suppose its ok to use some of
your concepts!!
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!
Thanks for appreciation.
Do you have any video of that? I’d want to find out more details.
I am creating YouTube video for it. I will post it soon.
I have great results using ShortPixel Image Optimizer.
Oh Really. Thanks for sharing. I didn’t know about ShortPixel Image Optimizer. I will read it.