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); }
/** * 加水印 * @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); }
/** * Function addText() performs several operations on the DrawingWand * $drw_wnd: * 1) sets the fill color (the color in which shapes, text, etc. will be * drawn) from the PixelWand (or Imagemagick color string) $pxl_wnd with * DrawSetFillColor() * 2) sets the font size of text to be drawn to $font_size, using * DrawSetFontSize() * 3) sets the position where text will be drawn with DrawSetGravity(); * position is set to MW_CenterGravity by default, which automatically * centers text horizontally and vertically * 4) sets text to be drawn later to $text, which will be drawn at * coordinate ($x, $y), (relative to the position indicated by * $gravity), with DrawAnnotation() * * If no font is set prior to this function being called, the MagickWand API * uses the default font (seems to be Arial). * * @param resource DrawingWand resource * @param mixed PixelWand resource or imagemagick color string * @param float desired text font size * @param float string to be drawn * @param int the desired text gravity, indicating the desired text * position; must be a MagickWand API GravityType * @param int x ordinate, relative to the chosen text gravity where * text will be drawn * @param int y ordinate, relative to the chosen text gravity where * text will be drawn * * @return void No return value, as all functions used return void. */ function addText($drw_wnd, $pxl_wnd, $font_size, $text, $gravity = MW_CenterGravity, $x = 0, $y = 0) { DrawSetFillColor($drw_wnd, $pxl_wnd); DrawSetFontSize($drw_wnd, $font_size); DrawSetGravity($drw_wnd, $gravity); DrawAnnotation($drw_wnd, $x, $y, $text); }
/** * Function addText() performs several operations on the DrawingWand * $drw_wnd: * 1) sets the fill color (the color in which shapes, text, etc. will be * drawn) from the PixelWand (or Imagemagick color string) $pxl_wnd with * DrawSetFillColor() * 2) sets the font size of text to be drawn to $font_size, using * DrawSetFontSize() * 3) sets the position where text will be drawn with DrawSetGravity(); * position is set to MW_CenterGravity by default, which automatically * centers text horizontally and vertically * 4) sets text to be drawn later to $text, which will be drawn at * coordinate ($x, $y), (relative to the position indicated by * $gravity), with DrawAnnotation() * * If no font is set prior to this function being called, the MagickWand API * uses the default font (seems to be Arial). * * @param resource DrawingWand resource * @param mixed PixelWand resource or imagemagick color string * @param float desired text font size * @param float string to be drawn * @param int the desired text gravity, indicating the desired text * position; must be a MagickWand API GravityType * @param int x ordinate, relative to the chosen text gravity where * text will be drawn * @param int y ordinate, relative to the chosen text gravity where * text will be drawn * * @return void No return value, as all functions used return void. */ function addText($drw_wnd, $pxl_wnd, $font_size, $text, $gravity = MW_CenterGravity, $x = 0, $y = 0) { /* Set the color used to draw shapes and text with $drw_wnd to $pxl_wnd's color */ DrawSetFillColor($drw_wnd, $pxl_wnd); /* Set the text font size used by $drw_wnd to $font_size */ DrawSetFontSize($drw_wnd, $font_size); /* Set $drw_wnd text gravity (automatic text positioning setting), to $gravity */ DrawSetGravity($drw_wnd, $gravity); /* Add a command to the $drw_wnd DrawingWand to draw the $text string at point ($x, $y) (relative to $drw_wnd's gravity setting). */ DrawAnnotation($drw_wnd, $x, $y, $text); }