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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
<?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 ArrayAccess;
use Maslosoft\Sprite\Interfaces\SpritePackageInterface;
use Symfony\Component\Finder\SplFileInfo;
use UnexpectedValueException;
/**
* SpriteImage
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class SpriteImage implements ArrayAccess
{
/**
* Base path for image
* @var string
*/
public $basePath = '';
/**
* Relative path to image, base for css class name
* @var SplFileInfo
*/
public $info = '';
/**
* Full path to file
* @var string
*/
public $path = '';
/**
* File size in bytes
* @var int
*/
public $size = 0;
/**
* Width in pixels
* @var int
*/
public $width = 0;
/**
* Height in pixels
* @var int
*/
public $height = 0;
/**
* Mime type
* @var string
*/
public $mime = '';
/**
* Image type based on mime data
* @var string
*/
public $type = '';
/**
* CSS sprite name
* @var string
*/
public $name = '';
/**
* Image checksum
* @var string
*/
public $hash = '';
/**
* Packages, to which this sprite belongs
* @var SpritePackageInterface[]
*/
public $packages = [];
/**
* Create sprite image data holder
* @param string $path
* @param SplFileInfo $fileInfo
*/
public function __construct($path, SplFileInfo $fileInfo)
{
$this->basePath = $path;
$this->info = $fileInfo;
// Assign fileinfo variables
$this->path = $this->info->getPathname();
$this->size = $this->info->getSize();
// Assign image data
$info = getimagesize($this->path);
if (!is_array($info))
{
throw new UnexpectedValueException("The image '$this->path' is not a correct image format.");
}
$this->hash = sha1(file_get_contents($this->getFullPath()));
$this->width = (int) $info[0];
$this->height = (int) $info[1];
$this->mime = $info['mime'];
$this->type = explode('/', $this->mime)[1];
// Assign css name
$ext = $this->info->getExtension();
// Replace path parts with `-`
$name = str_replace(['/', '\\', '_'], '-', $this->info->getRelativePathname());
// Remove leading `-`
$name = preg_replace('~^-*~', '', $name);
// Remove double dashes
$name = preg_replace('~-+~', '-', $name);
// Remove file extension
$name = preg_replace("~\.$ext$~", '', $name);
// Replace dots with -
$name = preg_replace('~\.~', '-', $name);
$this->name = $name;
}
public function isSquare()
{
return (int) $this->width === (int) $this->height;
}
/**
* Get full path to image
* @return string
*/
public function getFullPath()
{
return $this->info->getRealPath();
}
// <editor-fold defaultstate="collapsed" desc="ArrayAccess implementation">
/**
* Check if offset exists
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->$offset);
}
/**
* Get offset value
* @param string $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->$offset;
}
/**
* Set offset value
* @param string $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->$offset = $value;
}
/**
* Unset offset value
* @param string $offset
*/
public function offsetUnset($offset)
{
unset($this->$offset);
}
// </editor-fold>
}
API documentation generated by ApiGen