示例#1
0
 /**
  * Set and apply the text on the image
  *
  * @param  string $string
  * @throws Exception
  * @return Gmagick
  */
 public function text($string)
 {
     $draw = new \GmagickDraw();
     // Set the font if passed
     if (null !== $this->font) {
         if (!$draw->setFont($this->font)) {
             throw new Exception('Error: That font is not recognized by the Gmagick extension.');
         }
         // Else, attempt to set a basic, default system font
     } else {
         $fonts = $this->image->resource()->queryFonts();
         if (in_array('Arial', $fonts)) {
             $this->font = 'Arial';
         } else {
             if (in_array('Helvetica', $fonts)) {
                 $this->font = 'Helvetica';
             } else {
                 if (in_array('Tahoma', $fonts)) {
                     $this->font = 'Tahoma';
                 } else {
                     if (in_array('Verdana', $fonts)) {
                         $this->font = 'Verdana';
                     } else {
                         if (in_array('System', $fonts)) {
                             $this->font = 'System';
                         } else {
                             if (in_array('Fixed', $fonts)) {
                                 $this->font = 'Fixed';
                             } else {
                                 if (in_array('system', $fonts)) {
                                     $this->font = 'system';
                                 } else {
                                     if (in_array('fixed', $fonts)) {
                                         $this->font = 'fixed';
                                     } else {
                                         if (isset($fonts[0])) {
                                             $this->font = $fonts[0];
                                         } else {
                                             throw new Exception('Error: No default font could be found by the Gmagick extension.');
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $draw->setFont($this->font);
     $draw->setFontSize($this->size);
     $draw->setFillColor($this->image->getColor($this->fillColor, $this->opacity));
     if (null !== $this->rotation) {
         $draw->rotate($this->rotation);
     }
     if (null !== $this->strokeColor) {
         $draw->setStrokeColor($this->image->getColor($this->strokeColor, $this->opacity));
         $draw->setStrokeWidth((int) $this->strokeWidth);
     }
     $draw->annotate($this->x, $this->y, $string);
     $this->image->resource()->drawImage($draw);
     return $this;
 }
示例#2
0
 /** 
  * 生成水印
  * @param string $groundImage 
  */
 function waterImage($groundImage = '')
 {
     try {
         //获取背景图的高,宽
         if ($groundImage && is_file($groundImage)) {
             $bg = new Gmagick();
             $bg->readImage($groundImage);
             $bgHeight = $bg->getImageHeight();
             $bgWidth = $bg->getImageWidth();
         }
         //获取水印图的高,宽
         if ($this->waterImage && is_file($this->waterImage)) {
             $water = new Gmagick($this->waterImage);
             $waterHeight = $water->getImageHeight();
             $waterWidth = $water->getImageWidth();
         }
         //如果背景图的高宽小于水印图的高宽则不加水印
         if ($bgHeight < $waterHeight || $bgWidth < $waterWidth) {
             return false;
         } else {
             $isWaterImg = TRUE;
         }
         //加水印
         if ($isWaterImg) {
             $dw = new GmagickDraw();
             //加图片水印
             if (is_file($this->waterImage)) {
                 //水印位置随机
                 $waterPos = $this->getWaterPos($bgWidth, $bgHeight, $waterWidth, $waterHeight);
                 $bg->compositeImage($water, 1, $waterPos['x'], $waterPos['y']);
                 if (!$bg->writeImage($groundImage)) {
                     return FALSE;
                 }
             } else {
                 //加文字水印
                 $waterTextInfo = array('textFont' => '15', 'textColor' => '#FF0000', 'textAlpha' => 1, 'textInfo' => 'www.okooo.com');
                 $dw->setFontSize($waterTextInfo['textFont']);
                 //$dw->setFillColor($waterTextInfo['textColor']);
                 $dw->setFillOpacity(1);
                 $x = abs(130 - $bgWidth);
                 $y = abs(15 - $bgHeight);
                 $dw->annotate($x, $y, $waterTextInfo['textInfo']);
                 $dw->setTextEncoding('UTF-8');
                 $bg->drawImage($dw);
                 if (!$bg->writeImage($groundImage)) {
                     return FALSE;
                 }
             }
         }
     } catch (Exception $e) {
         Logger::getLogger('dataengine.lottery.snapshot')->apps('exception')->info(json_encode($e->getMessage()));
     }
 }