 Maslosoft Sprite API
	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 <?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\Generators;
use Maslosoft\Sprite\Interfaces\SpriteGeneratorInterface;
use Maslosoft\Sprite\Traits\CollectionAwareTrait;
use Maslosoft\Sprite\Traits\ConfigurationAwareTrait;
use UnexpectedValueException;
/**
 * ImgGenerator
 *
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
 */
class ImgGenerator implements SpriteGeneratorInterface
{
    use ConfigurationAwareTrait,
      CollectionAwareTrait;
    public function generate()
    {
        $collection = $this->getCollection();
        $config = $this->getConfig();
        if ($collection->width === 0 || $collection->height === 0)
        {
            throw new UnexpectedValueException('Expected collection width and height to be grater than zero');
        }
        $sprite = imagecreatetruecolor($collection->width, $collection->height);
        imagesavealpha($sprite, true);
        $transparent = imagecolorallocatealpha($sprite, 0, 0, 0, 127); //127 not 100
        imagefill($sprite, 0, 0, $transparent);
        foreach ($collection->getGroups() as $group)
        {
            $top = 0;
            foreach ($group->sprites as $image)
            {
                $img = '';
                $path = $image->getFullPath();
                switch ($image->type)
                {
                    case 'png':
                        $img = imagecreatefrompng($path);
                        break;
                    case 'jpg':
                        $img = imagecreatefromjpeg($path);
                        break;
                    case 'gif':
                        $img = imagecreatefromgif($path);
                        break;
                    default:
                        continue;
                }
                if (empty($img))
                {
                    continue;
                }
                imagecopy($sprite, $img, $group->offset, $top, 0, 0, $image->width, $image->height);
                $top += $image['height'];
            }
        }
        $filename = sprintf('%s/%s.png', $config->generatedPath, $config->basename);
        imagepng($sprite, $filename);
        imagedestroy($sprite);
    }
}
	 API documentation generated by ApiGen
