Giter Club home page Giter Club logo

yii2-tinify's Introduction

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.

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version Latest Unstable Version

Installation

The preferred way to install this extension is through composer.

Either run

$ composer require vintage/yii2-tinify

or add

"vintage/yii2-tinify": "~2.1"

to the require section of your composer.json.

Usage

Component

  1. Configure API token in app params with key tinify-api-token or in UploadedFile component
  2. 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];

Upload files to AWS S3 storage

  1. Configure AWS S3 service in config/params-local.php
use vintage\tinify\UploadedFileS3;

return [
    // ...
    UploadedFileS3::PARAM_KEY_AWS_ACCESS_KEY_ID         => '',
    UploadedFileS3::PARAM_KEY_AWS_SECRET_ACCESS_KEY     => '',
    UploadedFileS3::PARAM_KEY_S3_REGION                 => '',
    UploadedFileS3::PARAM_KEY_S3_BUCKET                 => '',
];
  1. Use \vintage\tinify\UploadedFileS3 insead of \vintage\tinify\UploadedFile
  2. Provide region and bucket name in methods calls
$file = UploadedFile::getInstance($model, 'image')->saveAs('image.jpg');

also you can override region and bucket

$file = UploadedFile::getInstance($model, 'image')
    ->setRegion('us-west-1')
    ->setPath('images-bucket/uploads') // path must be provided without slash in the end
    ->saveAs('image.jpg');

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

  1. Configure console controller in console/config/main.php
'controllerMap' => [
    // ...
    'tinify' => \vintage\tinify\cli\TinifyController::class,
],
  1. 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

License

This project is released under the terms of the BSD-3-Clause license.

yii2-tinify's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

yii2-tinify's Issues

Code from example doesn't work

When I try to run this example from the README on fresh yii2 instalation:

(new \vintage\tinify\components\TinifyResize('@webroot/uploads/image.jpg'))
    ->scale()
    ->width(600)
    ->process();

it throws an exception:

Tinify\AccountException
Provide an API key with Tinify\setKey(...)

here is my params config:

return [    
    'tinify-api-token' => '*******',
];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.