/** * Creates image with text * @param int $sizeX * @param int $sizeY * @param string $text * @param string $generator * @return \Imagick */ public function mkCapcha($sizeX, $sizeY, $text, $generator = self::METHOD_DEFAULT) { // init image $image = new \Imagick(); $image->newImage($sizeX, $sizeY, new \ImagickPixel('white')); $image->setImageFormat('png'); switch ($generator) { case self::METHOD_GRAYNOISE: $draw = new \ImagickDraw(); $draw->setFontSize(35); $draw->setFontWeight(900); $draw->setGravity(\imagick::GRAVITY_CENTER); $image->addNoiseImage(\imagick::NOISE_LAPLACIAN); $image->annotateImage($draw, 0, 0, 0, $text); $image->charcoalImage(2, 1.5); $image->addNoiseImage(\imagick::NOISE_LAPLACIAN); $image->gaussianBlurImage(1, 1); break; case self::METHOD_GRAYBLUR: $draw = new \ImagickDraw(); $order = []; for ($i = 0; $i < strlen($text); $i++) { $order[$i] = $i; } shuffle($order); for ($j = 0; $j < 2; $j++) { shuffle($order); $image->gaussianBlurImage(15, 3); for ($n = 0; $n < strlen($text); $n++) { $i = $order[$n]; $draw->setFont(__DIR__ . '/Capcha/DejaVuSans.ttf'); $draw->setFontSize($j ? rand($sizeY * 3 / 5, $sizeY * 5 / 6) : rand($sizeY * 4 / 6, $sizeY * 5 / 6)); $draw->setFontWeight(rand(100, 900)); $draw->setGravity(\imagick::GRAVITY_CENTER); $image->annotateImage($draw, ($i - strlen($text) / 2) * $sizeX / (strlen($text) + 2.3), 0, rand(-25, 25), $text[$i]); $image->gaussianBlurImage(1, 1); } } break; } return $image; }
<?php $draw = new ImagickDraw(); $font_path = __DIR__ . '/php_imagick_tests/anonymous_pro_minus.ttf'; $draw->setFont($font_path); var_dump($draw->getFont() === $font_path); try { $draw->setFont(">_<"); } catch (Exception $ex) { var_dump("setFont"); } $draw->setFontSize(12); var_dump($draw->getFontSize()); $draw->setFontStretch(Imagick::STRETCH_SEMIEXPANDED); var_dump($draw->getFontStretch() === Imagick::STRETCH_SEMIEXPANDED); $draw->setFontStyle(Imagick::STYLE_ITALIC); var_dump($draw->getFontStyle() === Imagick::STYLE_ITALIC); $draw->setFontWeight(500); var_dump($draw->getFontWeight()); try { $draw->setFontWeight(1000); } catch (ImagickDrawException $ex) { var_dump($ex->getMessage()); }
function exWrapper($a) { $line_1 = $a['line_1']; $line_2 = $a['line_2']; $line_3 = $a['line_3']; $line_4 = $a['line_4']; $line_1_font = $a['line_1_font']; $line_2_font = $a['line_2_font']; $line_3_font = $a['line_3_font']; $line_4_font = $a['line_4_font']; $line_1_size = $a['line_1_size']; $line_2_size = $a['line_2_size']; $line_3_size = $a['line_3_size']; $line_4_size = $a['line_4_size']; $line_1_x = $a['line_1_x']; $line_2_x = $a['line_2_x']; $line_3_x = $a['line_3_x']; $line_4_x = $a['line_4_x']; $line_1_y = $a['line_1_y']; $line_2_y = $a['line_2_y']; $line_3_y = $a['line_3_y']; $line_4_y = $a['line_4_y']; $red = $a['red']; $green = $a['green']; $blue = $a['blue']; $red_text = $a['red_text']; $green_text = $a['green_text']; $blue_text = $a['blue_text']; $base = new \Imagick('/var/www/mch' . $a['base']); $draw = new \ImagickDraw(); $textColor = "rgb({$red_text}, {$green_text}, {$blue_text})"; if (isset($a['layer_top']) && $a['layer_top'] != "") { $layout = new \Imagick(); $layout_x = $a['layer_top_x']; $layout_y = $a['layer_top_y']; if ($a['image_uploaded'] != true) { $tempimg = '/var/www/mch/' . $a['layer_top']; $im = imagecreatefromstring(base64_decode(transparent($tempimg))); imagepng($im, "test_layer.png"); $tempimg_path = "test_layer.png"; $blob = base64_decode(convert($tempimg_path, $red, $green, $blue)); $layout->readImageBlob($blob); $layout->setImageFormat('png'); } else { $layout->readImage($img); } } if ($line_1 != "" || $line_2 != "" || $line_3 != "" || $line_4 != "") { // $line_1_x = $line_1_x + 25; $line_2_x = $line_2_x + 25; $line_3_x = $line_3_x + 25; $line_4_x = $line_4_x + 25; $line_1_x = $line_1_x < 0 ? 0 : $line_1_x; //$line_1_x = ($line_1_x > 198 ? 198: $line_1_x); $line_2_x = $line_2_x < 0 ? 0 : $line_2_x; //$line_2_x = ($line_2_x > 198 ? 198: $line_2_x); $line_3_x = $line_3_x < 0 ? 0 : $line_3_x; //$line_3_x = ($line_3_x > 198 ? 198: $line_3_x); $line_4_x = $line_4_x < 0 ? 0 : $line_4_x; //$line_4_x = ($line_4_x > 198 ? 198: $line_4_x); // $temp = new Imagick(); // $temp->newImage($w, $h); $draw->setFont($line_1_font); $draw->setFontSize($line_1_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $base->annotateImage($draw, $line_1_x, $line_1_y, 0, $line_1); $draw->setFont($line_2_font); $draw->setFontSize($line_2_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $base->annotateImage($draw, $line_2_x, $line_2_y, 0, $line_2); $draw->setFont($line_3_font); $draw->setFontSize($line_3_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $base->annotateImage($draw, $line_3_x, $line_3_y, 0, $line_3); $draw->setFont($line_4_font); $draw->setFontSize($line_4_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $base->annotateImage($draw, $line_4_x, $line_4_y, 0, $line_4); } echo "line 1x : " . $line_1_x . " line 2x : " . $line_2_x . " line 3x : " . $line_3_x . " line 4x: " . $line_4_x; // $img = '/var/www/mch'.$a['layout']; // $size = getimagesize($a['img']); // $h = $size[1]; // $w = $size[0]; $h = 100; $w = 100; $x = $layout_x; $y = $layout_y; // echo $x." "; // echo $y; if ($x < 134) { $x = 134; } if ($x > 186) { $x = 186; } if ($y < 27) { $y = 27; } if ($y > 84) { $y = 84; } $date = date('Y_m_d_H_i_s'); if (isset($a['layer_top']) && $a['layer_top'] != "") { $layout->resizeImage($w, $h, Imagick::FILTER_LANCZOS, 1); $base->compositeImage($layout, Imagick::COMPOSITE_DISSOLVE, $x, $y); } $bar = $base->writeImage('/var/www/mch/public/test_wrapper.png'); $base->destroy(); if ($bar) { $barpath = "test_wrapper.png"; // echo "x-axis : ".$x." y-axis : ".$y; echo "<img src='{$barpath}'>"; return $bar; } else { return false; } }
$messageWordwrap = 17; break; case 30: $messageWordwrap = 16; break; } $fixedCardMessage = wordwrap($cardMessage, $messageWordwrap, "\n"); $b->annotateImage($draw2, 340, 150, 0, $fixedCardMessage); /* Draw Message */ $draw3 = new ImagickDraw(); /* Text Color */ $draw3->setFillColor($signatureColor); /* Font properties */ $draw3->setFont($signatureFont); $draw3->setFontSize($signatureSize); $draw3->setFontWeight(400); // change wordwrap depending on font size switch ($signatureSize) { case 20: $signatureWordwrap = 24; break; case 22: $signatureWordwrap = 23; break; case 24: $signatureWordwrap = 21; break; case 26: $signatureWordwrap = 19; break; case 28:
function setFontWeight($fillColor, $strokeColor, $backgroundColor) { $draw = new \ImagickDraw(); $draw->setStrokeColor($strokeColor); $draw->setFillColor($fillColor); $draw->setStrokeWidth(1); $draw->setFontSize(36); $draw->setFontWeight(100); $draw->annotation(50, 50, "Lorem Ipsum!"); $draw->setFontWeight(200); $draw->annotation(50, 100, "Lorem Ipsum!"); $draw->setFontWeight(400); $draw->annotation(50, 150, "Lorem Ipsum!"); $draw->setFontWeight(800); $draw->annotation(50, 200, "Lorem Ipsum!"); $imagick = new \Imagick(); $imagick->newImage(500, 500, $backgroundColor); $imagick->setImageFormat("png"); $imagick->drawImage($draw); header("Content-Type: image/png"); echo $imagick->getImageBlob(); }
function kingBar($a) { $line_1 = $a['line_1']; $line_2 = $a['line_2']; $line_3 = $a['line_3']; $line_4 = $a['line_4']; $line_1_font = $a['line_1_font']; $line_2_font = $a['line_2_font']; $line_3_font = $a['line_3_font']; $line_4_font = $a['line_4_font']; $line_1_size = $a['line_1_size']; $line_2_size = $a['line_2_size']; $line_3_size = $a['line_3_size']; $line_4_size = $a['line_4_size']; $line_1_x = $a['line_1_x']; $line_2_x = $a['line_2_x']; $line_3_x = $a['line_3_x']; $line_4_x = $a['line_4_x']; $line_1_y = $a['line_1_y']; $line_2_y = $a['line_2_y']; $line_3_y = $a['line_3_y']; $line_4_y = $a['line_4_y']; $base = new \Imagick('/var/www/mch' . $a['base']); $draw = new \ImagickDraw(); $fillColor = $base->getImagePixelColor(0, 0); $textColor = "rgb(126, 64, 17)"; if (isset($a['layer_top']) && $a['layer_top'] != "") { $layout = new \Imagick(); $layout_x = $a['layer_top_x']; $layout_y = $a['layer_top_y']; if ($a['image_uploaded'] != true) { $img = '/var/www/mch/' . $a['layer_top']; $blob = base64_decode(convert($img)); $layout->readImageBlob($blob); $layout->setImageFormat('png'); } else { $layout->readImage($img); } } if ($line_1 != "" || $line_2 != "" || $line_3 != "" || $line_4 != "") { // $line_1_x = $line_1_x + 25; $line_2_x = $line_2_x + 25; $line_3_x = $line_3_x + 25; $line_4_x = $line_4_x + 25; $line_1_x = $line_1_x < 70 ? 70 : $line_1_x; $line_1_x = $line_1_x > 196 ? 197 + 20 : $line_1_x; $line_2_x = $line_2_x < 70 ? 70 : $line_2_x; $line_2_x = $line_2_x > 196 ? 197 + 20 : $line_2_x; $line_3_x = $line_3_x < 70 ? 70 : $line_3_x; $line_3_x = $line_3_x > 196 ? 197 + 20 : $line_3_x; $line_4_x = $line_4_x < 70 ? 70 : $line_4_x; $line_4_x = $line_4_x > 196 ? 197 + 20 : $line_4_x; $size = getimagesize('/var/www/mch' . $a['base']); $w = $size[0]; $h = $size[1]; $tras = new Imagick(); $mask = new Imagick(); $tras->newImage($w, $h, new ImagickPixel('grey30')); $mask->newImage($w, $h, new ImagickPixel('black')); // $tras->paintTransparentImage(new ImagickPixel('black'), 0.4, 0); $draw->setFont($line_1_font); $draw->setFontSize($line_1_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $tras->annotateImage($draw, $line_1_x - 1, $line_1_y - 1, 0, $line_1); $draw->setFillColor('white'); $mask->annotateImage($draw, $line_1_x, $line_1_y, 0, $line_1); $mask->annotateImage($draw, $line_1_x - 1, $line_1_y - 1, 0, $line_1); $draw->setFillColor('black'); $mask->annotateImage($draw, $line_1_x - 2, $line_1_y - 2, 0, $line_1); $draw->setFont($line_2_font); $draw->setFontSize($line_2_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $tras->annotateImage($draw, $line_2_x - 1, $line_2_y - 1, 0, $line_2); $draw->setFillColor('white'); $mask->annotateImage($draw, $line_2_x, $line_2_y, 0, $line_2); $mask->annotateImage($draw, $line_2_x - 1, $line_2_y - 1, 0, $line_2); $draw->setFillColor('black'); $mask->annotateImage($draw, $line_2_x - 2, $line_2_y - 2, 0, $line_2); $draw->setFont($line_3_font); $draw->setFontSize($line_3_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $tras->annotateImage($draw, $line_3_x - 1, $line_3_y - 1, 0, $line_3); $draw->setFillColor('white'); $mask->annotateImage($draw, $line_3_x, $line_3_y, 0, $line_3); $mask->annotateImage($draw, $line_3_x - 1, $line_3_y - 1, 0, $line_3); $draw->setFillColor('black'); $mask->annotateImage($draw, $line_3_x - 2, $line_3_y - 2, 0, $line_3); $draw->setFont($line_4_font); $draw->setFontSize($line_4_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $tras->annotateImage($draw, $line_4_x - 1, $line_4_y - 1, 0, $line_4); $draw->setFillColor('white'); $mask->annotateImage($draw, $line_4_x, $line_4_y, 0, $line_4); $mask->annotateImage($draw, $line_4_x - 1, $line_4_y - 1, 0, $line_4); $draw->setFillColor('black'); $mask->annotateImage($draw, $line_4_x - 2, $line_4_y - 2, 0, $line_4); $mask->setImageMatte(false); $tras->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0); $base->compositeImage($tras, Imagick::COMPOSITE_DISSOLVE, 0, 0); } echo "line 1x : " . $line_1_x . " line 2x : " . $line_2_x . " line 3x : " . $line_3_x . " line 4x: " . $line_4_x; // $img = '/var/www/mch'.$a['layout']; // $size = getimagesize($a['img']); // $h = $size[1]; // $w = $size[0]; $h = 100; $w = 100; $x = $layout_x; $y = $layout_y; // echo $x." "; // echo $y; if ($x < 134) { $x = 134; } if ($x > 186) { $x = 186; } if ($y < 27) { $y = 27; } if ($y > 84) { $y = 84; } $date = date('Y_m_d_H_i_s'); if (isset($a['layer_top']) && $a['layer_top'] != "") { $layout->resizeImage($w, $h, Imagick::FILTER_LANCZOS, 1); $base->compositeImage($layout, Imagick::COMPOSITE_OVERLAY, $x, $y); } $bar = $base->writeImage('/var/www/mch/public/test_bar.png'); $base->destroy(); if ($bar) { $barpath = "test_bar.png"; // echo "x-axis : ".$x." y-axis : ".$y; echo "<img src='{$barpath}'>"; return $bar; } else { return false; } }
function create_thumbnail($filename, $save2disk = true, $resize_type = 0) { $filename = $this->basename($filename); if ($this->is_image($this->mmhclass->info->root_path . $this->mmhclass->info->config['upload_path'] . $filename) == true) { $extension = $this->file_extension($filename); $thumbnail = $this->thumbnail_name($filename); if ($save2disk == true) { // Seemed easier to build the image resize upload // option into the already established thumbnail function // instead of waisting time trying to chop it up for new one. if ($resize_type > 0 && $resize_type <= 8) { $thumbnail = $filename; $this->mmhclass->info->config['advanced_thumbnails'] = false; $size_values = array(1 => array("w" => 100, "h" => 75), 2 => array("w" => 150, "h" => 112), 3 => array("w" => 320, "h" => 240), 4 => array("w" => 640, "h" => 480), 5 => array("w" => 800, "h" => 600), 6 => array("w" => 1024, "h" => 768), 7 => array("w" => 1280, "h" => 1024), 8 => array("w" => 1600, "h" => 1200)); $thumbnail_size = $size_values[$resize_type]; } else { $thumbnail_size = $this->scale($filename, $this->mmhclass->info->config['thumbnail_width'], $this->mmhclass->info->config['thumbnail_height']); } if ($this->manipulator == "imagick") { // New Design of Advanced Thumbnails created by: IcyTexx - http://www.hostili.com // Classic Design of Advanced Thumbnails created by: Mihalism Technologies - http://www.mihalism.net $canvas = new Imagick(); $athumbnail = new Imagick(); $imagick_version = $canvas->getVersion(); // Imagick needs to start giving real version number, not build number. $new_thumbnails = version_compare($imagick_version['versionNumber'], "1621", ">=") == true ? true : false; $athumbnail->readImage("{$this->mmhclass->info->root_path}{$this->mmhclass->info->config['upload_path']}{$filename}[0]"); $athumbnail->flattenImages(); $athumbnail->orgImageHeight = $athumbnail->getImageHeight(); $athumbnail->orgImageWidth = $athumbnail->getImageWidth(); $athumbnail->orgImageSize = $athumbnail->getImageLength(); $athumbnail->thumbnailImage($thumbnail_size['w'], $thumbnail_size['h']); if ($this->mmhclass->info->config['advanced_thumbnails'] == true) { $thumbnail_filesize = $this->format_filesize($athumbnail->orgImageSize, true); $resobar_filesize = $thumbnail_filesize['f'] < 0 || $thumbnail_filesize['c'] > 9 ? $this->mmhclass->lang['5454'] : sprintf("%s%s", round($thumbnail_filesize['f']), $this->mmhclass->lang['7071'][$thumbnail_filesize['c']]); if ($new_thumbnails == true) { $textdraw = new ImagickDraw(); $textdrawborder = new ImagickDraw(); if ($athumbnail->getImageWidth() > 113) { $textdraw->setFillColor(new ImagickPixel("white")); $textdraw->setFontSize(9); $textdraw->setFont("{$mmhclass->info->root_path}css/fonts/sf_fedora_titles.ttf"); $textdraw->setFontWeight(900); $textdraw->setGravity(8); $textdraw->setTextKerning(1); $textdraw->setTextAntialias(false); $textdrawborder->setFillColor(new ImagickPixel("black")); $textdrawborder->setFontSize(9); $textdrawborder->setFont("{$mmhclass->info->root_path}css/fonts/sf_fedora_titles.ttf"); $textdrawborder->setFontWeight(900); $textdrawborder->setGravity(8); $textdrawborder->setTextKerning(1); $textdrawborder->setTextAntialias(false); $array_x = array("-1", "0", "1", "1", "1", "0", "-1", "-1"); $array_y = array("-1", "-1", "-1", "0", "1", "1", "1", "0"); foreach ($array_x as $key => $value) { $athumbnail->annotateImage($textdrawborder, $value, 3 - $array_y[$key], 0, "{$athumbnail->orgImageWidth}x{$athumbnail->orgImageHeight} - {$resobar_filesize}"); } $athumbnail->annotateImage($textdraw, 0, 3, 0, "{}x{$athumbnail->orgImageHeight} - {$resobar_filesize}"); } } else { $transback = new Imagick(); $canvasdraw = new ImagickDraw(); $canvas->newImage($athumbnail->getImageWidth(), $athumbnail->getImageHeight() + 12, new ImagickPixel("black")); $transback->newImage($canvas->getImageWidth(), $canvas->getImageHeight() - 12, new ImagickPixel("white")); $canvas->compositeImage($transback, 40, 0, 0); $canvasdraw->setFillColor(new ImagickPixel("white")); $canvasdraw->setGravity(8); $canvasdraw->setFontSize(10); $canvasdraw->setFontWeight(900); $canvasdraw->setFont("AvantGarde-Demi"); $canvas->annotateImage($canvasdraw, 0, 0, 0, "{$athumbnail->orgImageWidth}x{$athumbnail->orgImageHeight} - {$resobar_filesize}"); $canvas->compositeImage($athumbnail, 40, 0, 0); $athumbnail = $canvas->clone(); } } if ($this->mmhclass->info->config['thumbnail_type'] == "jpeg") { $athumbnail->setImageFormat("jpeg"); $athumbnail->setImageCompression(9); } else { $athumbnail->setImageFormat("png"); } $athumbnail->writeImage($this->mmhclass->info->root_path . $this->mmhclass->info->config['upload_path'] . $thumbnail); } else { // I hate GD. Piece of crap supports nothing. NOTHING! if (in_array($extension, array("png", "gif", "jpg", "jpeg")) == true) { $function_extension = str_replace("jpg", "jpeg", $extension); $image_function = "imagecreatefrom{$function_extension}"; $image = $image_function($this->mmhclass->info->root_path . $this->mmhclass->info->config['upload_path'] . $filename); $imageinfo = $this->get_image_info($this->mmhclass->info->root_path . $this->mmhclass->info->config['upload_path'] . $this->basename($filename)); $thumbnail_image = imagecreatetruecolor($thumbnail_size['w'], $thumbnail_size['h']); $index = imagecolortransparent($thumbnail_image); if ($index < 0) { $white = imagecolorallocate($thumbnail_image, 255, 255, 255); imagefill($thumbnail_image, 0, 0, $white); } imagecopyresampled($thumbnail_image, $image, 0, 0, 0, 0, $thumbnail_size['w'], $thumbnail_size['h'], $imageinfo['width'], $imageinfo['height']); $image_savefunction = sprintf("image%s", $this->mmhclass->info->config['thumbnail_type'] == "jpeg" ? "jpeg" : "png"); $image_savefunction($thumbnail_image, $this->mmhclass->info->root_path . $this->mmhclass->info->config['upload_path'] . $thumbnail); chmod($this->mmhclass->info->root_path . $this->mmhclass->info->config['upload_path'] . $thumbnail, 0644); imagedestroy($image); imagedestroy($thumbnail_image); } else { trigger_error("Image format not supported by GD", E_USER_ERROR); } } chmod($this->mmhclass->info->root_path . $this->mmhclass->info->config['upload_path'] . $thumbnail, 0644); } else { readfile($this->mmhclass->info->root_path . $this->mmhclass->info->config['upload_path'] . $thumbnail); } } }
/** * handler for img requests */ function ProcessImgRequest() { if ($_SERVER["REQUEST_METHOD"] == "GET") { $method = $_GET["method"]; $params = explode(",", $_GET["params"]); $width = (int) $params[0]; $height = (int) $params[1]; if ($method == "placeholder") { $image = new Imagick(); $image->newImage($width, $height, "#707070"); $image->setImageFormat("png"); $x = 0; $y = 0; $size = 40; $draw = new ImagickDraw(); while ($y < $height) { $draw->setFillColor("#808080"); $points = [["x" => $x, "y" => $y], ["x" => $x + $size, "y" => $y], ["x" => $x + $size * 2, "y" => $y + $size], ["x" => $x + $size * 2, "y" => $y + $size * 2]]; $draw->polygon($points); $points = [["x" => $x, "y" => $y + $size], ["x" => $x + $size, "y" => $y + $size * 2], ["x" => $x, "y" => $y + $size * 2]]; $draw->polygon($points); $x += $size * 2; if ($x > $width) { $x = 0; $y += $size * 2; } } $draw->setFillColor("#B0B0B0"); $draw->setFontSize($width / 5); $draw->setFontWeight(800); $draw->setGravity(Imagick::GRAVITY_CENTER); $draw->annotation(0, 0, $width . " x " . $height); $image->drawImage($draw); header("Content-type: image/png"); echo $image; } else { $file_name = $_GET["src"]; $path_parts = pathinfo($file_name); switch ($path_parts["extension"]) { case "png": $mime_type = "image/png"; break; case "gif": $mime_type = "image/gif"; break; default: $mime_type = "image/jpeg"; break; } $file_name = $path_parts["basename"]; $image = ResizeImage($file_name, $method, $width, $height); header("Content-type: " . $mime_type); echo $image; } } }
/** * 添加文字注解,可用于文字水印 * * @param string $txt 必须为utf8编码,文档格式utf-8格式即可 * @param float $opacity 设置不透明度 * @param constant $gravity 设置文字摆放位置,GRAVITY_NorthWest,GRAVITY_North,GRAVITY_NorthEast,GRAVITY_West, * GRAVITY_Center,GRAVITY_East,GRAVITY_SouthWest,GRAVITY_South,GRAVITY_SouthEast,SAE_Static * @param array $font 字体数组可以设置如下属性: * <pre> * name,常量,字体名称,如果需要添加中文注解,请使用中文字体,否则中文会显示乱码。 * 支持的字体:FONT_SimSun(宋体,默认)、FONT_SimKai(楷体)、FONT_SimHei(正黑)、FONT_MicroHei(微米黑)、FONT_Arial * weight,字体宽度,int * size,字体大小,int * color,字体颜色,例如:"blue", "#0000ff", "rgb(0,0,255)"等,默认为"black"; * </pre> * * @return bool * @author Sauwe */ public function annotate($txt, $opacity = 0.5, $gravity = SAE_Static, $font = array("name" => FONT_SimSun, "size" => 15, "weight" => 300, "color" => "black")) { if ($this->notValid()) { return false; } $opacity = floatval($opacity); $fontfamily = FONT_SimSun; if (isset($font["name"])) { $fontfamily = $font["name"]; } $fontsize = 15; if (isset($font["size"])) { $fontsize = $font["size"]; } $fontweight = 300; if (isset($font["weight"])) { $fontweight = $font["weight"]; } $fontcolor = "black"; if (isset($font["color"])) { $fontcolor = $font["color"]; } try { $im = new Imagick(); $im->readImageBlob($this->_img_data); $id = new ImagickDraw(); $id->setFont($fontfamily); //这里是一个实际上的字体路径 $id->setFontSize($fontsize); $id->setFontWeight($fontweight); $id->setFillColor(new ImagickPixel($fontcolor)); $id->setGravity($gravity); $id->setFillOpacity($opacity); if ($im->annotateImage($id, 0, 0, 0, $txt)) { $this->_img_data = $im->getImageBlob(); return true; } return false; } catch (Exception $e) { $this->_errno = SAE_ErrUnknown; $this->_errmsg = $e->getMessage(); return false; } }
$draw = new ImagickDraw(); while ($y < $height) { $draw->setFillColor("#808080"); $points = [["x" => $x, "y" => $y], ["x" => $x + $size, "y" => $y], ["x" => $x + $size * 2, "y" => $y + $size], ["x" => $x + $size * 2, "y" => $y + $size * 2]]; $draw->polygon($points); $points = [["x" => $x, "y" => $y + $size], ["x" => $x + $size, "y" => $y + $size * 2], ["x" => $x, "y" => $y + $size * 2]]; $draw->polygon($points); $x += $size * 2; if ($x > $width) { $x = 0; $y += $size * 2; } } $draw->setFillColor("#B0B0B0"); $draw->setFontSize($width / 5); $draw->setFontWeight(800); $draw->setGravity(Imagick::GRAVITY_CENTER); $draw->annotation(0, 0, $width . " x " . $height); $image->drawImage($draw); header("Content-type: image/png"); echo $image; } else { $file_name = $_GET["src"]; $path_parts = pathinfo($file_name); switch ($path_parts["extension"]) { case "png": $mime_type = "image/png"; break; case "gif": $mime_type = "image/gif"; break;