2017 © Pedro Peláez
 

yii2-extension yii2-image-thumbnail

Yii2 image thumbnail component

image

sadovojav/yii2-image-thumbnail

Yii2 image thumbnail component

  • Monday, January 15, 2018
  • by sadovojav
  • Repository
  • 5 Watchers
  • 19 Stars
  • 9,593 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 11 Forks
  • 2 Open issues
  • 2 Versions
  • 10 % Grown

The README.md

Yii2 image thumbnail

Create image thumbnails use Imagine. Thumbnail created and cached automatically. It allows you to create placeholder with service https://placeholder.com/, holder.js or Imagine, (*1)

Features:

  • Easy to use
  • Use Imagine
  • TinyPng compression
  • Automaticly thumbnails caching
  • Cache sorting to subdirectories
  • Caching placeholder from URL (placeholdit.imgix.net)
  • Use placeholdit.imgix.net

Installation

Composer

The preferred way to install this extension is through Composer., (*2)

Either run php composer.phar require sadovojav/yii2-image-thumbnail "dev-master", (*3)

or add "sadovojav/yii2-image-thumbnail": "dev-master" to the require section of your composer.json, (*4)

Config

Attach the component in your config file:, (*5)

'components' => [
    'thumbnail' => [
        'class' => 'sadovojav\image\Thumbnail',
    ],
],

Parameters

  • string basePath = null - Base path
  • string prefixPath = null - Prefix path
  • string cachePath = @runtime/thumbnails - Cache path alias
  • integer cacheExpire = 604800 - Cache expire time
  • array options - Other options (placeholder/quality/additional compression)

Default options:

'options' => [
    'placeholder' => [
        'type' => sadovojav\image\Thumbnail::PLACEHOLDER_TYPE_URL,
        'backgroundColor' => '#f5f5f5',
        'textColor' => '#cdcdcd',
        'textSize' => 30,
        'text' => 'No image'
    ],
    'quality' => 92,
    'format' => null, //webp, jpg, png
    'tinyPng' => [
        'apiKey' => null
    ]
]

Note

textSize - does't support on PLACEHOLDER_TYPE_URL, (*6)

Placeholder type

    1. sadovojav\image\Thumbnail::PLACEHOLDER_TYPE_URL - get placeholder by url
    1. sadovojav\image\Thumbnail::PLACEHOLDER_TYPE_IMAGINE - create placeholder used Imagine

Using

Get cache image

echo Yii::$app->thumbnail->img($file, $params, $options);

This method returns Html::img(), (*7)

Parameters

  • string $file required - Image file path
  • array $params required - Image manipulation methods. See Methods
  • array $options - options for Html::img()

For example:

<?= Yii::$app->thumbnail->img(IMAGE_SRC, [
    'thumbnail' => [
        'width' => 320,
        'height' => 230,
    ],
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

Get cache image url

echo Yii::$app->thumbnail->url($file, $params);

This method returns cache image url, (*8)

Parameters

  • string $file required - Image file path
  • array $params - Image manipulation methods. See Methods

For example:

<?= Yii::$app->thumbnail->url(IMAGE_SRC, [
    'thumbnail' => [
        'width' => 320,
        'height' => 230,
    ],
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

Get placeholder image

echo Yii::$app->thumbnail->img($file, $params, $options);

This method returns Html::img(), (*9)

Parameters

  • string $file required - must to be Null
  • array $params required - Image manipulation methods. See Methods
  • array $options - options for Html::img()

For example:

<?= Yii::$app->thumbnail->img(null, [
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

Get placeholder url

echo Yii::$app->thumbnail->url($file, $params, $options);

This method returns path to placeholder image, (*10)

Parameters

  • string $file required - must to be Null
  • array $params required - Image manipulation methods. See Methods
  • array $options - options for Html::img()

For example:

<?= Yii::$app->thumbnail->url(null, [
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

Method

Resize

'resize' => [
    'width' => 320,
    'height' => 200
]

Parameters

  • integer width required - New width
  • integer height required - New height

Crop

'crop' => [
    'width' => 250,
    'height' => 200,
]

Parameters

  • integer width required - New width
  • integer height required - New height
  • integer x = 0 - X start crop position
  • integer y = 0 - Y start crop position

Thumbnail

'thumbnail' => [
    'width' => 450,
    'height' => 250,
]

Parameters

  • integer width required - New width
  • integer height required - New height
  • string mode = THUMBNAIL_OUTBOUND - Thumbnail mode THUMBNAIL_OUTBOUND or THUMBNAIL_INSET

Placeholder

'placeholder' => [
    'width' => 450,
    'height' => 250,
]

This method return image placeholder if image file doesn't exist., (*11)

Parameters

  • integer width required - Placeholder image width
  • integer height required - Placeholder image height
  • string backgroundColor = #f5f5f5 - Background color
  • string textColor = #cdcdcd - Text color
  • string text = No image - Text

Watermark

'watermark' => [
    'image' => IMAGE_SRC
    'posX' => 0,
    'posY' => 0,
    'width' => 50,
    'height' => 50
]

Parameters

  • string image required - watermark path
  • integer posX required - X/-X watermark position
  • integer posY required - Y/-Y watermark position
  • integer width - Watermark width
  • integer height - Watermark height
  • string mode = THUMBNAIL_OUTBOUND - Thumbnail mode THUMBNAIL_OUTBOUND or THUMBNAIL_INSET

Compression (TinyPng)

'compress' => true

The Versions

15/01 2018

dev-master

9999999-dev

Yii2 image thumbnail component

  Sources   Download

The Requires

 

component yii2 image resize crop thumbnail placeholder tinypng

05/07 2015

1.0.0.0

1.0.0.0

Yii2 image thumbnail component

  Sources   Download

MIT

The Requires

 

component yii2 image resize crop thumbnail placeholder