Ejemplo n.º 1
0
 /**
  * 绘制图像
  * @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);
     }
 }