snappyimg/snappyimg-php

Scale, crop and optimize images on-the-fly,

v1.0 2018-09-20 15:46 UTC

This package is auto-updated.

Last update: 2024-04-12 04:48:57 UTC


README

logo.png

Build Status

This is a PHP implementation of Snappyimg URL builder. For more information about the service, go to https://www.snappyimg.com/.

Installation

This library is available as Composer package:

$ composer require snappyimg/snappyimg-php

Demo

Processed original image https://www.snappyimg.com/demo.jpg into 640×360: https://serve.snappyimg.com/snappyimg/pvvJK7Di3E1Fjetx9viameQMNo0LjHcM2rUh8v10En0/fit/640/360/ce/1/aHR0cHM6Ly93d3cuc25hcHB5aW1nLmNvbS9kZW1vLmpwZw.jpg

68747470733a2f2f73657276652e736e61707079696d672e636f6d2f736e61707079696d672f7076764a4b374469334531466a657478397669616d65514d4e6f304c6a48634d3272556838763130456e302f6669742f3634302f3336302f63652f312f6148523063484d364c7939336433637563323568634842356157316e4c6d4e766253396b5a5731764c6d70775a772e6a7067

Example usage

$snappy = new Snappyimg($appToken, $appSecret, Snappyimg::STAGE_DEMO);

$options = Options::fromDefaults(360, 420);
$url = $snappy->buildUrl($options, 'https://www.snappyimg.com/demo.jpg');

Where $appToken and $appSecret are generated for you when you register at snappyimg.com.

<img alt="Snappyimg Example" src="{{$url}}">

While STAGE_DEMO is available for free for all users, you will need a subscription to use STAGE_SERVE.

Additional options

The Options class lets you specify exactly how the image should be processed.

$options = Options::fromDefaults(360, 420)
    ->setResize(Snappyimg::RESIZE_FIT)
    ->setGravity(Snappyimg::GRAVITY_SMART)
    ->setEnlarge(FALSE)
    ->setFormat(Snappyimg::FORMAT_WEBP);

The options themselves are explained at Documentation and at each method.