public function render(Job $job) { $format = $this->configuration->getString('format', 'png'); $defaultColor = $format == 'png' ? 0x0 : 0xffffffff; $backgroundColor = $this->configuration->getColor('background', $defaultColor); foreach ($job->spritesheets as $spritesheet) { /** @var Spritesheet $spritesheet */ $image = ImageTools::createImage($spritesheet->width, $spritesheet->height, $backgroundColor); foreach ($spritesheet->sprites as $sprite) { $repeatY = $sprite->repeatY; while ($repeatY--) { $repeatX = $sprite->repeatX; while ($repeatX--) { imagecopy($image, $sprite->image, $sprite->spriteX + $sprite->paddingLeft + $repeatX * $sprite->width, $sprite->spriteY + $sprite->paddingTop + $repeatY * $sprite->height, 0, 0, $sprite->width, $sprite->height); } } } $output = $this->mosaic->getPath($this->configuration->getString('outputFolder', Configuration::VALUE_REQUIRED)); $encodedSpritesheetPath = Tools::encodeFilePath($output . '/' . $spritesheet->name . '.' . $format); FileTools::createDirectory(dirname($encodedSpritesheetPath)); if ($format == 'png') { imagepng($image, $encodedSpritesheetPath, 9, E_ALL); } elseif ($format == 'jpg') { imagejpeg($image, $encodedSpritesheetPath, $this->configuration->getInt('quality', 90)); } imagedestroy($image); $encodedSpritesheetPath = realpath($encodedSpritesheetPath); if (!$encodedSpritesheetPath) { throw new \Exception('Could not verify rendered spritesheet path.'); } // Normalize output path $spritesheet->path = Tools::decodeFilePath($encodedSpritesheetPath); } }
public function onSpritesLoaded(Event $event) { foreach ($this->job->sprites as $sprite) { if (!$sprite->enabled) { continue; } $configuration = $this->getSpriteConfiguration($sprite); if (!$configuration->getBool('enabled', true)) { continue; } $scale = $configuration->getTwoSide('scale', 1); if ($scale->sideA != 1 && $scale->sideB != 1) { $image = ImageTools::resizeImage($sprite->image, round($sprite->width * $scale->sideA), round($sprite->height * $scale->sideB), false, ImageTools::MODE_EXACT); $sprite->replaceImage($image); continue; } $width = $configuration->getInt('width'); $height = $configuration->getInt('height'); if (!$width && !$height) { return; } $newWidth = $width; $newHeight = $height; if ($width && $height) { } elseif ($width) { $newHeight = $newWidth / ($sprite->width / $sprite->height); } else { $newWidth = $newHeight * ($sprite->width / $sprite->height); } if ($newWidth != $sprite->width || $newHeight != $sprite->height) { $image = ImageTools::resizeImage($sprite->image, $newWidth, $newHeight, false, ImageTools::MODE_EXACT); $sprite->replaceImage($image); } } }
public function load(Job $job) { $excludeFilters = $job->configuration->get('excludes', null); $includeFilters = $job->configuration->get('includes', null); /** @var Sprite[] $loadSprites */ $loadSprites = []; foreach ($this->folders as $folder) { $files = FileTools::listDirectory($folder, false, true); foreach ($files as $file) { $originalSpriteName = pathinfo(str_replace('/', '-', $file), PATHINFO_FILENAME); $filteredSpriteName = preg_replace('/\\{[^\\}]+\\}/', '', $originalSpriteName); // Strip {tags} $included = true; if ($excludeFilters !== null) { $included = !Tools::matches($excludeFilters, $originalSpriteName) && !Tools::matches($excludeFilters, $filteredSpriteName); } elseif ($includeFilters !== null) { $included = false; } if (!$included && $includeFilters !== null) { $included = Tools::matches($includeFilters, $originalSpriteName) || Tools::matches($includeFilters, $filteredSpriteName); } if (!$included) { continue; } $sprite = new Sprite(); $sprite->originalName = $sprite->name = $originalSpriteName; $sprite->name = $filteredSpriteName; $sprite->path = $folder . $file; $loadSprites[$sprite->name] = $sprite; } } foreach ($loadSprites as $sprite) { $sprite->configuration->extend($job->configuration->configuration); $this->extendSpriteSettings($job, $sprite); if (!$sprite->configuration->getBool('enabled', true)) { continue; } $image = ImageTools::checkFile($sprite->path); if ($image->success) { $sprite->image = $image->image; $sprite->calculateDimensions(); $job->sprites[$sprite->name] = $sprite; } } }
public function onSpritesLoaded(Event $event) { foreach ($this->job->sprites as $sprite) { if (!$sprite->enabled) { continue; } $configuration = $this->getSpriteConfiguration($sprite); if (!$configuration->getBool('enabled', true)) { continue; } $backgroundColor = $configuration->getColor('color'); $padding = $configuration->getFourSide('padding', 0); if ($backgroundColor) { $newImage = ImageTools::createImage($sprite->width + $padding->left + $padding->right, $sprite->height + $padding->top + $padding->bottom, $backgroundColor); imagealphablending($newImage, true); imagecopy($newImage, $sprite->image, $padding->left, $padding->top, 0, 0, $sprite->width, $sprite->height); $sprite->replaceImage($newImage); } } }
public static function calcSizeFromImage($image, $maxWidth, $maxHeight, $allowUpScale = true, $allowRotation = false) { return ImageTools::calcSize(imagesx($image), imagesy($image), $maxWidth, $maxHeight, $allowUpScale, $allowRotation); }
public function save() { if ($this->_image) { $this->onProcessImage($this->_image); if ($this->_data) { $this->saveFilename = $this->_data; if (!$this->valueContainsExtension) { $this->saveFilename .= '.' . $this->saveFormat; } } else { if (!$this->saveFilename) { $this->_data = FileTools::createUniqueFilename($this->saveDirectory, '.' . $this->saveFormat, 32, true); if ($this->valueContainsExtension) { $this->saveFilename = $this->_data = $this->_data . '.' . $this->saveFormat; } else { $this->saveFilename = $this->_data . '.' . $this->saveFormat; } } } if ($this->resizeToWidth) { $image = ImageTools::resizeImage($this->_image, $this->resizeToWidth, $this->resizeToHeight, false, $this->resizeImageToolsScaleMode, $this->resizeImageToolsBackground); } else { $image = $this->_image; } if ($this->saveFormat == 'jpg') { imagejpeg($image, $this->saveDirectory . $this->saveFilename, $this->saveQuality); } else { if ($this->saveFormat == 'png') { imagepng($image, $this->saveDirectory . $this->saveFilename); } } $this->onImageProcessed($this->_image); if ($image != $this->_image) { imagedestroy($this->_image); } imagedestroy($image); } }