1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
<?php
/**
* Images Helper.
*
* @package Silla.IO
* @subpackage Core\Helpers
* @author Plamen Nikolov <plamen@athlonsofia.com>
* @copyright Copyright (c) 2015, Silla.io
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3.0 (GPLv3)
*/
namespace Core\Helpers;
use Core;
use \Imagine\Gd\Imagine;
use \Imagine\Image\Box;
use \Imagine\Image\ImageInterface;
/**
* Contains helper methods concerned with image manipulation.
*/
class Image
{
/**
* Generate one or more center cropped thumbnails given thumbnail sizes.
*
* @param string $imagePath Path to the input image file.
* @param array $thumbsSizes Array with thumb sizes. Example: ['150x150', '240x320', '640x480'].
* @param integer $quality Image compression quality (only for JPG and PNG).
*
* @uses self::createThumbnail To create a thumbnail from the given image.
*
* @return array Of paths to the newly created thumbnails.
*/
public static function createCroppedThumbnail($imagePath, array $thumbsSizes, $quality = 95)
{
return self::createThumbnail($imagePath, $thumbsSizes, $quality, ImageInterface::THUMBNAIL_OUTBOUND);
}
/**
* Generate one or more scaled thumbnails that fit in the given sizes.
*
* @param string $imagePath Path to the input image file.
* @param array $thumbsSizes Array with thumb sizes. Example: ['150x150', '240x320', '640x480'].
* @param integer $quality Image compression quality (only for JPG and PNG).
*
* @uses self::createThumbnail To create a thumbnail from the given image.
*
* @return array Of paths to the newly created thumbnails.
*/
public static function createScaledThumbnail($imagePath, array $thumbsSizes, $quality = 95)
{
return self::createThumbnail($imagePath, $thumbsSizes, $quality, ImageInterface::THUMBNAIL_INSET);
}
/**
* Gets image size and aspect ratio.
*
* @param string $imagePath Image file path.
*
* @return array With width, height, and aspect ratio.
*/
public static function getSize($imagePath)
{
$dimensions = getimagesize($imagePath);
return array(
'width' => $dimensions[0],
'height' => $dimensions[1],
'ratio' => $dimensions[0] / $dimensions[1],
);
}
/**
* Retrieve thumbnail file path.
*
* @param string $imagePath Path to the original image file.
* @param string $size Thumbnail size.
* @param string $type Thumbnail type.
*
* @return string
*/
public static function getThumbnailFilePath($imagePath, $size, $type)
{
$meta = pathinfo($imagePath);
$meta['extension'] = isset($meta['extension']) ? ".{$meta['extension']}" : '';
return $meta['dirname'] . "/{$meta['filename']}_{$size}_{$type}{$meta['extension']}";
}
/**
* Generate one or more thumbnails of an input file.
*
* @param string $imagePath Path to the input image file.
* @param array $thumbsSizes Array with thumb sizes. Example: ['150x150', '240x320', '640x480'].
* @param integer $quality Image compression quality (only for JPG and PNG).
* @param string $mode Used by thumbnail creation. Can be either inset or outbound.
*
* @return array Of paths to the newly created thumbnails.
*/
private static function createThumbnail($imagePath, array $thumbsSizes, $quality, $mode)
{
$thumbnails = array();
$type = ($mode === 'inset') ? 'scaled' : 'cropped';
$imagine = new Imagine();
$image = $imagine->open($imagePath);
foreach ($thumbsSizes as $size) {
preg_match_all('/(\d+)/', $size, $thumbSize);
$sizeName = "{$thumbSize[0][0]}x{$thumbSize[0][1]}";
$filePath = self::getThumbnailFilePath($imagePath, $sizeName, $type);
$size = new Box($thumbSize[0][0], $thumbSize[0][1]);
$image
->thumbnail($size, $mode)
->save(
$filePath,
array('quality' => $quality)
);
/* Append the file path of the thumbnail for return. */
$thumbnails[] = $filePath;
}
return $thumbnails;
}
}