public function getColor($color_idx = "", $opacity = 1) { if (empty($color_idx)) { $color_idx = "#"; $color_idx .= substr("0" . dechex(rand(0, 255)), 0, 2); $color_idx .= substr("0" . dechex(rand(0, 255)), 0, 2); $color_idx .= substr("0" . dechex(rand(0, 255)), 0, 2); } $theColor = NewPixelWand($color_idx); if (is_numeric($opacity)) { $opacity = 1; } if ($opacity < 0) { $opacity = 0; } if ($opacity > 1) { $opacity = 1; } if ($opacity != 1) { PixelSetOpacity($theColor, $opacity); } if (WandHasException($theColor)) { $this->Error($theColor); } return $theColor; }
/** * 画一条线 * @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); }
/** * liberty_magickwand_panorama_image - strictly speaking, this belongs in one of the image processing plugin files, but we'll leave it here for the moment * * @param array $pFileHash File hash - souce_file is required * @param array $pOptions * @access public * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure */ function liberty_magickwand_panorama_image(&$pFileHash, $pOptions = array()) { $magickWand = NewMagickWand(); $pFileHash['error'] = NULL; if (!empty($pFileHash['source_file']) && is_file($pFileHash['source_file'])) { if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand))) { // calculate border width $iwidth = round(MagickGetImageWidth($magickWand)); $iheight = round(MagickGetImageHeight($magickWand)); $aspect = $iwidth / $iheight; $metaHash = array('width' => $iwidth, 'height' => $iheight, 'aspect' => $aspect); // store original file information that we can adjust the viewer LibertyMime::storeMetaData($pFileHash['attachment_id'], 'PANO', $metaHash); // we need to pad the image if the aspect ratio is not 2:1 (give it a wee bit of leeway that we don't add annoying borders if not really needed) if ($aspect > 2.1 || $aspect < 1.9) { $bwidth = $bheight = 0; if ($aspect > 2) { $bheight = round(($iwidth / 2 - $iheight) / 2); } else { $bwidth = round(($iheight / 2 - $iwidth) / 2); } // if the ratio has nothing to do with a panorama image - i.e. gives us a negative number here, we won't generate a panorama image if ($bheight > 0) { $pixelWand = NewPixelWand(); PixelSetColor($pixelWand, !empty($pOptions['background']) ? $pOptions['background'] : 'black'); if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickBorderImage($magickWand, $pixelWand, $bwidth, $bheight), $magickWand))) { if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickWriteImage($magickWand, $pFileHash['source_file']), $magickWand))) { // yay! } } DestroyPixelWand($pixelWand); } } } } DestroyMagickWand($magickWand); return empty($pFileHash['error']); }
/** * Rotate the image clockwise * * @param Asido_TMP &$tmp * @param float $angle * @param Asido_Color &$color * @return boolean * @access protected */ function __rotate(&$tmp, $angle, &$color) { // skip full loops // if ($angle % 360 == 0) { return true; } list($r, $g, $b) = $color->get(); $ret = MagickRotateImage($tmp->target, NewPixelWand("rgb({$r},{$g},{$b})"), $angle); $tmp->image_width = MagickGetImageWidth($tmp->target); $tmp->image_height = MagickGetImageHeight($tmp->target); return $ret; }
/** * liberty_magickwand_rotate_image * * @param array $pFileHash * @access public * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure */ function liberty_magickwand_rotate_image(&$pFileHash) { $ret = FALSE; $magickWand = NewMagickWand(); $pFileHash['error'] = NULL; if (!empty($pFileHash['source_file']) && is_file($pFileHash['source_file'])) { if ($error = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand)) { $pFileHash['error'] = $error; } elseif (empty($pFileHash['degrees']) || !is_numeric($pFileHash['degrees'])) { $pFileHash['error'] = tra('Invalid rotation amount'); } else { $bgWand = NewPixelWand('white'); if ($error = liberty_magickwand_check_error(MagickRotateImage($magickWand, $bgWand, $pFileHash['degrees']), $magickWand)) { $pFileHash['error'] .= $error; } if ($error = liberty_magickwand_check_error(MagickWriteImage($magickWand, $pFileHash['source_file']), $magickWand)) { $pFileHash['error'] .= $error; } } } else { $pFileHash['error'] = "No source file to resize"; } return empty($pFileHash['error']); }
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; }
$x2 = $width - $x_int; $y2 = $height - $y_int; $x_radius = $y_radius = 10; $font_size = 30; $swirl_deg_start = 0; $swirl_deg_end = 360; $swirl_deg_int = 60; $swirl_delay = 10; $num_morph_frames = 5; $morph_delay = 20; $logo = 'MagickWand for PHP'; $welcome_msg = 'The future awaits...'; $mgck_wnd = NewMagickWand(); $bg_color = NewPixelWand(); $white = NewPixelWand('white'); $pxl_wnd = NewPixelWand(); $drw_wnd = NewDrawingWand(); $txt_wnd = NewDrawingWand(); for ($i = 0; $i < $noise_frames; $i++) { checkWandError(MagickNewImage($mgck_wnd, $width, $height, $bg_color), $mgck_wnd, __LINE__); checkWandError(MagickAddNoiseImage($mgck_wnd, MW_LaplacianNoise), $mgck_wnd, __LINE__); checkWandError(MagickSetImageDelay($mgck_wnd, $noise_delay), $mgck_wnd, __LINE__); } $pre_morph_wnd =& checkWandError(MagickGetImage($mgck_wnd), $mgck_wnd, __LINE__); $red = mt_rand(0, 255); $green = mt_rand(0, 255); $blue = mt_rand(0, 255); checkWandError(PixelSetColor($pxl_wnd, "rgb({$red}, {$green}, {$blue})"), $pxl_wnd, __LINE__); DrawSetFillColor($drw_wnd, $pxl_wnd); DrawRoundRectangle($drw_wnd, $x1, $y1, $x2, $y2, $x_radius, $y_radius); drawNewImageSetDelay($pre_morph_wnd, $drw_wnd, $width, $height, $bg_color, $morph_delay, __LINE__);