public function validateFile(CM_File $file) { parent::validateFile($file); try { $image = new CM_Image_Image($file->read()); $image->validate(); } catch (CM_Exception $e) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('Invalid image')); } }
public function generateFavicon() { $faviconConfigList = $this->_getFaviconConfigList(); $this->_getStreamOutput()->writeln('Generating favicons'); $themeDirStructList = Functional\map(CM_Site_Abstract::getAll(), function (CM_Site_Abstract $site) { $render = new CM_Frontend_Render(new CM_Frontend_Environment($site)); return ['render' => $render, 'themeDir' => new CM_File($render->getThemeDir(true))]; }); $themeDirStructList = Functional\unique($themeDirStructList, function (array $themeDirStruct) { /** @var CM_File $themeDir */ $themeDir = $themeDirStruct['themeDir']; return $themeDir->getPath(); }); //filter site aliases foreach ($themeDirStructList as $themeDirStruct) { /** @var CM_Frontend_Render $render */ $render = $themeDirStruct['render']; /** @var CM_File $themeDir */ $themeDir = $themeDirStruct['themeDir']; $svgFile = $themeDir->joinPath('resource', 'img', self::FAVICON_SVG_FILENAME); if ($svgFile->exists()) { foreach ($faviconConfigList as $outputFilename => $config) { $backgroundWidth = (int) $config['width']; $backgroundHeight = (int) $config['height']; $backgroundColor = false === $config['transparent'] ? $render->getLessVariable(self::FAVICON_BACKGROUND_LESS_VARIABLE) : 'transparent'; $background = new Imagick(); $background->newPseudoImage($backgroundWidth, $backgroundHeight, 'canvas:' . $backgroundColor); $backgroundImage = new CM_Image_Image($background); $iconSize = (int) (min($backgroundWidth, $backgroundHeight) * (double) $config['iconSize']); $iconImage = CM_Image_Image::createFromSVGWithSize($svgFile->read(), $iconSize, $iconSize); $backgroundImage->compositeImage($iconImage, ($backgroundWidth - $iconSize) / 2, ($backgroundHeight - $iconSize) / 2); $backgroundImage->setFormat(CM_Image_Image::FORMAT_PNG); $targetFile = $themeDir->joinPath('resource', 'img', 'meta', $outputFilename); $targetFile->ensureParentDirectory(); $targetFile->write($backgroundImage->getBlob()); $this->_getStreamOutput()->writeln('Generated ' . $targetFile->getPath()); } } } }
public function validateFile(CM_File $file) { parent::validateFile($file); try { $image = new CM_Image_Image($file->read()); $image->validate(); } catch (CM_Exception $e) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('Invalid image')); } $minSize = $this->_getMinSize($image); $maxSize = $this->_getMaxSize($image); if ($image->getWidth() < $minSize->getX()) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('Image is too small (min width {$minWidth}px).', ['minWidth' => $minSize->getX()])); } if ($image->getHeight() < $minSize->getY()) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('Image is too small (min height {$minHeight}px).', ['minHeight' => $minSize->getY()])); } if ($image->getWidth() > $maxSize->getX()) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('Maximum resolution exceeded (max width {$maxWidth}px).', ['maxWidth' => $maxSize->getX()])); } if ($image->getHeight() > $maxSize->getY()) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('Maximum resolution exceeded (max height {$maxHeight}px).', ['maxHeight' => $maxSize->getY()])); } }
/** * @param string $content * @param bool $compress * @return string */ private function _compileLess($content, $compress) { $render = $this->_render; $lessCompiler = new lessc(); $lessCompiler->registerFunction('image', function ($arg) use($render) { /** @var CM_Frontend_Render $render */ list($type, $delimiter, $values) = $arg; return array('function', 'url', array('string', $delimiter, array($render->getUrlResource('layout', 'img/' . $values[0])))); }); $lessCompiler->registerFunction('image-inline', function ($arg) use($render) { /** @var CM_Frontend_Render $render */ list($type, $delimiter, $values) = $arg; if (2 == sizeof($values) && is_array($values[0]) && is_array($values[1])) { $delimiter = (string) $values[0][1]; $path = (string) $values[0][2][0]; $size = (int) $values[1][1]; } else { $path = $values[0]; $size = 0; } $imagePath = $render->getLayoutPath('resource/img/' . $path, null, null, true, true); $cache = CM_Cache_Persistent::getInstance(); $imageBase64 = $cache->get($cache->key(__METHOD__, md5($imagePath), 'size:' . $size), function () use($imagePath, $size) { $file = new CM_File($imagePath); $img = new CM_Image_Image($file->read()); if ($size > 0) { $img->resize($size, $size); } $img->setFormat(CM_Image_Image::FORMAT_GIF); return base64_encode($img->getBlob()); }); $url = 'data:image/gif;base64,' . $imageBase64; return array('function', 'url', array('string', $delimiter, array($url))); }); $lessCompiler->registerFunction('urlFont', function ($arg) use($render) { /** @var CM_Frontend_Render $render */ list($type, $delimiter, $values) = $arg; return array($type, $delimiter, array($render->getUrlStatic('/font/' . $values[0]))); }); if ($compress) { $lessCompiler->setFormatter('compressed'); } return $lessCompiler->compile($content); }
public function testCalculateDimensionsLower() { $dimensions = CM_Image_Image::calculateDimensions(100, 200, 1000, 500, false); $this->assertEquals(100, $dimensions['width']); $this->assertEquals(200, $dimensions['height']); }
public function testCompositeImage() { $backgroundColor = '#0000FF'; $background = new \Imagick(); $background->newPseudoImage(100, 100, 'canvas:' . $backgroundColor); $backgroundImage = new CM_Image_Image($background); $foregroundColor = '#FF0000'; $foreground = new \Imagick(); $foreground->newPseudoImage(30, 30, 'canvas:' . $foregroundColor); $foregroundImage = new CM_Image_Image($foreground); $backgroundImage->compositeImage($foregroundImage, 10, 10); $backgroundImagick = $this->_getImagickObject($backgroundImage); $colorBackground = $backgroundImagick->getImagePixelColor(9, 9)->getColor(); $this->assertSame(0, $colorBackground['r']); $this->assertSame(0, $colorBackground['g']); $this->assertSame(255, $colorBackground['b']); $colorForeground = $backgroundImagick->getImagePixelColor(10, 10)->getColor(); $this->assertSame(255, $colorForeground['r']); $this->assertSame(0, $colorForeground['g']); $this->assertSame(0, $colorForeground['b']); }