Resize Image In PHP Using TinyPNG

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

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

Why One Should Resize Images?

When it comes to a website, the size of an image really matters. If you are using big size images, it hampers your site load. Slow site speed affects your search rankings.

If your container size for an image is around 200*200 px. Then fitting image of size 500*500 px in small container(200*200 px) is not a good idea. In such case, you should resize your image according to your small container size. Doing so, not only reduce your file size but also help to load your page quickly.

Why Should Use TinyPNG To Resize Image?

There are quite popular libraries available in PHP which performs well in terms of image resizing.

Then the question comes why should use TinyPNG to resize the image?

Well, there are 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 TinyPNG PHP library.

Convinced? Let us see step by step guide how to resize image in PHP using TinyPNG.

Set Up The Environment For TinyPNG

The installation process of PHP library provided by TinyPNG 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 has installed Composer on your computer.

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

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

TinyPNG API key

At this stage, We have installed the library and you are ready with your API key. We are completed with the setting environment for TinyPNG.

Resize Image In PHP Using TinyPNG

To resize the image, we need to write a little piece of code. Open your PHP file in your file editor. Resizing image code is as below.

require_once("vendor/autoload.php");

\Tinify\setKey("YOUR_API_KEY");

$source = \Tinify\fromFile("large.jpg"); //image to be resize
$resized = $source->resize(array(
    "method" => "fit",
    "width" => 150,
    "height" => 100
));
$resized->toFile("thumbnail.jpg"); //resized image

Above code takes an image called ‘large.jpg’. This is the image which we have to resize. Then it gives a call to API Method ‘resize’ with some required parameters like method, width, height. At last, we got the resized and compressed version of the image which saved as ‘thumbnail.jpg’.

Normally, we keep thumbnail name as ‘{filename}-thumbnail.extension’. In our case, it would be ‘large-thumbnail.jpg’. The naming convention is up to you. You can decide name format for your resized image.

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

Here are the description from 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.

We have taken an example with method ‘fit’ and as mentioned we have passed both ‘width’ and ‘height’. You can give a try to another methods ‘scale’ and ‘cover’.

We hope you understand how to resize image in PHP using Tinypng. You may also want to read our related articles below.

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

2 thoughts on “Resize Image 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 *