function addText($source_name, $string, $save_name = "", $font_height = 30, $text_align = "center", $color = "#ff0000", $font = "Arial", $Antialias = true)
 {
     $resource = NewMagickWand();
     $drw_wnd = NewDrawingWand();
     MagickReadImage($resource, $source_name);
     //MagickResizeImage( $resource, 160, 120 ,MW_BoxFilter,1);
     DrawSetFillColor($drw_wnd, $color);
     DrawSetFontSize($drw_wnd, $font_height);
     DrawSetFont($drw_wnd, $font);
     DrawSetTextAntialias($drw_wnd, $Antialias);
     $src_image_x = MagickGetImageWidth($resource);
     $src_image_y = MagickGetImageHeight($resource);
     $string_width = MagickGetStringWidth($resource, $drw_wnd, $string, FALSE);
     $string_height = MagickGetStringHeight($resource, $drw_wnd, $string, FALSE);
     switch ($text_align) {
         case "center":
             $text_x = $src_image_x / 2 - $string_width / 2;
             $text_y = $src_image_y / 2 - $string_height / 2;
             break;
         default:
             break;
     }
     DrawAnnotation($drw_wnd, $text_x, $text_y, $string);
     MagickDrawImage($resource, $drw_wnd);
     if ($save_name) {
         MagickWriteImage($resource, $save_name);
     } else {
         header('Content-Type: image/jpeg');
         MagickEchoImageBlob($resource);
     }
     DestroyDrawingWand($drw_wnd);
     DestroymagickWand($resource);
 }
Example #2
0
 /**
  * 加水印
  * @param string $text 要添加水印的文字
  * @param int $size 字体大小
  * @param int $opcity 水印透明度
  * @param int $degree 要旋转的角度
  * @param const $position 要添加的位置
  * @param int $x 水平位移
  * @param int $y 垂直位移
  * @return bool 成功返回true,失败返回false
  */
 public function addWaterMark($text, $size, $opacity = 1, $degree = 0, $position = self::Center, $x = 0, $y = 0)
 {
     $width = $this->getWidth();
     $height = $this->getHeight();
     $black = NewPixelWand('black');
     $none = NewPixelWand('none');
     $textWand = NewMagickWand();
     MagickNewImage($textWand, $width, $height, $none);
     $textDrawWand = NewDrawingWand();
     DrawSetTextEncoding($textDrawWand, 'UTF-8');
     DrawSetFont($textDrawWand, '/Library/Fonts/Hei.ttf');
     DrawSetFontWeight($textDrawWand, 900);
     DrawSetFillColor($textDrawWand, $black);
     DrawSetFontSize($textDrawWand, $size);
     if ($x == 0 && $y == 0) {
         DrawSetGravity($textDrawWand, constant($position));
     }
     DrawSetFillAlpha($textDrawWand, $opacity);
     $degree = intval($degree);
     if ($degree !== 0) {
         DrawRotate($textDrawWand, $degree);
     }
     DrawAnnotation($textDrawWand, $x, $y, $text);
     MagickDrawImage($textWand, $textDrawWand);
     return MagickCompositeImage(self::$resource, $textWand, MW_OverCompositeOp, $x, $y);
 }
Example #3
0
 public function setFont($para = array())
 {
     $textColor = "#000000";
     $textWeight = 120;
     $textSize = 12;
     $textFont = "simsun.ttc";
     $textAlpha = 1;
     $textAlign = 1;
     $textStrokeColor = "#FFFFFF";
     $textStrokeWidth = 1;
     $textStrokeOpacity = 0.2;
     if (count($para) > 1) {
         extract($para, EXTR_OVERWRITE);
     }
     $imgFont = NewDrawingWand();
     $fontColor = $this->getColor($textColor);
     DrawSetTextEncoding($imgFont, "UTF-8");
     DrawSetFont($imgFont, $textFont);
     DrawSetFontWeight($imgFont, $textWeight);
     DrawSetFillColor($imgFont, $fontColor);
     DrawSetFontSize($imgFont, $textSize);
     DrawSetGravity($imgFont, $textAlign);
     DrawSetFillAlpha($imgFont, $textAlpha);
     if ($textStrokeWidth > 0 && $textStrokeOpacity > 0) {
         $strokeColor = $this->getColor($textStrokeColor);
         DrawSetStrokeColor($imgFont, $strokeColor);
         DrawSetStrokeWidth($imgFont, $textStrokeWidth);
         DrawSetStrokeOpacity($imgFont, $textStrokeOpacity);
     }
     if (WandHasException($imgFont)) {
         $this->Error($imgFont);
     }
     $this->destoryWand($fontColor);
     return $imgFont;
 }
