2017 © Pedro Peláez
 

library web-thumbnailer

PHP library which will retrieve a thumbnail for any given URL

image

arthurhoaro/web-thumbnailer

PHP library which will retrieve a thumbnail for any given URL

  • Tuesday, July 17, 2018
  • by ArthurHoaro
  • Repository
  • 1 Watchers
  • 9 Stars
  • 260 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 70 % Grown

The README.md

Web Thumbnailer

Coverage Status Scrutinizer Code Quality, (*1)

PHP library which will retrieve a thumbnail for any given URL, if available., (*2)

Features

  • Support various specific website features: Imgur, FlickR, Youtube, XKCD, etc.
  • Work with any website supporting OpenGraph (tag meta og:image)
  • Or use direct link to images
  • Local cache
  • Resizing and/or cropping according to given settings

Requirements

Mandatory:, (*3)

  • PHP 7.1 (v2.0.0+) - PHP 5.6 (v1.x.x)
  • PHP GD extension

(Highly) Recommended:, (*4)

  • PHP cURL extension: it let you retrieve thumbnails without downloading the whole remote page

Installation

Using Composer:, (*5)

composer require arthurhoaro/web-thumbnailer

Usage

Using WebThumbnailer is pretty straight forward:, (*6)

require_once 'vendor/autoload.php';

use WebThumbnailer\WebThumbnailer;

$wt = new WebThumbnailer();

// Very basic usage
$thumb = $wt->thumbnail('https://github.com/ArthurHoaro');

// Using a bit of configuration
$thumb2 = $wt->maxHeight(180)
            ->maxWidth(320)
            ->crop(true)
            ->thumbnail('https://github.com/ArthurHoaro');
echo '<img src="'. $thumb .'">';
echo '<img src="'. $thumb2 .'">';

// returns false
$wt->thumbnail('bad url');

Result:, (*7)

, (*8)

Thumbnail configuration

There are 2 ways to set up thumbnail configuration:, (*9)

  • using WebThumbnailer helper functions as shown in Usage section.
  • passing an array of settings to thumbnail() while getting a thumbnail. This will override any setting setup with the helper functions. Example:
$conf = [
    WebThumbnailer::MAX_HEIGHT => 320,
    WebThumbnailer::MAX_WIDTH => 320,
    WebThumbnailer::CROP => true
];
$wt->thumbnail('https://github.com/ArthurHoaro', $conf);

Download mode

There are 3 download modes, only one can be used at once:, (*10)

  • Download (default): it will download thumbnail, resize it and save it in the cache folder.
  • Hotlink: it will use image hotlinking if the domain authorize it, download it otherwise.
  • Hotlink strict: it will use image hotlinking if the domain authorize it, fail otherwise.

Usage:, (*11)

// Download (default value)
$wt = $wt->modeDownload();
$conf = [WebThumbnailer::DOWNLOAD];
// Hotlink
$wt = $wt->modeHotlink();
$conf = [WebThumbnailer::HOTLINK];
// Hotlink Strict
$wt = $wt->modeHotlinkStrict();
$conf = [WebThumbnailer::HOTLINK_STRICT];

Warning: hotlinking means that thumbnails won't be resized, and have to be downloaded as their original size., (*12)

Image Size

In download mode, thumbnails size can be defined using max width/height settings:, (*13)

  • with max height and max width, the thumbnail will be resized to match the first reached limit.
  • with max height only, the thumbnail will be resized to the given height no matter its width.
  • with max width only, the thumbnail will be resized to the given width no matter its height.
  • if no size is provided, the default settings will apply (see Settings section).

Usage:, (*14)

// Sizes are given in number of pixels as an integer
$wt = $wt->maxHeight(180);
$conf = [WebThumbnailer::MAX_HEIGHT => 180];
$wt = $wt->maxWidth(320);
$conf = [WebThumbnailer::MAX_WIDTH => 180];

Bonus feature: for websites which support an open API regarding their thumbnail size (e.g. Imgur, FlickR), WebThumbnailer makes sure to download the smallest image matching size requirements., (*15)

Image Crop

Image resizing might not be enough, and thumbnails might have to have a fixed size. This option will crop the image (from its center) to match given dimensions., (*16)

Note: max width AND height must be provided to use image crop., (*17)

Usage:, (*18)

// Sizes are given in number of pixels as an integer
$wt = $wt->crop(true);
$conf = [WebThumbnailer::CROP => true];

Resize Mode

