Maslosoft Sprite API
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/sprite
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link http://maslosoft.com/sprite/
*/
namespace Maslosoft\Sprite\Models;
use Maslosoft\Sprite\Helpers\ImageSorter;
/**
* Collection of image groups
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Collection
{
/**
* Total width of all groups
* @var int
*/
public $width = 0;
/**
* Total height of all groups
* @var int
*/
public $height = 0;
private $sprites = [];
/**
* Image groups
* @var Group[]
*/
private $groups = [];
/**
*
* @param SpriteImage[] $sprites
*/
public function __construct($sprites = [])
{
ImageSorter::sort($sprites);
$this->sprites = $sprites;
}
/**
* Get groups
* @return Group[]
*/
public function getGroups()
{
if (empty($this->groups))
{
$this->createGroups();
}
return $this->groups;
}
/**
* Get all sprites
* @return SpriteImage[]
*/
public function getSprites()
{
return $this->sprites;
}
private function createGroups()
{
/**
* TODO Sprites should be regrouped by hash,
* however packages need to be merged too.
* Without hash grouping resulting images
* and CSS might contain duplicates.
*/
$imagesCount = count($this->sprites);
$splitFactor = floor(sqrt($imagesCount));
$split = ceil($imagesCount / $splitFactor);
$m = 0;
$i = 1;
$totalHeight = 0;
$totalWidth = 0;
$height = 0;
$doSplit = false;
$counter = 0;
$column = [];
$columns = [];
$groups = [];
// Integer indexed sprites
$images = array_values($this->sprites);
foreach ($images as $id => $image)
{
/* @var $image SpriteImage */
/* @var $nextImage SpriteImage */
$counter++;
$column['widths'][] = $image->width;
$column['heights'][] = $image->height;
$column['images'][] = $image;
$column['width'] = max($column['widths']);
$column['height'] = (int) array_sum($column['heights']);
if (isset($images[$id + 1]))
{
$nextImage = $images[$id + 1];
}
else
{
$nextImage = [];
}
// Split because it's last image
if ($counter == $imagesCount)
{
$doSplit = true;
}
// Split because image number is above split treshold
if ($i > $split)
{
$doSplit = true;
}
// Ignore small image width differences
if ($nextImage && round($nextImage->width / 10) > round($image->width / 10))
{
$doSplit = true;
}
// Split because height is different
if ($height && $column['height'] >= $height)
{
$doSplit = true;
}
// Do split (create new column of images) if nessesary
if ($doSplit)
{
// Ignore first small images if there are not enough of them
if (count($column['heights']) > $split || $height)
{
$height = max($height, $column['height']);
}
unset($column['widths']);
unset($column['heights']);
$group = new Group();
$group->sprites = $column['images'];
$group->width = (int) $column['width'];
$group->height = (int) $column['height'];
$groups[$m] = $group;
$columns[$m] = $column;
$i = 0;
$m++;
$column = [];
$doSplit = false;
}
$i++;
}
foreach ($columns as $key => $dimensions)
{
$groups[$key]->offset = $totalWidth;
$totalWidth += $dimensions['width'];
if ($dimensions['height'] > $totalHeight)
{
$totalHeight = $dimensions['height'];
}
}
$this->height = $totalHeight;
$this->width = $totalWidth;
$this->groups = $groups;
}
}
API documentation generated by ApiGen