Tinify API
Facade of Tinify API for Yii2 Framework. This extension allows you to resize and compress images without loss of quality. For more information you can read official API documentation for PHP.
Installation ¶
Install package ¶
Run command
$ composer require vintage/yii2-tinify
or add
"vintage/yii2-tinify": "~2.0"
to the require section of your composer.json
file.
Usage ¶
Component ¶
- Configure API token in app params with key
tinify-api-token
or inUploadedFile
component - Use
\vintage\tinify\UploadedFile
instead of\yii\web\UploadedFile
If you need to save some metadata, for example location
, you can configure saveMetadata
option like follow
use vintage\tinify\UploadedFile;
$file = UploadedFile::getInstance($model, 'image');
$file->saveMetadata = UploadedFile::METADATA_LOCATION;
// or more items
$file->saveMetadata = [UploadedFile::METADATA_LOCATION, UploadedFile::METADATA_CREATION];
Resizing ¶
You can resize uploaded file
$file = \vintage\tinify\UploadedFile::getInstance($model, 'image');
$file->resize() // creates \vintage\tinify\components\TinifyResize object
->fit() // resize algorithm, also you can use scale() and cover()
->width(600) // set image width
->height(400) // set image height
->process(); // resize image
$file->saveAs('@webroot/uploads');
or resize existing image
(new \vintage\tinify\components\TinifyResize('@webroot/uploads/image.jpg'))
->scale()
->width(600)
->process();
CLI ¶
- Configure console controller in
console/config/main.php
'controllerMap' => [
// ...
'tinify' => \vintage\tinify\cli\TinifyController::class,
],
Run in console
./yii tinify/<command>
Command Description $ ./yii tinify/test-connect [api-token]
Test connection to API $ ./yii tinify/compress '/path/to/src.jpg' '/path/to/dest.jpg'
Compress image $ ./yii tinify/compress-catalog '/path/to/catalog'
Compress all images in catalog $ ./yii tinify/count
Display compression images count
Tests ¶
You can run tests with composer command
$ composer test
or using following command
$ codecept build && codecept run
Licence ¶
This project is released under the terms of the BSD-3-Clause license.
Copyright (c) 2017, Vintage Web Production
Cannot use yii\base\Object as Object because 'Object' is a special class name
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.