This setting choose whether to use imagecopyresized (RESIZE mode) or imagecopyresampled (RESAMPLE mode):, (*19)

  • RESAMPLE: higher CPI usage, with better resized image rendering.
  • RESIZE: faster and lower CPU usage, but the resized image might not look as good.

By default, this library uses RESAMPLE setting since the setting was introduced., (*20)

Example:, (*21)

|---------------------RESIZE---------------------|---------------------RESAMPLE---------------------|, (*22)

, (*23)

Usage:, (*24)

// Resize
$wt = $wt->resize();
$conf = [WebThumbnailer::RESIZE_MODE => WebThumbnailer::RESIZE];

// Resample
$wt = $wt->resample();
$conf = [WebThumbnailer::RESIZE_MODE => WebThumbnailer::RESAMPLE];

Miscellaneous

  • NOCACHE: Force the thumbnail to be resolved and downloaded instead of using cache files.
  • DEBUG: Will throw an exception if an error occurred or if no thumbnail is found, instead of returning false.
  • VERBOSE: Will log an entry in error log if a thumbnail could not be retrieved.
  • DOWNLOAD_TIMEOUT: Override download timeout setting (in seconds).
  • DOWNLOAD_MAX_SIZE: Override download max size setting (in bytes).

Usage:, (*25)

$wt = $wt
    ->noCache(true)
    ->debug(true)
    ->verbose(true)
    ->downloadTimeout(30)
    ->downloadMaxSize(4194304)
;
$conf = [
    WebThumbnailer::NOCACHE => true,
    WebThumbnailer::DEBUG => true,
    WebThumbnailer::VERBOSE => true,
    WebThumbnailer::DOWNLOAD_TIMEOUT => 30,
    WebThumbnailer::DOWNLOAD_MAX_SIZE => 4194304,
];

Settings

Settings are stored in JSON, and can be overrode using a custom JSON file:, (*26)

use WebThumbnailer\Application\ConfigManager;

ConfigManager::addFile('conf/mysettings.json');

Available settings:, (*27)

  • default:
    • download_mode: default download mode (DOWNLOAD, HOTLINK or HOTLINK_STRICT).
    • timeout: default download timeout, in seconds.
    • max_img_dl: default download max size, in bytes.
    • max_width: default max width if no size requirement is provided.
    • max_height: default max height if no size requirement is provided.
    • cache_duration: cache validity duration, in seconds (use a negative value for infinite cache).
  • path:
    • cache: cache path.
  • apache_version: force .htaccess syntax depending on Apache's version, otherwise it uses mod_version (allowed values: 2.2 or 2.4).

Thumbnails path

In download mode, the path to the thumbnail returned by WebThumbnailer library will depend on what's provided to the path.cache setting. If an absolute path is set, thumbnails will be attached to an absolute path, same for relative., (*28)

Relative path will depend on the entry point of the execution. For example, if your entry point for all request is an index.php file in your project root directory, the default cache/ setting will create a cache/ folder in the root directory. Another example, for Symfony, the cache folder will be relative to the web/ directory, which is the entry point with app.php., (*29)

If you don't have a single entry point in your project folder structure, you should provide an absolute path and process the path yourself., (*30)

Contributing

WebThumbnailer can easily support more website by adding new rules in rules.json using one of the default Finders, or by writing a new Finder for specific cases., (*31)

Please report any issue you might encounter., (*32)

Also, feel free to correct any horrible English mistake I may have made in this README., (*33)

License

MIT license, see LICENSE.md, (*34)

The Versions

17/07 2018

dev-master

9999999-dev

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

 

The Development Requires

17/07 2018

v1.2.1

1.2.1.0

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

 

The Development Requires

17/07 2018

dev-hotfix/dl-mode-json

dev-hotfix/dl-mode-json

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

 

The Development Requires

30/06 2018

v1.2.0

1.2.0.0

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

 

The Development Requires

13/06 2018

v1.1.3

1.1.3.0

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

13/06 2018

v1.1.4

1.1.4.0

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

05/05 2018

v1.1.2

1.1.2.0

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

05/05 2018

dev-hotfix/subdirs

dev-hotfix/subdirs

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

01/05 2018

v1.1.1

1.1.1.0

PHP library which will retrieve a thumbnail for any given URL

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

01/05 2018
18/04 2018

dev-scrutinizer-refactoring

dev-scrutinizer-refactoring

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

18/04 2018

dev-curl-partial

dev-curl-partial

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

05/04 2018

dev-coverage

dev-coverage

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

11/11 2017

v1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

11/11 2017

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires