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);
 }
Пример #2
0
 /**
  * 画一条线
  * @param int $sx     起始坐标x
  * @param int $sy     起始坐标y
  * @param int $ex     终止坐标y
  * @param int $ey     终止坐标y
  * @param int $border 线条宽度
  */
 public function drawLine($sx, $sy, $ex, $ey, $property = array())
 {
     $width = $this->getWidth();
     $height = $this->getHeight();
     $color = NewPixelWand(isset($property['color']) ? $property['color'] : 'black');
     $fillColor = NewPixelWand(isset($property['fillColor']) ? $property['fillColor'] : 'none');
     $backgroundColor = NewPixelWand(isset($property['fillColor']) ? $property['backgroundColor'] : 'none');
     $picWand = NewMagickWand();
     MagickNewImage($picWand, $width, $height, $backgroundColor);
     $drawWand = NewDrawingWand();
     DrawSetFillColor($drawWand, $fillColor);
     DrawSetStrokeWidth($drawWand, isset($property['borderWidth']) ? $property['borderWidth'] : 1);
     DrawSetStrokeColor($drawWand, $color);
     if (isset($property['stroke'])) {
         DrawSetStrokeDashArray($drawWand, $property['stroke']);
     }
     DrawLine($drawWand, $sx, $sy, $ex, $ey);
     MagickDrawImage($picWand, $drawWand);
     return MagickCompositeImage(self::$resource, $picWand, MW_OverCompositeOp, $x, $y);
 }
Пример #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;
 }
Пример #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;
 }
Пример #5
0
/**
 * 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);
}
Пример #6
0
/**
 * 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);
}