Resize Images in PHP using TinyPNG

Image resizing is a very common task for developers. Every programming language has its own libraries or techniques to resize images. In this article, we study how to resize images in PHP using a service called TinyPNG.

Note: As the name contains ‘PNG’ in ‘TinyPNG’, that doesn’t mean they allow only png images. ‘TinyPNG’ is the brand name. They provide support for all kinds of images.

Why Should Resize Images?

When it comes to a website, the size of an image really matters. If your container size for an image is around 200*200 px. Then fitting an image of size 500*500 px in a small container(200*200 px) is not a good idea. 

In such a case, you should resize your image according to your container size. By resizing images, you not only reduce your file size but also help to load your page quickly.

If you are using large images, it increases the load on your site. Slow site speed damages your search rankings.

Why use TinyPNG to Resize Images?

There are few libraries available in PHP which perform well in terms of image resizing.

Then the question is why should I use TinyPNG to resize the images?

Well, there are a few reasons for using TinyPNG to resize the images.

  • TinyPNG shrink images for your website. As a result, your images will use less bandwidth and load faster.
  • You don’t need to install any extension apart from the TinyPNG PHP library.
  • You need to send an image to TinyPNG through the API. The image will be processed on their server and you receive the desired version of the image.

Convinced? Let’s see now how to resize images in PHP using TinyPNG.

Installation

The installation process of the TinyPNG library is quite easy. Open the command prompt in your project directory and run the command below.

composer require tinify/tinify

To run the above command, you should have installed Composer on your computer.

Next, you need an API key which you can get from https://tinypng.com/developers.

All you need to do is just enter your name and email address. You will receive an email regarding the API key.

TinyPNG API key

TinyPNG allows 500 images free per month for compression. Resizing counts as one additional compression. This limit is enough for small websites. If you need more than 500 images then check out their pricing.

Resize Images in PHP using TinyPNG

At this stage, you have installed the library and are ready with your API key. Now, to resize the image, we need to write a code. Let’s create the HTML form with 2 inputs – a file and a button.

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

Upon submission, we upload the optimized version of the image on the server. Then we will take this image, resize it and store it with a different name. In the end, we will have 2 images – original and resize one.

The code for it is as follows. I am making a thumbnail of width 150px. The height will be calculated proportionally.

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

if ( isset($_POST['submit']) ) {
    // upload optimized image
    $source = \Tinify\fromFile($_FILES['image']['tmp_name']);
    $source->toFile($_FILES['image']['name']);

    // resize image
    $source = \Tinify\fromFile($_FILES['image']['name']);
    $resized = $source->resize(array(
        "method" => "scale",
        "width" => 150
    ));
    $resized->toFile('150-'.$_FILES['image']['name']);

    echo "Image stored successfully.";
}
?>

TinyPNG provides 2 more methods apart from ‘scale’. Those methods are ‘fit’ and ‘cover’.

Here is the description from the TinyPNG website for all 3 methods.

  • scale: Scales the image down proportionally. You must provide either a target ‘width’ or a target ‘height’, but not both. The scaled image will have exactly the provided width or height.
  • fit: Scales the image down proportionally so that it fits within the given dimensions. You must provide both a ‘width’ and a ‘height’. The scaled image will not exceed either of these dimensions.
  • cover: Scales the image proportionally and crops it if necessary so that the result has exactly the given dimensions. You must provide both a ‘width’ and a ‘height’. Which parts of the image are cropped away is determined automatically. An intelligent algorithm determines the most important areas and leaves these intact.

I have written a code with the method ‘scale’ and as mentioned I have passed the ‘width’ parameter. You can give a try to the other methods ‘fit’ and ‘cover’.

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

2 thoughts on “Resize Images in PHP using TinyPNG

  1. This is very nice post. It is also very helpful for us. I have been searching types of posts. Some days ago I read an article about post. But this better than post.

  2. No I’m not convinced – you didn’t at all mention the restrictions…

    “You only pay for what you use. The first 500 compressions each month are free. You will only be billed if you compress more than 500 images”

Leave a Reply

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