Example #4
0
 function createWaterMark($src_image = "")
 {
     if (!IsMagickWand($src_image)) {
         $src_image = NewMagickWand();
         MagickReadImage($src_image, $this->src_image_name);
     }
     if (!$src_image) {
         return false;
     }
     $src_image_w = MagickGetImageWidth($src_image);
     $src_image_h = MagickGetImageHeight($src_image);
     if ($this->wm_image_name) {
         $wm_image_info = $this->getInfo($this->wm_image_name);
         if (!$wm_image_info) {
             return false;
         }
         $wm_image = NewMagickWand();
         MagickReadImage($wm_image, $this->wm_image_name);
         $wm_image_w = MagickGetImageWidth($wm_image);
         $wm_image_h = MagickGetImageHeight($wm_image);
         $temp_wm_image = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos, $wm_image);
         $wm_image_x = $temp_wm_image["dest_x"];
         $wm_image_y = $temp_wm_image["dest_y"];
         $opacity0 = MagickGetQuantumRange();
         $opacity100 = 0;
         $opacitypercent = $this->wm_image_transition;
         $opacity = $opacity0 - $opacity0 * $opacitypercent / 100;
         if ($opacity > $opacity0) {
             $opacity = $opacity0;
         } elseif ($opacity < 0) {
             $opacity = 0;
         }
         MagickSetImageIndex($wm_image, 0);
         MagickSetImageType($wm_image, MW_TrueColorMatteType);
         MagickEvaluateImage($wm_image, MW_SubtractEvaluateOperator, $opacity, MW_OpacityChannel);
         MagickCompositeImage($src_image, $wm_image, MW_OverCompositeOp, $wm_image_x, $wm_image_y);
     }
     if ($this->wm_text) {
         $this->wm_text = $this->wm_text;
         $temp_wm_text = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos);
         $wm_text_x = $temp_wm_text["dest_x"];
         $wm_text_y = $temp_wm_text["dest_y"];
         $drawing_wand = NewDrawingWand();
         if ($this->wm_text_font != "") {
             DrawSetFont($drawing_wand, $this->wm_text_font);
         }
         DrawSetFontSize($drawing_wand, $this->wm_text_size);
         switch ($this->wm_image_pos) {
             case 0:
                 DrawSetGravity($drawing_wand, MW_CenterGravity);
                 break;
             case 1:
                 DrawSetGravity($drawing_wand, MW_NorthWestGravity);
                 break;
             case 2:
                 DrawSetGravity($drawing_wand, MW_NorthEastGravity);
                 break;
             case 3:
                 DrawSetGravity($drawing_wand, MW_SouthEastGravity);
                 break;
             case 4:
                 DrawSetGravity($drawing_wand, MW_SouthWestGravity);
                 break;
             case 5:
                 DrawSetGravity($drawing_wand, MW_NorthGravity);
                 break;
             case 6:
                 DrawSetGravity($drawing_wand, MW_EastGravity);
                 break;
             case 7:
                 DrawSetGravity($drawing_wand, MW_SouthGravity);
                 break;
             case 8:
                 DrawSetGravity($drawing_wand, MW_WestGravity);
                 break;
             default:
                 DrawSetGravity($drawing_wand, MW_CenterGravity);
                 break;
         }
         $pixel_wand = NewPixelWand();
         if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)) {
             $red = hexdec($color[1]);
             $green = hexdec($color[2]);
             $blue = hexdec($color[3]);
             PixelSetColor($pixel_wand, "rgb({$red},{$green},{$blue})");
         } else {
             PixelSetColor($pixel_wand, "rgb(255,255,255)");
         }
         DrawSetFillColor($drawing_wand, $pixel_wand);
         MagickAnnotateImage($src_image, $drawing_wand, 0, 0, $this->wm_angle, $this->wm_text);
     }
     return $src_image;
 }