/** * Draws a polygon on the handle * * @param ImageMagick-object $handle The handle on which the polygon is drawn * @param Zend_Image_Action_DrawPolygon $polygon The object that with all info */ public function perform($handle, Zend_Image_Action_DrawPolygon $polygon) { // As of ZF2.0 / PHP5.3, this can be made static. $points = $this->_parsePoints($polygon->getPoints()); if ($polygon->isClosed()){ //add first point at the end to close $points[count($points)] = $points[0]; } $draw = new ImagickDraw(); $draw->setStrokeColor('#' . $polygon->getStrokeColor()->getHex()); $draw->setStrokeOpacity($polygon->getStrokeAlpha()/100); $draw->setStrokeWidth($polygon->getStrokeWidth()); $strokeDashArray = $polygon->getStrokeDashPattern(); if (count($strokeDashArray) > 0){ $draw->setStrokeDashArray($strokeDashArray); } $draw->setStrokeDashOffset($polygon->getStrokeDashOffset()); if($polygon->isFilled()) { $fillColor = $polygon->getFillColor(); $draw->setFillColor('#' . $fillColor->getHex()); $draw->polygon($points); } else { //Use transparent fill to render unfilled $draw->setFillOpacity(0); $draw->polyline($points); } $handle->getHandle()->drawImage($draw); }
function draw_pie(&$canvas, $ox, $oy, $r, $sd, $ed, $color) { $draw = new ImagickDraw(); $draw->setFillColor($color); $draw->setStrokeColor($color); $draw->setStrokeWidth(1); $draw->arc($ox - $r, $oy - $r, $ox + $r, $oy + $r, $sd, $ed); $draw->polygon(array(array('x' => $ox, 'y' => $oy), get_coord($ox, $oy, $r, $r, $sd), get_coord($ox, $oy, $r, $r, $ed))); $canvas->drawImage($draw); }
/** * Draw polygon on given image * * @param Image $image * @param integer $x * @param integer $y * @return boolean */ public function applyToImage(Image $image, $x = 0, $y = 0) { $polygon = new \ImagickDraw(); // set background $bgcolor = new Color($this->background); $polygon->setFillColor($bgcolor->getPixel()); // set border if ($this->hasBorder()) { $border_color = new Color($this->border_color); $polygon->setStrokeWidth($this->border_width); $polygon->setStrokeColor($border_color->getPixel()); } $polygon->polygon($this->points); $image->getCore()->drawImage($polygon); return true; }
public function render($pDatas) { $im = new Imagick(); $im->newImage(300, 300, "white", "png"); $draw = new ImagickDraw(); foreach ($pDatas as $key => $data) { if ($data->value == 0) { continue; } $draw->setFillColor('red'); $draw->setstrokecolor('black'); $draw->polygon($data->polygons); $draw->setFillColor('black'); $draw->setFontSize(18); $draw->annotation($data->center['x'], $data->center['y'] + 9, $data->value); } $im->drawimage($draw); return $im; }
public function draw($image) { $draw = new \ImagickDraw(); $draw->setStrokeWidth($this->borderSize); if (null !== $this->fillColor) { $fillColor = new \ImagickPixel($this->fillColor->getHexString()); $draw->setFillColor($fillColor); } else { $draw->setFillOpacity(0); } if (null !== $this->borderColor) { $borderColor = new \ImagickPixel($this->borderColor->getHexString()); $draw->setStrokeColor($borderColor); } else { $draw->setStrokeOpacity(0); } $draw->polygon($this->points()); $image->getCore()->drawImage($draw); return $image; }
<?php /* This examples was ported from Imagemagick C examples. */ /* Create Imagick objects */ $Imagick = new Imagick(); /* Create ImagickDraw objects */ $ImagickDraw = new ImagickDraw(); /* Create ImagickPixel objects */ $ImagickPixel = new ImagickPixel(); /* This array contains polygon geometry */ $array = array(array("x" => 378.1, "y" => 81.72), array("x" => 381.1, "y" => 79.56), array("x" => 384.3, "y" => 78.12), array("x" => 387.6, "y" => 77.33), array("x" => 391.1, "y" => 77.11), array("x" => 394.6, "y" => 77.62), array("x" => 397.8, "y" => 78.77), array("x" => 400.9, "y" => 80.56999999999999), array("x" => 403.6, "y" => 83.02), array("x" => 523.9, "y" => 216.8), array("x" => 526.2, "y" => 219.7), array("x" => 527.6, "y" => 223), array("x" => 528.4, "y" => 226.4), array("x" => 528.6, "y" => 229.8), array("x" => 528.0, "y" => 233.3), array("x" => 526.9, "y" => 236.5), array("x" => 525.1, "y" => 239.5), array("x" => 522.6, "y" => 242.2), array("x" => 495.9, "y" => 266.3), array("x" => 493, "y" => 268.5), array("x" => 489.7, "y" => 269.9), array("x" => 486.4, "y" => 270.8), array("x" => 482.9, "y" => 270.9), array("x" => 479.5, "y" => 270.4), array("x" => 476.2, "y" => 269.3), array("x" => 473.2, "y" => 267.5), array("x" => 470.4, "y" => 265), array("x" => 350, "y" => 131.2), array("x" => 347.8, "y" => 128.3), array("x" => 346.4, "y" => 125.1), array("x" => 345.6, "y" => 121.7), array("x" => 345.4, "y" => 118.2), array("x" => 346, "y" => 114.8), array("x" => 347.1, "y" => 111.5), array("x" => 348.9, "y" => 108.5), array("x" => 351.4, "y" => 105.8), array("x" => 378.1, "y" => 81.72)); /* This ImagickPixel is used to set background color */ $ImagickPixel->setColor('gray'); /* Create new image, set color to gray and format to png*/ $Imagick->newImage(700, 500, $ImagickPixel); $Imagick->setImageFormat('png'); /* Create the polygon*/ $ImagickDraw->polygon($array); /* Render the polygon to image*/ $Imagick->drawImage($ImagickDraw); /* Send headers and output the image */ header("Content-Type: image/{$Imagick->getImageFormat()}"); echo $Imagick->getImageBlob();
function drawImage() { $strokeColor = 'black'; $fillColor = 'salmon'; $backgroundColor = 'lightblue'; $draw = new \ImagickDraw(); $draw->setStrokeOpacity(1); $draw->setStrokeColor($strokeColor); $draw->setStrokeWidth(4); $draw->setFillColor($fillColor); $points = [['x' => 40 * 5, 'y' => 10 * 5], ['x' => 20 * 5, 'y' => 20 * 5], ['x' => 70 * 5, 'y' => 50 * 5], ['x' => 60 * 5, 'y' => 15 * 5]]; $draw->polygon($points); $image = new \Imagick(); $image->newImage(500, 300, $backgroundColor); $image->setImageFormat("png"); $image->drawImage($draw); header("Content-Type: image/png"); echo $image->getImageBlob(); }
/** * Based on an image specification, write such an image to disk, using Imagick PHP extension * @param array $spec Spec describing background and circles to draw * @param string $format File format to write * @param string $filename Filename to write to */ public function writeImageWithApi($spec, $format, $filename) { // this is a hack because I can't get setImageOrientation() to work. See below. global $wgExiv2Command; $image = new Imagick(); /** * If the format is 'jpg', will also add a random orientation -- the * image will be drawn rotated with triangle points facing in some * direction (0, 90, 180 or 270 degrees) and a countering rotation * should turn the triangle points upward again. */ $orientation = self::$orientations[0]; // default is normal orientation if ($format == 'jpg') { $orientation = self::$orientations[array_rand(self::$orientations)]; $spec = self::rotateImageSpec($spec, $orientation['counterRotation']); } $image->newImage($spec['width'], $spec['height'], new ImagickPixel($spec['fill'])); foreach ($spec['draws'] as $drawSpec) { $draw = new ImagickDraw(); $draw->setFillColor($drawSpec['fill']); $draw->polygon($drawSpec['shape']); $image->drawImage($draw); } $image->setImageFormat($format); // this doesn't work, even though it's documented to do so... // $image->setImageOrientation( $orientation['exifCode'] ); $image->writeImage($filename); // because the above setImageOrientation call doesn't work... nor can I // get an external imagemagick binary to do this either... Hacking this // for now (only works if you have exiv2 installed, a program to read // and manipulate exif). if ($wgExiv2Command) { $cmd = wfEscapeShellArg($wgExiv2Command) . " -M " . wfEscapeShellArg("set Exif.Image.Orientation " . $orientation['exifCode']) . " " . wfEscapeShellArg($filename); $retval = 0; $err = wfShellExec($cmd, $retval); if ($retval !== 0) { print "Error with {$cmd}: {$retval}, {$err}\n"; } } }
function slantRectangle(&$canvas, $x, $y, $w, $h, $color1, $image = 0) { $background = "#FFFFFF"; //$ltr = '#000000'; $ltr = '#888'; $bd = '#A6A6A6'; $iglow = '#404040'; try { $gradient1 = new Imagick(); $gradient1->newPseudoImage($h, $w * 3 / 8, "gradient:white-{$ltr}"); $gradient1->rotateImage(new ImagickPixel(), 270); $gradient2 = new Imagick(); $gradient2->newPseudoImage($h, $w * 3 / 8, "gradient:{$ltr}-white"); $gradient2->rotateImage(new ImagickPixel(), 270); $lefttoright = new Imagick(); $lefttoright->newPseudoImage($w, $h, "xc:{$ltr}"); $lefttoright->compositeImage($gradient1, imagick::COMPOSITE_OVER, 0, 0); $lefttoright->compositeImage($gradient2, imagick::COMPOSITE_OVER, $w * 5 / 8, 0); $whiteup = new Imagick(); $whiteup->newPseudoImage($w, $h / 2, "gradient:black-#888"); $gradient1 = new Imagick(); $gradient1->newPseudoImage($w, $h / 7, "gradient:{$bd}-white"); $gradient2 = new Imagick(); $gradient2->newPseudoImage($w, $h / 7, "gradient:white-{$bd}"); $bottomdark = new Imagick(); $bottomdark->newPseudoImage($w, $h, "xc:white"); $bottomdark->compositeImage($gradient1, imagick::COMPOSITE_OVER, 0, 0); $bottomdark->compositeImage($gradient2, imagick::COMPOSITE_OVER, 0, $h / 2 - $h / 7); $background = new Imagick(); $background->newPseudoImage($w, $h, "xc:{$color1}"); $background->compositeImage($lefttoright, imagick::COMPOSITE_MULTIPLY, 0, 0); if ($image) { $logo = new Imagick(); $logo->readImage(realpath($image)); $logo->resizeImage($h, $h, imagick::FILTER_TRIANGLE, 1); $background->compositeImage($logo, imagick::COMPOSITE_OVER, $w - $h - $h / 3, 0); } $background->compositeImage($whiteup, imagick::COMPOSITE_SCREEN, 0, 0); $background->compositeImage($bottomdark, imagick::COMPOSITE_MULTIPLY, 0, $h / 2); $slantleft = new Imagick(); $slantleft->newPseudoImage($h * sqrt(5) / 2, 8, "gradient:{$iglow}-white"); $slantleft->rotateImage("none", 296.6); $slantright = new Imagick(); $slantright->newPseudoImage($h * sqrt(5) / 2, 8, "gradient:{$iglow}-white"); $slantright->rotateImage("none", 117.2); $top = new Imagick(); $top->newPseudoImage($w, 8, "gradient:{$iglow}-white"); $bottom = new Imagick(); $bottom->newPseudoImage($w, 8, "gradient:white-{$iglow}"); $slants = new Imagick(); $slants->newPseudoImage($w, $h, "xc:white"); $slants->compositeImage($slantleft, imagick::COMPOSITE_OVER, -1, 0); $slants->compositeImage($slantright, imagick::COMPOSITE_OVER, $w - $h / 2 - 9, 0); $slants->compositeImage($top, imagick::COMPOSITE_MULTIPLY, 0, 0); $slants->compositeImage($bottom, imagick::COMPOSITE_MULTIPLY, 0, $h - 8); $background->compositeImage($slants, imagick::COMPOSITE_MULTIPLY, 0, 00); $draw1 = new ImagickDraw(); $draw1->pushPattern('gradient', 0, 0, $w, $h); $draw1->composite(Imagick::COMPOSITE_OVER, 0, 0, $w, $h, $background); $draw1->popPattern(); $draw1->setFillPatternURL('#gradient'); $draw1->polygon(array(array('x' => 00, 'y' => $h - 1), array('x' => $h / 2 - 1, 'y' => 00), array('x' => $w - 1, 'y' => 00), array('x' => $w - $h / 2 - 1, 'y' => $h - 1))); $points = array(array('x' => 0, 'y' => $h - 1), array('x' => $h / 2 - 1, 'y' => 00), array('x' => $w - 1, 'y' => 00), array('x' => $w - $h / 2 - 1, 'y' => $h - 1)); for ($i = 0; $i < 4; $i++) { $points[$i]['x'] += 10; $points[$i]['y'] += 10; } $shadow = new Imagick(); $shadow->newPseudoImage($w + 20, $h + 20, "xc:none"); $draws = new ImagickDraw(); $draws->setFillColor("black"); $draws->polygon($points); $shadow->drawImage($draws); $shadow->blurImage(0, 4, imagick::CHANNEL_ALPHA); $im = new Imagick(); $im->newPseudoImage($w, $h, "xc:none"); $im->drawImage($draw1); $im2 = new Imagick(); $im2->newPseudoImage($w + 50, $h + 50, "xc:none"); $im2->compositeImage($shadow, imagick::COMPOSITE_OVER, 5, 5); $draw1 = new ImagickDraw(); $draw1->setStrokeWidth(6); $draw1->setStrokeColor("black"); $draw1->polygon($points); $draw1->setStrokeWidth(2); $draw1->setStrokeColor("white"); $draw1->polygon($points); $im2->drawImage($draw1); $im2->compositeImage($im, imagick::COMPOSITE_OVER, 10, 10); $canvas->compositeImage($im2, imagick::COMPOSITE_OVER, $x - 10, $y - 10); } catch (Exception $e) { echo 'Error: ', $e->getMessage(), ""; } }
/** * {@inheritdoc} */ public function polygon(array $coordinates, ColorInterface $color, $fill = false, $thickness = 1) { if (count($coordinates) < 3) { throw new InvalidArgumentException(sprintf('Polygon must consist of at least 3 coordinates, %d given', count($coordinates))); } $points = array_map(function (PointInterface $p) { return array('x' => $p->getX(), 'y' => $p->getY()); }, $coordinates); try { $pixel = $this->getColor($color); $polygon = new \ImagickDraw(); $polygon->setStrokeColor($pixel); $polygon->setStrokeWidth(max(1, (int) $thickness)); if ($fill) { $polygon->setFillColor($pixel); } else { $polygon->setFillColor('transparent'); } $polygon->polygon($points); $this->imagick->drawImage($polygon); $pixel->clear(); $pixel->destroy(); $polygon->clear(); $polygon->destroy(); } catch (\ImagickException $e) { throw new RuntimeException('Draw polygon operation failed', $e->getCode(), $e); } return $this; }
public function build() { // Prepage image $this->_canvas = new Imagick(); $this->_canvas->newImage(self::WIDTH, self::HEIGHT, new ImagickPixel("white")); $color['line'] = new ImagickPixel("rgb(216, 76, 64)"); $color['text'] = new ImagickPixel("rgb(16, 35, 132)"); $color['karma'] = new ImagickPixel("rgb(116, 194, 98)"); $color['force'] = new ImagickPixel("rgb(37, 168, 255)"); $color['bottom_bg'] = new ImagickPixel("rgb(255, 244, 224)"); $color['bg'] = new ImagickPixel("white"); $color['neutral'] = new ImagickPixel("rgb(200, 200, 200)"); // Prepare canvas for drawing main graph $draw = new ImagickDraw(); $draw->setStrokeColor($color['line']); $draw->setFillColor($color['bg']); $draw->setStrokeAntialias(true); $draw->setStrokeWidth(2); // Prepare values for drawing main graph define('BOTTOM_PAD', 30); define('TOP_PAD', 25); define('LEFT_PAD', 43); define('RIGHT_PAD', 15); $graph = array('b' => self::HEIGHT - BOTTOM_PAD, 't' => TOP_PAD); $graph['height'] = $graph['b'] - $graph['t']; $dataHeight = $this->_max_karma - $this->_min_karma; if ($dataHeight != 0) { $graph['k'] = $graph['height'] / $dataHeight; } else { $graph['k'] = 1; } $karma = array_reverse($this->_karma); $graph['segmentWidth'] = (self::WIDTH - LEFT_PAD - RIGHT_PAD) / (count($karma) - 1); $lastX = LEFT_PAD; $lastY = $karma[0]; $graph['cords'] = array(); foreach ($karma as $y) { $graph['cords'][] = array('x' => (double) $lastX, 'y' => $graph['b'] - round($y - $this->_min_karma) * $graph['k']); $lastX += $graph['segmentWidth']; } //Draw graph $draw->polyline($graph['cords']); $this->_canvas->drawImage($draw); // Draw bottom bg define('TOP_SPACER', 10); $draw = new ImagickDraw(); $draw->setFillColor($color['bottom_bg']); $draw->polygon(array(array('x' => 0, 'y' => $graph['b'] + TOP_SPACER), array('x' => self::WIDTH, 'y' => $graph['b'] + TOP_SPACER), array('x' => self::WIDTH, 'y' => self::HEIGHT), array('x' => 0, 'y' => self::HEIGHT))); $this->_canvas->drawImage($draw); $draw->destroy(); // Draw texts $draw = new ImagickDraw(); $draw->setTextAntialias(true); $draw->setFontSize(12); $draw->setFillColor($color['text']); $draw->setFont(realpath('stuff/arial.ttf')); $draw->annotation(2, 13, 'Хаброметр юзера'); $draw->setFont(realpath('stuff/arialbd.ttf')); $code = $this->_user; if (strlen($code) > 25) { $code = substr($code, 0, 22) . '...'; } $draw->annotation(125, 13, $code); $image = new Imagick('stuff/bg-user2.gif'); $this->_canvas->compositeImage($image, Imagick::COMPOSITE_COPY, 109, 2, Imagick::CHANNEL_ALL); $image->clear(); $image->destroy(); $this->_canvas->drawImage($draw); $draw->destroy(); $draw = new ImagickDraw(); $draw->setFontSize(10); $draw->setFillColor($color['karma']); $draw->setFont(realpath('stuff/consola.ttf')); $draw->annotation(2, $graph['t'] + 5, sprintf('%01.2f', $this->_max_karma)); $draw->annotation(2, $graph['b'] + 2, sprintf('%01.2f', $this->_min_karma)); $draw->setFontSize(10); $draw->setFillColor($color['neutral']); $draw->setFont(realpath('stuff/consola.ttf')); $t = preg_split('/-|\\s|:/', $this->_logTime); $this->_canvas->annotateImage($draw, self::WIDTH - 3, $graph['b'] + 2, -90, $t[2] . '.' . $t[1] . '.' . substr($t[0], -2) . ' ' . $t[3] . ':' . $t[4]); $this->_canvas->drawImage($draw); $draw->destroy(); $draw = new ImagickDraw(); $draw->setFillColor($color['karma']); $text = 'min/max: ' . sprintf('%01.2f', $this->_extremums['karma_min']) . ' / ' . sprintf('%01.2f', $this->_extremums['karma_max']); $draw->setFontSize(12); $draw->setFont(realpath('stuff/consola.ttf')); $draw->setFillColor($color['bg']); $draw->annotation(3, $graph['b'] + 25, $text); $draw->setFillColor($color['karma']); $draw->annotation(2, $graph['b'] + 24, $text); $this->_canvas->drawImage($draw); $draw->destroy(); $draw = new ImagickDraw(); $draw->setTextAlignment(Imagick::ALIGN_RIGHT); $text = 'min/max: ' . sprintf('%01.2f', $this->_extremums['habraforce_min']) . ' / ' . sprintf('%01.2f', $this->_extremums['habraforce_max']); $draw->setFontSize(12); $draw->setFont(realpath('stuff/consola.ttf')); $draw->setFillColor($color['bg']); $draw->annotation(self::WIDTH - 3, $graph['b'] + 25, $text); $draw->setFillColor($color['force']); $draw->annotation(self::WIDTH - 2, $graph['b'] + 24, $text); $this->_canvas->drawImage($draw); $draw->destroy(); $draw = new ImagickDraw(); $draw->setTextAlignment(Imagick::ALIGN_RIGHT); $draw->setTextUnderColor($color['karma']); $draw->setFillColor($color['bg']); $draw->setFontSize(14); $draw->setFont(realpath('stuff/consolab.ttf')); $textInfo = $this->_canvas->queryFontMetrics($draw, sprintf('%01.2f', $this->_habraforce), null); $draw->annotation($lastX - $graph['segmentWidth'] + 1 - $textInfo['textWidth'] - 10, 13, sprintf('%01.2f', $karma[count($karma) - 1])); $draw->setTextUnderColor($color['force']); $draw->setFillColor($color['bg']); $draw->annotation($lastX - $graph['segmentWidth'] + 1, 13, sprintf('%01.2f', $this->_habraforce)); $this->_canvas->drawImage($draw); $draw->destroy(); $image = new Imagick('stuff/logo_mini.png'); $this->_canvas->compositeImage($image, Imagick::COMPOSITE_COPY, 3, 47, Imagick::CHANNEL_ALL); $image->clear(); $image->destroy(); return true; }
function setStrokeMiterLimit($strokeColor, $fillColor, $backgroundColor) { $draw = new \ImagickDraw(); $draw->setStrokeColor($strokeColor); $draw->setStrokeOpacity(0.6); $draw->setFillColor($fillColor); $draw->setStrokeWidth(10); $yOffset = 100; $draw->setStrokeLineJoin(\Imagick::LINEJOIN_MITER); for ($y = 0; $y < 3; $y++) { $draw->setStrokeMiterLimit(40 * $y); $points = [['x' => 22 * 3, 'y' => 15 * 4 + $y * $yOffset], ['x' => 20 * 3, 'y' => 20 * 4 + $y * $yOffset], ['x' => 70 * 5, 'y' => 45 * 4 + $y * $yOffset]]; $draw->polygon($points); } $image = new \Imagick(); $image->newImage(500, 500, $backgroundColor); $image->setImageFormat("png"); $image->drawImage($draw); $image->setImageType(\Imagick::IMGTYPE_PALETTE); //TODO - this should either be everywhere or nowhere $image->setImageCompressionQuality(100); $image->stripImage(); header("Content-Type: image/png"); echo $image->getImageBlob(); }
/** * Method to add a polygon to the image. * * @param array $points * @return \Pop\Image\Imagick */ public function drawPolygon($points) { $draw = new \ImagickDraw(); $draw->setFillColor($this->setColor($this->fillColor)); if (null !== $this->strokeWidth) { $draw->setStrokeColor($this->setColor($this->strokeColor)); $draw->setStrokeWidth(null === $this->strokeWidth ? 1 : $this->strokeWidth); } $draw->polygon($points); $this->resource->drawImage($draw); return $this; }
/** * 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; } } }
$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");
/** * Draw a polygon on the image. * * @param array $points * @return Imagick */ public function polygon($points) { $draw = new \ImagickDraw(); if (null !== $this->fillColor) { $draw->setFillColor($this->image->getColor($this->fillColor, $this->opacity)); } if ($this->strokeWidth > 0) { $draw->setStrokeColor($this->image->getColor($this->strokeColor, $this->opacity)); $draw->setStrokeWidth($this->strokeWidth); } $draw->polygon($points); $this->image->resource()->drawImage($draw); return $this; }
<?php $im = new Imagick(); $draw = new ImagickDraw(); try { $draw->polygon(array(array('x' => 1, 'y' => 2), array('x' => 'hello', 'y' => array()))); echo "pass\n"; } catch (Exception $e) { echo "fail\n"; } try { $draw->polygon(array(array())); echo "fail\n"; } catch (ImagickDrawException $e) { echo "pass\n"; }
function slantRectangle(&$canvas, $o) { $background = "#FFFFFF"; //$ltr = '#000000'; $ltr = '#888'; $bd = '#A6A6A6'; $iglow = '#404040'; try { $gradient1 = new Imagick(); $gradient1->newPseudoImage($o['h'], $o['w'] * 3 / 8, "gradient:white-$ltr"); $gradient1->rotateImage(new ImagickPixel(), 270); $gradient2 = new Imagick(); $gradient2->newPseudoImage($o['h'], $o['w'] * 3 / 8, "gradient:$ltr-white"); $gradient2->rotateImage(new ImagickPixel(), 270); $lefttoright = new Imagick(); $lefttoright->newPseudoImage($o['w'], $o['h'], "xc:$ltr"); $lefttoright->compositeImage($gradient1, imagick::COMPOSITE_OVER, 0, 0); $lefttoright->compositeImage($gradient2, imagick::COMPOSITE_OVER, $o['w'] * 5 / 8, 0); $whiteup = new Imagick(); $whiteup->newPseudoImage($o['w'], $o['h'] / 2, "gradient:black-#888"); $gradient1 = new Imagick(); $gradient1->newPseudoImage($o['w'], $o['h'] / 7, "gradient:$bd-white"); $gradient2 = new Imagick(); $gradient2->newPseudoImage($o['w'], $o['h'] / 7, "gradient:white-$bd"); $bottomdark = new Imagick(); $bottomdark->newPseudoImage($o['w'], $o['h'], "xc:white"); $bottomdark->compositeImage($gradient1, imagick::COMPOSITE_OVER, 0, 0); $bottomdark->compositeImage($gradient2, imagick::COMPOSITE_OVER, 0, ($o['h'] / 2) - ($o['h'] / 7)); $background = fillRectangle($o['w'], $o['h'], $o['color']); $background->compositeImage($lefttoright, imagick::COMPOSITE_MULTIPLY, 0, 0); // experimental if ($o['image']) { $logo = new Imagick(); $logo->readImage(realpath($o['image'])); $logo->resizeImage($o['h'], $o['h'], imagick::FILTER_TRIANGLE, 1); $background->compositeImage($logo, imagick::COMPOSITE_OVER, $o['w'] - $o['h'] - ($o['h'] / 3), 0); } $background->compositeImage($whiteup, imagick::COMPOSITE_SCREEN, 0, 0); $background->compositeImage($bottomdark, imagick::COMPOSITE_MULTIPLY, 0, $o['h'] / 2); $slantleft = new Imagick(); $slantleft->newPseudoImage($o['h'] * sqrt(5) / 2, 8, "gradient:$iglow-white"); $slantleft->rotateImage("none", 296.6); $slantright = new Imagick(); $slantright->newPseudoImage($o['h'] * sqrt(5) / 2, 8, "gradient:$iglow-white"); $slantright->rotateImage("none", 117.2); $top = new Imagick(); $top->newPseudoImage($o['w'], 8, "gradient:$iglow-white"); $bottom = new Imagick(); $bottom->newPseudoImage($o['w'], 8, "gradient:white-$iglow"); $slants = new Imagick(); $slants->newPseudoImage($o['w'], $o['h'], "xc:white"); $slants->compositeImage($slantleft, imagick::COMPOSITE_OVER, -1, 0); $slants->compositeImage($slantright, imagick::COMPOSITE_OVER, $o['w'] - ($o['h'] / 2) - 9, 0); $slants->compositeImage($top, imagick::COMPOSITE_MULTIPLY, 0, 0); $slants->compositeImage($bottom, imagick::COMPOSITE_MULTIPLY, 0, $o['h'] - 8); $background->compositeImage($slants, imagick::COMPOSITE_MULTIPLY, 0, 00); $draw1 = new ImagickDraw(); $draw1->pushPattern('gradient', 0, 0, $o['w'], $o['h']); $draw1->composite(Imagick::COMPOSITE_OVER, 0, 0, $o['w'], $o['h'], $background); $draw1->popPattern(); $draw1->setFillPatternURL('#gradient'); $draw1->polygon(array(array('x' => 00, 'y' => $o['h'] - 1), array('x' => ($o['h'] / 2) - 1, 'y' => 00), array('x' => $o['w'] - 1, 'y' => 00), array('x' => $o['w'] - ($o['h'] / 2) - 1, 'y' => $o['h'] - 1))); $points = array(array('x' => 0, 'y' => $o['h'] - 1), array('x' => ($o['h'] / 2) - 1, 'y' => 00), array('x' => $o['w'] - 1, 'y' => 00), array('x' => $o['w'] - ($o['h'] / 2) - 1, 'y' => $o['h'] - 1)); for ($i = 0; $i < 4; $i++) { $points[$i]['x']+=10; $points[$i]['y']+=10; } $shadow = new Imagick(); $shadow->newPseudoImage($o['w'] + 20, $o['h'] + 20, "xc:none"); $draws = new ImagickDraw(); $draws->setFillColor("black"); $draws->polygon($points); $shadow->drawImage($draws); $shadow->blurImage(0, 4, imagick::CHANNEL_ALPHA); $im = new Imagick(); $im->newPseudoImage($o['w'], $o['h'], "xc:none"); $im->drawImage($draw1); $im2 = new Imagick(); $im2->newPseudoImage($o['w'] + 50, $o['h'] + 50, "xc:none"); $im2->compositeImage($shadow, imagick::COMPOSITE_OVER, 5, 5); $draw1 = new ImagickDraw(); $draw1->setStrokeWidth(6); $draw1->setStrokeColor("black"); $draw1->polygon($points); $draw1->setStrokeWidth(2); $draw1->setStrokeColor("white"); $draw1->polygon($points); $im2->drawImage($draw1); $im2->compositeImage($im, imagick::COMPOSITE_OVER, 10, 10); $canvas->compositeImage($im2, imagick::COMPOSITE_OVER, $o['x'] - 10, $o['y'] - 10); } catch (Exception $e) { echo 'Error: ', $e->getMessage(), ""; } }
/** * Draw a polygon based on a set of vertices. * * @param array $vertices An array of x and y labeled arrays * (eg. $vertices[0]['x'], $vertices[0]['y'], ...). * @param string $color The color you want to draw the polygon with. * @param string $fill The color to fill the polygon. */ public function polygon($verts, $color, $fill = 'none') { $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel($fill)); $draw->setStrokeColor(new ImagickPixel($color)); $draw->polygon($verts); try { $res = $this->_imagick->drawImage($draw); } catch (ImagickException $e) { throw new Horde_Image_Exception($e); } $draw->destroy(); }
/** * * * @param * * @return void */ private function drawPolygon($event, $au_scalar, &$polyOffsetX, &$polyOffsetY, &$polygonURL, &$polyWidth, &$polyHeight) { $originX = null; $originY = null; $polygonURL = null; $maxPixelScale = 0.60511022; // arcseconds per pixel $polyString = $event['hpc_boundcc']; $polyString = str_replace(array('POLYGON', '(', ')'), '', $polyString); foreach (explode(',', $polyString) as $xy) { list($x_coord, $y_coord) = explode(' ', $xy); $x[] = $x_coord * $au_scalar; $y[] = -$y_coord * $au_scalar; } $originX = min($x); $originY = min($y); $polyOffsetX = $originX; $polyOffsetY = $originY; $width = 0; $height = 0; for ($i = 0; $i < count($x); $i++) { $xCoord = ($x[$i] - $originX) / $maxPixelScale; $yCoord = ($y[$i] - $originY) / $maxPixelScale; $polyArray[] = array('x' => $xCoord, 'y' => $yCoord); if ($xCoord > $width) { $width = $xCoord; } if ($yCoord > $height) { $height = $yCoord; } } /* Create a new imagick object */ $im = new Imagick(); /* Create ImagickDraw object */ $draw = new ImagickDraw(); $strokeWidth = 4; $draw->setStrokeLineJoin(Imagick::LINEJOIN_ROUND); $draw->setStrokeColor('#00000088'); $draw->setStrokeWidth($strokeWidth); $draw->setStrokeAntialias(true); $draw->setFillColor('#' . $GLOBALS['HEK_COLORS'][$event['event_type']] . '66'); $draw->polygon($polyArray); $polyWidth = $width + $strokeWidth; $polyHeight = $height + $strokeWidth; /* Create a new canvas object and a transparent image */ $canvas = new Imagick(); $canvas->newImage($polyWidth, $polyHeight, 'none'); /* Draw the ImagickDraw on to the canvas */ $canvas->drawImage($draw); /* Set the format to PNG */ $canvas->setImageFormat('png'); $canvas->setInterlaceScheme(Imagick::INTERLACE_PNG); /* Output the image */ $dateArray = date_parse($event['event_starttime']); $cache_base_dir = HV_CACHE_DIR . '/events/' . $dateArray['year'] . '/' . str_pad($dateArray['month'], 2, '0', STR_PAD_LEFT) . '/' . str_pad($dateArray['day'], 2, '0', STR_PAD_LEFT); // Check for existence of cache sub-directory if (!@file_exists($cache_base_dir)) { @mkdir($cache_base_dir, 0777, true); } $cache_file = rawurlencode($event['kb_archivid']) . '.png'; $cache_file_path = $cache_base_dir . '/' . $cache_file; $polygonURL = str_replace(HV_CACHE_DIR, 'cache', $cache_base_dir) . '/' . rawurlencode($cache_file); $fp = @fopen($cache_file_path, 'wb'); if ($fp !== false) { @fwrite($fp, $canvas); @fclose($fp); } }