PHP library
Source code on GitHub
Package on Packagist
Installation
composer require tinytools/tiny.pictures-phpUsage
First, you need to load the library's files by using Composer's autoloader. Second, instantiate the supplied TinyPictures\TinyPictures class with an $options array.
<?php
require_once __DIR__ . '/vendor/autoload.php';
$tinyPictures = new TinyPictures\TinyPictures([
'user' => 'demo',
'namedSources' => [
'main' => 'https://tiny.pictures/'
]
]);You can then use the public functions of the instance as described in the API section below.
Options
- user (string)
- Mandatory. Your tiny.pictures user name.
- protocol ('https'|'http')
- Optional. Default
'https'. Set to'http'if you are running a non-TLS-encrypted website to save your users' devices from unnecessary computation time. - namedSources (['name' => 'url'][])
- Optional. Default
[]. An array of arrays, each one describing a named source you configured on your dashboard. Copy the value directly from there.
API
url
This function converts any image URL to a tiny.pictures URL. You can specify image processing operations in the options array.
Function parameters
- $source (string)
- Mandatory. URL of the original image.
- $options (array)
- Optional. Default
[]. Array containing the image processing operations you'd like to be applied.
Function returns
stringExamples
These examples assume that the user name is demo and there is a named source with the name main that points to https://tiny.pictures/.
$tinyPictures->url('https://tiny.pictures/example1.jpg')
// 'https://demo.tiny.pictures/main/example1.jpg'
$tinyPictures->url('https://tiny.pictures/example1.jpg', ['width' => 200])
// 'https://demo.tiny.pictures/main/example1.jpg?width=200'
$tinyPictures->url('https://tiny.pictures/example1.jpg', ['width' => 200, 'height' => 100, 'resizeType' => 'cover', 'enlarge' => true])
// 'https://demo.tiny.pictures/main/example1.jpg?width=200&height=100&resizeType=cover&enlarge=true'