public function __construct($map, $options = null, $isMergeImage = null, $isReplaceUri = null) { parent::__construct($map); $this->isMergeImage = is_null($isMergeImage) ? C('IS_MERGE_IMAGE') : $isMergeImage; $this->isReplaceUri = is_null($isReplaceUri) ? C('IS_REPLACE_URI') : $isReplaceUri; if (!self::$cacheDir) { self::$cacheDir = C('CSS_CACHE_PATH') . '/' . str_random(); } // 如果需要合图,则导入合图配置 if ($this->isMergeImage) { if (!file_exists(C('IMERGE_PATH'))) { mark('您已打开合图功能,但没有生成合图配置,请先进行合图,或者关闭合图功能', 'error'); $this->isMergeImage = false; } else { $loader = new MergeConfigLoader(C('IMERGE_PATH')); $this->spriteConfig = $loader->getSpriteConfig(); } } }
/** * 绘制图像 * @param $type */ public function draw($type, $config = array()) { if (empty($config)) { $loader = new MergeConfigLoader($this->imergePath); $config = $loader->getImageConfigByType($type); } $imgList = array(); if (!empty($config)) { foreach ($config as $key => $value) { try { $imgList[$key] = new Image($this->staticRelativePath . $key, $value); } catch (ImageException $e) { mark("合图{$type}中,存在小图{$key}的配置文件,但该小图不存在,请到合图配置界面删除该小图", 'warn'); } } } $layout = new Layout($imgList); $spriteConfigs = array(); $spriteConfigs[$type] = array(); if ($layout->reflow($width, $height)) { $this->sprite = imagecreatetruecolor($width, $height); imagealphablending($this->sprite, false); imagesavealpha($this->sprite, true); imagefill($this->sprite, 0, 0, imagecolorallocatealpha($this->sprite, 0, 0, 0, 127)); $spriteConfigs[$type]['config'] = $this->repaint($this->sprite, $imgList, $width, $height); // 记录sprite的属性 $spriteConfigs[$type]['attr'] = array('filename' => $type . C('SPRITE_SUFFIX') . '.png', 'width' => $width, 'height' => $height); // $spriteConfigs[$type]['filename'] = $type.C('SPRITE_SUFFIX').'.png'; // 生成大图到imerge_sprite_dir $path = $this->imergePath . '/' . C('IMERGE_SPRITE_DIR') . '/' . $type . C('SPRITE_SUFFIX') . '.png'; imagepng($this->sprite, $path); // 写入配置 $writer = new MergeConfigWriter($this->imergePath); $writer->writeSpriteConfig($spriteConfigs); // 派发处理完成事件 trigger('DRAW_SPRITE_END', $path); } }