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 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); }
public function fixSize(&$img, $fix_width = 0, $fix_height = 0, $fix_top = 0, $fix_left = 0, $bgcolor = "white", $fix_mode = false) { if ($fix_width == 0 && $fix_height == 0) { return; } $width = MagickGetImageWidth($img); $height = MagickGetImageHeight($img); if ($fix_mode) { $fix_width += $width; $fix_height += $height; } else { if ($fix_width == 0) { $fix_width = $width; } if ($fix_height == 0) { $fix_height = $height; } } $result = NewMagickWand(); MagickResetIterator($img); while (MagickNextImage($img)) { $drawWand = NewDrawingWand(); DrawComposite($drawWand, MW_AddCompositeOp, $fix_left, $fix_top, $width, $height, $img); $frame = $this->getImgWand(array($bgcolor, $fix_width, $fix_height)); MagickDrawImage($frame, $drawWand); MagickSetImageDelay($frame, MagickGetImageDelay($img)); MagickAddImage($result, $frame); } $this->destoryWand($img, $imgTemp, $drawWand); $img = $result; return; }
function createThumb($objWidth, $objHeight, $nmw = "") { $srcImage = $this->src_image_name; if (!IsMagickWand($nmw)) { $nmw = NewMagickWand(); MagickReadImage($nmw, $srcImage); } $srcImageWidth = MagickGetImageWidth($nmw); $srcImageHeight = MagickGetImageHeight($nmw); if ($objWidth == 0 || $objHeight == 0) { $objWidth = $srcImageWidth; $objHeight = $srcImageHeight; } if ($objWidth < $objHeight) { $mu = $srcImageWidth / $objWidth; $objHeight = ceil($srcImageHeight / $mu); } else { $mu = $srcImageHeight / $objHeight; $objWidth = ceil($srcImageWidth / $mu); } MagickScaleImage($nmw, $objWidth, $objHeight); $ndw = NewDrawingWand(); DrawComposite($ndw, MW_AddCompositeOp, 0, 0, $objWidth, $objHeight, $nmw); $res = NewMagickWand(); MagickNewImage($res, $objWidth, $objHeight); MagickDrawImage($res, $ndw); MagickSetImageFormat($res, MagickGetImageFormat($nmw)); return $res; }
/** * Function drawNewImage() creates a new image, using MagickNewImage(), of * $width * $height area, filled with $bg_color (a PixelWand, or ImageMagick * color string) color. * * It then uses MagickDrawImage() to draw the commands contained in the * DrawingWand $drw_wnd on the newly reated image. * * @param resource MagickWand resource * @param resource DrawingWand resource * @param int width of new image * @param int height of new image * @param mixed PixelWand resource or imagemagick color string * @param int Always __LINE__ (script current line number predefined * PHP constant) * * @return void */ function drawNewImage($mgck_wnd, $drw_wnd, $width, $height, $bg_color, $line) { $line = 'program line ' . $line . ', function line '; checkWandError(MagickNewImage($mgck_wnd, $width, $height, $bg_color), $mgck_wnd, $line . __LINE__); checkWandError(MagickDrawImage($mgck_wnd, $drw_wnd), $mgck_wnd, $line . __LINE__); }