The Tinify API allows you to compress and optimize WebP, JPEG and PNG images.

Related tags

Imagery tinify
Overview

Tinity PHP

The Tinify API allows you to compress and optimize WebP, JPEG and PNG images. It is designed as a REST service. The client libraries in various languages make it very easy to interact with the Tinify API.

installation

You can use the PHP client by installing the Composer package and adding it to your application’s dependencies:

composer require devscast/tinify

Authentication

To use the API you must provide your API key. You can get an API key by registering with your name and email address. Always keep your API key secret!

use Devscast\Tinify\Client;

$tinify = new Client('yourtinifytoken');

All requests will be made over an encrypted HTTPS connection.

You can instruct the API client to make all requests over an HTTP proxy. Set the URL of your proxy server, which can optionally include credentials.

use Devscast\Tinify\Client;

$tinify = new Client(
    token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    proxy: 'http://user:[email protected]:8080'
);

Compressing Images

You can upload any WebP, JPEG or PNG image to the Tinify API to compress it. We will automatically detect the type of image and optimise with the TinyPNG or TinyJPG engine accordingly. Compression will start as soon as you upload a file or provide the URL to the image. You can choose a local file as the source and write it to another file.



use Devscast\Tinify\Client;

$tinify = new Client(token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

$tinify->toFile(
    source: $tinify->fromFile('/home/tinify/pictures/test.png'),
    path: '/home/tinify/pictures/test-compressed.png'
);

You can also upload an image from a buffer (a string with binary) and get the compressed image data.

toBuffer(source: $tinify->fromBuffer($data)); ">


$sourceBuffer = file_get_contents("unoptimized.jpg");
$compressedBuffer = $tinify->toBuffer(source: $tinify->fromBuffer($data));

You can provide a URL to your image instead of having to upload it.



$tinify->toFile(
    source: $tinify->fromUrl('https://tinypng.com/images/panda-happy.png'),
    path: '/home/tinify/pictures/test-compressed.png'
);

Resizing images

Use the API to create resized versions of your uploaded images. By letting the API handle resizing you avoid having to write such code yourself and you will only have to upload your image once. The resized images will be optimally compressed with a nice and crisp appearance.

You can also take advantage of intelligent cropping to create thumbnails that focus on the most visually important areas of your image.

Resizing counts as one additional compression. For example, if you upload a single image and retrieve the optimized version plus 2 resized versions this will count as 3 compressions in total.

$tinify->toFile(
    source: $tinify->resize(
        source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
        method: 'fit',
        width: 500,
        height: 500
    ),
    path: '/home/bernard-ng/tinify/test-compressed-resized.png'
);

The method describes the way your image will be resized. The following methods are available:

  • 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 of your image.

  • thumb : A more advanced implementation of cover that also detects cut out images with plain backgrounds. The image is scaled down to the width and height you provide. If an image is detected with a free standing object it will add more background space where necessary or crop the unimportant parts.

If the target dimensions are larger than the original dimensions, the image will not be scaled up. Scaling up is prevented in order to protect the quality of your images.

Preserving metadata

You can request that specific metadata is copied from the uploaded image to the compressed version. Preserving copyright information, the GPS location and the creation date are currently supported. Preserving metadata adds to the compressed file size, so you should only preserve metadata that is important to keep.

Preserving metadata will not count as an extra compression. However, in the background the image will be created again with the additional metadata.

$tinify->toFile(
    source: $tinify->preserve(
        source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
        metadata: ['creation', 'copyright']
    ),
    path: '/home/bernard-ng/dev/projects/tinify-php/data/test-compressed.png'
);

You can provide the following options to preserve specific metadata. No metadata will be added if the requested metadata is not present in the uploaded image.

  • copyright : Preserves any copyright information. This includes the EXIF copyright tag (JPEG), the XMP rights tag (PNG) as well as a Photoshop copyright flag or URL. Uses up to 90 additional bytes, plus the length of the copyright data.

  • creation : Preserves any creation date or time. This is the moment the image or photo was originally created. This includes the EXIF original date time tag (JPEG) or the XMP creation time (PNG). Uses around 70 additional bytes.

  • location (JPEG only) : Preserves any GPS location data that describes where the image or photo was taken. This includes the EXIF GPS latitude and GPS longitude tags (JPEG). Uses around 130 additional bytes.

Saving to Amazon S3

You can tell the Tinify API to save compressed images directly to Amazon S3. If you use S3 to host your images this saves you the hassle of downloading images to your server and uploading them to S3 yourself.

$source = $tinify->toCloud(
    source: $tinify->fromFile('/home/tinify/pictures/test.png'),
    bucket_path: 'tinify/images/test.png',
    storage: new Aws(
        region: 'ap-south-1',
        secret_access_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        access_key_id: 'xxxxxxxxxxxxxxx',
        option: ['headers' => ['Cache-Control' => 'max-age=31536000, public']] // optional
    )
);

You need to provide the following options in order to save an image on Amazon S3:

  • aws_access_key_id

  • aws_secret_access_key : Your AWS access key ID and secret access key. These are the credentials to an Amazon AWS user account. Find out how to obtain them in Amazon’s documentation. The user must have the correct permissions, see below for details.

  • region : The AWS region in which your S3 bucket is located.

  • path : The path at which you want to store the image including the bucket name. The path must be supplied in the following format: / / .

  • headers (experimental) : You can add a Cache-Control header to control browser caching of the stored image, with for example: public, max-age=31536000. The full list of directives can be found in the MDN web docs.

The user that corresponds to your AWS access key ID must have the PutObject and PutObjectAcl permissions on the paths of the objects you intend to create.

Saving to Google Cloud Storage

You can tell the Tinify API to save compressed images directly to Google Cloud Storage. If you use GCS to host your images this saves you the hassle of downloading images to your server and uploading them to GCS yourself.

Before you can store an image in GCS you will need to generate an access token with a service account.

$tinify->toCloud(
    source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
    bucket_path: 'tinify/images/test.png',
    storage: new Gcs(
        access_token: 'XXXXXXXXXXXXXXXXXXXXXXXXX',
        option: ['headers' => ['Cache-Control' => 'max-age=31536000, public']] // optional
    )
);

You need to provide the following options in order to save an image on Google Cloud Storage:

  • gcp_access_token : The access token for authenticating to Google's Cloud Platform. Find out how to generate these tokens with the example above.

  • path : The path at which you want to store the image including the bucket name. The path must be supplied in the following format: / / .

  • headers (experimental) : You can add a Cache-Control header to control browser caching of the stored image, with for example: public, max-age=31536000. The full list of directives can be found in the MDN web docs.

Compression count

The API client automatically keeps track of the number of compressions you have made this month. You can get the compression count after you have validated your API key or after you have made at least one compression request.



$source = $tinify->fromUrl('https://tinypng.com/images/panda-happy.png');
$source->getCompressionCount();

$tinify->toFile($source, path: '/home/tinify/pictures/test-compressed.png');

acknowledgement

this package is a reimplementation of the tinify/tinify-php library, supporting PHP 8, rewritten with a design that removes static calls for a more object-oriented approach

You might also like...
Simple, fast and secure archive for images
Simple, fast and secure archive for images

Slim Image Archive Simple*, fast and secure archive for images: Create multiple categories with multiple albums with multiple images! Manage multiple

GifCreator is a PHP class that creates animated GIF from multiple images

================================ GifCreator ================================ GifCreator is a PHP class to create animated GIF from multiple images For

Create images with embedded text using advanced typography

Image with Text This class makes it super easy to render images with multiple, independently styled text blocks. You can control each text block's ali

Easily convert images with Glide
Easily convert images with Glide

Easily convert images with Glide This package provides an easy to use class to manipulate images. Under the hood it leverages Glide to perform the man

Upload SVG images in Magento 2.x

Upload SVG images in Magento 2.x This extension for Magento 2 allows uploading SVG images in the following sections: wysiwyg editor in static blocks a

Inline Images Protocol implementation for PHP.
Inline Images Protocol implementation for PHP.

Imgecho Echo the image on iTerm App using Inline Images Protocol. Installation Use Composer to install. composer require mileschou/imgecho Usage Use

Create beautiful generative background images from a string. Based on jasonlong/geo_pattern

GeoPattern This is a PHP port of jasonlong/geo_pattern. Generate beautiful tiling SVG patterns from a string. The string is converted into a SHA and a

Contao extension to provide the possibility of defining alternative images to be used on different output devices.
Contao extension to provide the possibility of defining alternative images to be used on different output devices.

Contao Image Alternatives This extensions expands the capabilities of using responsive images with art direction in Contao. You will have the possibil

A great looking and easy-to-use photo-management-system you can run on your server, to manage and share photos.
A great looking and easy-to-use photo-management-system you can run on your server, to manage and share photos.

Lychee A great looking and easy-to-use photo-management-system. Since the 1st of April 2018 this project has moved to it's own Organisation (https://g

Releases(2.0.0)
Owner
Devscast
The boost you need
Devscast
Optimize your images on the fly with Glide for Laravel.

Glide for Laravel Optimize your images on the fly with Glide for Laravel. Support us Like our work? You can support us by purchasing one of our produc

Flowframe 53 Oct 17, 2022
PHP Exif Library - library for reading and writing Exif headers in JPEG and TIFF files using PHP.

PEL: PHP Exif Library README file for PEL: PHP Exif Library. A library with support for reading and writing Exif headers in JPEG and TIFF images using

null 264 Dec 4, 2022
A simple page view counter that store data as text and shows data as a PNG image

Image Counter A simple page view counter that store data as text and shows the counter as a PNG image.

Victor Ribeiro 10 Apr 19, 2022
Convert image types. Take samples of pdfs or psd, do some filters as SEPIA, resizes. Save to WEBP

img2img. PHP Converter, sampler of pdfs & psd, do resizes & some filters in images as SEPIA. Save to WEBP V.1.0.3 This class in pure PHP try to manage

Rafael Martin Soto 9 Oct 23, 2022
Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file, move it to a new directory, and returns the new source for the image.

NO LONGER MAINTAINED!!! Image Cache v. 1.0.0 Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file,

Erik Nielsen 455 Dec 30, 2022
The image server plugin allows you to add responsive images via config without extra elements

Grav image server plugin adds picture tag with loading, responsive, high density images

catchIT 7 Dec 30, 2022
ImageWorkshop is a PHP5.3+ library that helps you to manage images based on GD library

================================ ImageWorkshop class ================================ Summary and features Really flexible and easy-to-use PHP class t

Clément Guillemain 853 Dec 27, 2022
A PHP GD + TwitterOAuth demo to dynamically generate Twitter header images and upload them via the API.

A PHP GD + TwitterOAuth demo to dynamically generate Twitter header images and upload them via the API. This enables you to build cool little tricks, like showing your latest followers or sponsors, latest content creted, a qrcode to something, a progress bar for some goal, and whathever you can think of.

Erika Heidi 172 Jan 5, 2023
The Gregwar\Image class purpose is to provide a simple object-oriented images handling and caching API

Gregwar's Image class The Gregwar\Image class purpose is to provide a simple object-oriented images handling and caching API. Installation With compos

Grégoire Passault 958 Dec 29, 2022
PHP library to resize, scale and crop images.

PHP library to resize, scale and crop images.

Gumlet 1.1k Jan 3, 2023