public function draw(OsuSignature $signature) { if (empty($this->hexColour)) { $this->hexColour = $signature->getHexColour(); } $composite = new Imagick(); $composite->newPseudoImage($this->getWidth(), $this->getHeight(), "canvas:transparent"); // Background $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel("#555555")); $draw->rectangle(0, 0, $this->getWidth(), $this->getHeight()); $composite->drawImage($draw); // Main bar $level = $signature->getUser()['level']; $xp = $level - floor($level); $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel($this->hexColour)); $draw->rectangle(0, 0, $this->getWidth() * $xp, $this->getHeight()); $composite->drawImage($draw); // Bar end glow $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel('#ffffff')); $draw->setFillOpacity(0.35); $draw->rectangle($this->getWidth() * $xp - $this->getHeight(), 0, $this->getWidth() * $xp, $this->getHeight()); $composite->drawImage($draw); // Text draw & metrics $textDraw = new ImagickDraw(); $textDraw->setFillColor(new ImagickPixel('#555555')); $textDraw->setFontSize(12); $textDraw->setFont(ComponentLabel::FONT_DIRECTORY . ComponentLabel::FONT_REGULAR); $textDraw->setGravity(Imagick::GRAVITY_NORTHWEST); $metrics = $composite->queryFontMetrics($textDraw, 'lv' . floor($level)); // Text white bg $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel('#ffffff')); $draw->rectangle(($this->getWidth() - $metrics['textWidth']) / 2 - 2, 0, ($this->getWidth() + $metrics['textWidth']) / 2 + 1, $this->getHeight()); $composite->drawImage($draw); // Rounding $roundMask = new Imagick(); $roundMask->newPseudoImage($this->getWidth(), $this->getHeight(), "canvas:transparent"); $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel("black")); $draw->roundRectangle(0, 0, $this->getWidth() - 1, $this->getHeight() - 1, $this->rounding, $this->rounding); $roundMask->drawImage($draw); $roundMask->setImageFormat('png'); $composite->compositeImage($roundMask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA); $signature->getCanvas()->compositeImage($composite, Imagick::COMPOSITE_DEFAULT, $this->x, $this->y); // Level text $signature->getCanvas()->annotateImage($textDraw, $this->x + ($this->getWidth() - $metrics['textWidth']) / 2, $this->y + ($this->getHeight() - $metrics['textHeight']) / 2 - 2, 0, 'lv' . floor($level)); }
/** * アバター自動生成処理 * * @param Model $model ビヘイビア呼び出し元モデル * @param array $user ユーザデータ配列 * @return mixed On success Model::$data, false on failure * @throws InternalErrorException */ public function createAvatarAutomatically(Model $model, $user) { //imagickdraw オブジェクトを作成します $draw = new ImagickDraw(); //文字色のセット $draw->setfillcolor('white'); //フォントサイズを 160 に設定します $draw->setFontSize(140); //テキストを追加します $draw->setFont(CakePlugin::path($model->plugin) . 'webroot' . DS . 'fonts' . DS . 'ipaexg.ttf'); $draw->annotation(19, 143, mb_substr(mb_convert_kana($user['User']['handlename'], 'KVA'), 0, 1)); //新しいキャンバスオブジェクトを作成する $canvas = new Imagick(); //ランダムで背景色を指定する $red1 = strtolower(dechex(mt_rand(3, 12))); $red2 = strtolower(dechex(mt_rand(0, 15))); $green1 = strtolower(dechex(mt_rand(3, 12))); $green2 = strtolower(dechex(mt_rand(0, 15))); $blue1 = strtolower(dechex(mt_rand(3, 12))); $blue2 = strtolower(dechex(mt_rand(0, 15))); $canvas->newImage(179, 179, '#' . $red1 . $red2 . $green1 . $green2 . $blue1 . $blue2); //ImagickDraw をキャンバス上に描画します $canvas->drawImage($draw); //フォーマットを PNG に設定します $canvas->setImageFormat('png'); App::uses('TemporaryFolder', 'Files.Utility'); $folder = new TemporaryFolder(); $filePath = $folder->path . DS . Security::hash($user['User']['handlename'], 'md5') . '.png'; $canvas->writeImages($filePath, true); return $filePath; }
/** * this is to force RGB and to apply custom icc color profiles */ protected function applyColorProfiles() { if ($this->resource->getImageColorspace() == Imagick::COLORSPACE_CMYK) { if (self::getCMYKColorProfile() && self::getRGBColorProfile()) { $profiles = $this->resource->getImageProfiles('*', false); // we're only interested if ICC profile(s) exist $has_icc_profile = array_search('icc', $profiles) !== false; // if it doesn't have a CMYK ICC profile, we add one if ($has_icc_profile === false) { $this->resource->profileImage('icc', self::getCMYKColorProfile()); } // then we add an RGB profile $this->resource->profileImage('icc', self::getRGBColorProfile()); $this->resource->setImageColorspace(Imagick::COLORSPACE_RGB); } } // this is a HACK to force grayscale images to be real RGB - truecolor, this is important if you want to use // thumbnails in PDF's because they do not support "real" grayscale JPEGs or PNGs // problem is described here: http://imagemagick.org/Usage/basics/#type // and here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=6888#p31891 if ($this->resource->getimagetype() == Imagick::IMGTYPE_GRAYSCALE) { $draw = new ImagickDraw(); $draw->setFillColor("red"); $draw->setfillopacity(0.001); $draw->point(0, 0); $this->resource->drawImage($draw); } }
public function renderImage() { //Create a ImagickDraw object to draw into. $draw = new \ImagickDraw(); $darkColor = new \ImagickPixel('brown'); //http://www.imagemagick.org/Usage/compose/#compose_terms $draw->setStrokeColor($darkColor); $draw->setFillColor('white'); $draw->setStrokeWidth(2); $draw->setFontSize(72); $draw->setStrokeOpacity(1); $draw->setStrokeColor($darkColor); $draw->setStrokeWidth(2); $draw->setFont("../fonts/CANDY.TTF"); $draw->setFontSize(140); $draw->setFillColor('none'); $draw->rectangle(0, 0, 1000, 300); $draw->setFillColor('white'); $draw->annotation(50, 180, "Lorem Ipsum!"); $imagick = new \Imagick(realpath("images/TestImage.jpg")); $draw->composite(\Imagick::COMPOSITE_MULTIPLY, -500, -200, 2000, 600, $imagick); //Create an image object which the draw commands can be rendered into $imagick = new \Imagick(); $imagick->newImage(1000, 300, "SteelBlue2"); $imagick->setImageFormat("png"); //Render the draw commands in the ImagickDraw object //into the image. $imagick->drawImage($draw); //Send the image to the browser header("Content-Type: image/png"); echo $imagick->getImageBlob(); }
public function makeImageOfCertification() { date_default_timezone_set('UTC'); $timeStamp = date('jS F Y'); $text = "CERTIFIED COPY" . "\n" . $this->serial . "\n" . $timeStamp; $image = new \Imagick(); $draw = new \ImagickDraw(); $color = new \ImagickPixel('#000000'); $background = new \ImagickPixel("rgb(85, 196, 241)"); $draw->setFont($this->container->getParameter('assetic.write_to') . $this->container->get('templating.helper.assets')->getUrl('fonts/futura.ttf')); $draw->setFontSize(24); $draw->setFillColor($color); $draw->setTextAntialias(true); $draw->setStrokeAntialias(true); //Align text to the center of the background $draw->setTextAlignment(\Imagick::ALIGN_CENTER); //Get information of annotation image $metrics = $image->queryFontMetrics($draw, $text); //Calc the distance(pixels) to move the sentences $move = $metrics['textWidth'] / 2; $draw->annotation($move, $metrics['ascender'], $text); //Create an image of certification $image->newImage($metrics['textWidth'], $metrics['textHeight'], $background); $image->setImageFormat('png'); $image->drawImage($draw); //Save an image temporary $image->writeImage("cert_" . $this->serial . "_.png"); }
/** * {@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; }
/** * Return a PNG image representation of barcode (requires GD or Imagick library). * * @param string $code code to print * @param string $type type of barcode: * @param int $widthFactor Width of a single bar element in pixels. * @param int $totalHeight Height of a single bar element in pixels. * @param array $color RGB (0-255) foreground color for bar elements (background is transparent). * @return \Imagick imagick object with barcode * @public */ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0)) { $barcodeData = $this->getBarcodeData($code, $type); // calculate image size $width = $barcodeData['maxWidth'] * $widthFactor; $height = $totalHeight; $colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); $png = new \Imagick(); $png->newImage($width, $height, 'none', 'png'); $imageMagickObject = new \imagickdraw(); $imageMagickObject->setFillColor($colorForeground); $imageMagickObject->setStrokeAntialias(false); $imageMagickObject->setStrokeColor('rgb(255,255,255)'); // print bars $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { $bw = round($bar['width'] * $widthFactor, 3); $bh = round($bar['height'] * $totalHeight / $barcodeData['maxHeight'], 3); if ($bar['drawBar']) { $y = round($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight'], 3); // draw a vertical bar $imageMagickObject->rectangle($positionHorizontal, $y, $positionHorizontal + $bw, $y + $bh); } $positionHorizontal += $bw; } $png->drawImage($imageMagickObject); return $png; }
function writeText($text) { if ($this->printer == null) { throw new LogicException("Not attached to a printer."); } if ($text == null) { return; } $text = trim($text, "\n"); /* Create Imagick objects */ $image = new Imagick(); $draw = new ImagickDraw(); $color = new ImagickPixel('#000000'); $background = new ImagickPixel('white'); /* Create annotation */ //$draw -> setFont('Arial');// (not necessary?) $draw->setFontSize(24); // Size 21 looks good for FONT B $draw->setFillColor($color); $draw->setStrokeAntialias(true); $draw->setTextAntialias(true); $metrics = $image->queryFontMetrics($draw, $text); $draw->annotation(0, $metrics['ascender'], $text); /* Create image & draw annotation on it */ $image->newImage($metrics['textWidth'], $metrics['textHeight'], $background); $image->setImageFormat('png'); $image->drawImage($draw); //$image -> writeImage("test.png"); /* Save image */ $escposImage = new EscposImage(); $escposImage->readImageFromImagick($image); $size = Escpos::IMG_DEFAULT; $this->printer->bitImage($escposImage, $size); }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $format = "png", $quality = 85, $filename = FALSE, $save = TRUE, $print = false) { $imgH = count($frame); $imgW = strlen($frame[0]); $col[0] = new \ImagickPixel("white"); $col[1] = new \ImagickPixel("black"); $image = new \Imagick(); $image->newImage($imgW, $imgH, $col[0]); $image->setCompressionQuality($quality); $image->setImageFormat($format); $draw = new \ImagickDraw(); $draw->setFillColor($col[1]); for ($y = 0; $y < $imgH; $y++) { for ($x = 0; $x < $imgW; $x++) { if ($frame[$y][$x] == '1') { $draw->point($x, $y); } } } $image->drawImage($draw); $image->borderImage($col[0], $outerFrame, $outerFrame); $image->scaleImage(($imgW + 2 * $outerFrame) * $pixelPerPoint, 0); if ($save) { if ($filename === false) { throw new Exception("QR Code filename can't be empty"); } $image->writeImages($filename, true); } if ($print) { Header("Content-type: image/" . $format); echo $image; } }
/** * @return $this */ public function setColorspaceToRGB() { $imageColorspace = $this->resource->getImageColorspace(); if ($imageColorspace == \Imagick::COLORSPACE_CMYK) { if (self::getCMYKColorProfile() && self::getRGBColorProfile()) { $profiles = $this->resource->getImageProfiles('*', false); // we're only interested if ICC profile(s) exist $has_icc_profile = array_search('icc', $profiles) !== false; // if it doesn't have a CMYK ICC profile, we add one if ($has_icc_profile === false) { $this->resource->profileImage('icc', self::getCMYKColorProfile()); } // then we add an RGB profile $this->resource->profileImage('icc', self::getRGBColorProfile()); $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB); // we have to use SRGB here, no clue why but it works } else { $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB); } } else { if ($imageColorspace == \Imagick::COLORSPACE_GRAY) { $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB); } else { if (!in_array($imageColorspace, array(\Imagick::COLORSPACE_RGB, \Imagick::COLORSPACE_SRGB))) { $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB); } else { // this is to handle embedded icc profiles in the RGB/sRGB colorspace $profiles = $this->resource->getImageProfiles('*', false); $has_icc_profile = array_search('icc', $profiles) !== false; if ($has_icc_profile) { try { // if getImageColorspace() says SRGB but the embedded icc profile is CMYK profileImage() will throw an exception $this->resource->profileImage('icc', self::getRGBColorProfile()); } catch (\Exception $e) { \Logger::warn($e); } } } } } // this is a HACK to force grayscale images to be real RGB - truecolor, this is important if you want to use // thumbnails in PDF's because they do not support "real" grayscale JPEGs or PNGs // problem is described here: http://imagemagick.org/Usage/basics/#type // and here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=6888#p31891 $currentLocale = setlocale(LC_ALL, "0"); // this locale hack thing is also a hack for imagick setlocale(LC_ALL, ""); // details see https://www.pimcore.org/issues/browse/PIMCORE-2728 $draw = new \ImagickDraw(); $draw->setFillColor("#ff0000"); $draw->setfillopacity(0.01); $draw->point(floor($this->getWidth() / 2), floor($this->getHeight() / 2)); // place it in the middle of the image $this->resource->drawImage($draw); setlocale(LC_ALL, $currentLocale); // see setlocale() above, for details ;-) return $this; }
/** * @see wcf\system\image\adapter\IImageAdapter::drawText() */ public function drawText($string, $x, $y) { $draw = new \ImagickDraw(); $draw->setFillColor($this->color); $draw->setTextAntialias(true); // draw text $draw->annotation($x, $y, $string); $this->imagick->drawImage($draw); }
/** * @param \Imagick $imagick * @param int $graphWidth * @param int $graphHeight */ public static function analyzeImage(\Imagick $imagick, $graphWidth = 255, $graphHeight = 127) { $sampleHeight = 20; $border = 2; $imagick->transposeImage(); $imagick->scaleImage($graphWidth, $sampleHeight); $imageIterator = new \ImagickPixelIterator($imagick); $luminosityArray = []; foreach ($imageIterator as $row => $pixels) { /* Loop through pixel rows */ foreach ($pixels as $column => $pixel) { /* Loop through the pixels in the row (columns) */ /** @var $pixel \ImagickPixel */ if (false) { $color = $pixel->getColor(); $luminosityArray[] = $color['r']; } else { $hsl = $pixel->getHSL(); $luminosityArray[] = $hsl['luminosity']; } } /* Sync the iterator, this is important to do on each iteration */ $imageIterator->syncIterator(); break; } $draw = new \ImagickDraw(); $strokeColor = new \ImagickPixel('red'); $fillColor = new \ImagickPixel('red'); $draw->setStrokeColor($strokeColor); $draw->setFillColor($fillColor); $draw->setStrokeWidth(0); $draw->setFontSize(72); $draw->setStrokeAntiAlias(true); $previous = false; $x = 0; foreach ($luminosityArray as $luminosity) { $pos = $graphHeight - 1 - $luminosity * ($graphHeight - 1); if ($previous !== false) { /** @var $previous int */ //printf ( "%d, %d, %d, %d <br/>\n" , $x - 1, $previous, $x, $pos); $draw->line($x - 1, $previous, $x, $pos); } $x += 1; $previous = $pos; } $plot = new \Imagick(); $plot->newImage($graphWidth, $graphHeight, 'white'); $plot->drawImage($draw); $outputImage = new \Imagick(); $outputImage->newImage($graphWidth, $graphHeight + $sampleHeight, 'white'); $outputImage->compositeimage($plot, \Imagick::COMPOSITE_ATOP, 0, 0); $outputImage->compositeimage($imagick, \Imagick::COMPOSITE_ATOP, 0, $graphHeight); $outputImage->borderimage('black', $border, $border); $outputImage->setImageFormat("png"); App::cachingHeader("Content-Type: image/png"); echo $outputImage; }
function blueDiscAlpha($width, $height) { $imagick = new Imagick(); $imagick->newImage($width, $height, 'none'); $draw = new ImagickDraw(); $draw->setStrokeOpacity(0); $draw->setFillColor('blue'); $draw->circle(2 * $width / 3, 2 * $height / 3, $width - ($width / 3 - $width / 4), 2 * $height / 3); $imagick->drawImage($draw); return $imagick; }
public static function createDefaultLogo($path) { try { $image = new \Imagick(); $image->newImage(90, 90, new \ImagickPixel("white")); $draw = new \ImagickDraw(); $draw->setFillColor(new \ImagickPixel("#" . mt_rand(100000, 999999))); $draw->ellipse(45, 45, 45, 45, 0, 360); $image->drawImage($draw); //$draw->setFillColor( new ImagickPixel( "#".mt_rand(100000, 999999) ) );#01C3EB $draw->setFillColor(new \ImagickPixel("#418bc9")); $draw->ellipse(45, 45, 20, 20, 0, 360); $image->drawImage($draw); $image->setImageFormat("png"); $image_name = 'image_' . time() . '.png'; $image->writeImage($path . $image_name); return $image_name; } catch (Exception $e) { //echo $e->getMessage(); return false; } }
/** * Draw a polyline (a non-closed, non-filled 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 line with. * @param string $width The width of the line. */ public function polyline($verts, $color, $width = 1) { $draw = new ImagickDraw(); $draw->setStrokeColor(new ImagickPixel($color)); $draw->setStrokeWidth($width); $draw->setFillColor(new ImagickPixel('none')); $draw->polyline($verts); try { $res = $this->_imagick->drawImage($draw); } catch (ImagickException $e) { throw new Horde_Image_Exception($e); } $draw->destroy(); }
protected function getImage() { $image = new \Imagick(); $image->newImage(2, 2, new \ImagickPixel('white')); $draw = new \ImagickDraw(); $draw->setFillColor(new \ImagickPixel('black')); $draw->color(0, 1, \Imagick::PAINT_POINT); $draw2 = new \ImagickDraw(); $draw2->setFillColor(new \ImagickPixel('black')); $draw2->color(1, 1, \Imagick::PAINT_POINT); $image->drawImage($draw); $image->drawimage($draw2); $image->setformat('png'); return $image; }
/** * @return $this */ public function setColorspaceToRGB() { $imageColorspace = $this->resource->getImageColorspace(); if ($imageColorspace == \Imagick::COLORSPACE_CMYK) { if (self::getCMYKColorProfile() && self::getRGBColorProfile()) { $profiles = $this->resource->getImageProfiles('*', false); // we're only interested if ICC profile(s) exist $has_icc_profile = array_search('icc', $profiles) !== false; // if it doesn't have a CMYK ICC profile, we add one if ($has_icc_profile === false) { $this->resource->profileImage('icc', self::getCMYKColorProfile()); } // then we add an RGB profile $this->resource->profileImage('icc', self::getRGBColorProfile()); $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB); // we have to use SRGB here, no clue why but it works } else { $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB); } } else { if ($imageColorspace == \Imagick::COLORSPACE_GRAY) { $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB); } else { if (!in_array($imageColorspace, array(\Imagick::COLORSPACE_RGB, \Imagick::COLORSPACE_SRGB))) { $this->resource->setImageColorspace(\Imagick::COLORSPACE_SRGB); } else { // this is to handle embedded icc profiles in the RGB/sRGB colorspace $profiles = $this->resource->getImageProfiles('*', false); $has_icc_profile = array_search('icc', $profiles) !== false; if ($has_icc_profile) { $this->resource->profileImage('icc', self::getRGBColorProfile()); } } } } // this is a HACK to force grayscale images to be real RGB - truecolor, this is important if you want to use // thumbnails in PDF's because they do not support "real" grayscale JPEGs or PNGs // problem is described here: http://imagemagick.org/Usage/basics/#type // and here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=6888#p31891 $draw = new \ImagickDraw(); $draw->setFillColor("#ff0000"); $draw->setfillopacity(0.01); $draw->point(floor($this->getWidth() / 2), floor($this->getHeight() / 2)); // place it in the middle of the image $this->resource->drawImage($draw); return $this; }
function a0nCRQ($msg, $padx, $pady, $bc, $fc, $tc) { $im = new Imagick(); $idraw = new ImagickDraw(); $idraw->setFontSize(30); $idraw->setFont('MyriadPro-Regular.otf'); $idraw->setGravity(Imagick::GRAVITY_CENTER); $metrics = $im->queryFontMetrics($idraw, $msg); $im->newPseudoImage($metrics["textWidth"] + $padx * 2, $metrics["textHeight"] + $pady * 2, "xc:none"); $idraw->setFillColor($fc); $idraw->setStrokeColor($bc); $idraw->roundrectangle(0, 0, $metrics["textWidth"] + $padx * 2 - 1, $metrics["textHeight"] + $pady * 2 - 1, 10, 10); $idraw->setFillColor($tc); $idraw->setStrokeColor($tc); $idraw->annotation(0, 0, $msg); $im->drawImage($idraw); return $im; }
/** * @see \wcf\system\image\adapter\IImageAdapter::drawText() */ public function drawText($text, $x, $y, $font, $size, $opacity = 1) { $draw = new \ImagickDraw(); $draw->setFillOpacity($opacity); $draw->setFillColor($this->color); $draw->setTextAntialias(true); $draw->setFont($font); $draw->setFontSize($size); // draw text $draw->annotation($x, $y, $text); if ($this->imagick->getImageFormat() == 'GIF') { $this->imagick = $this->imagick->coalesceImages(); do { $this->imagick->drawImage($draw); } while ($this->imagick->nextImage()); } else { $this->imagick->drawImage($draw); } }
/** * Draws an arc. * * @param integer $x The x coordinate of the centre. * @param integer $y The y coordinate of the centre. * @param integer $r The radius of the arc. * @param integer $start The start angle of the arc. * @param integer $end The end angle of the arc. * @param string $color The line color of the arc. * @param string $fill The fill color of the arc (defaults to none). */ public function arc($x, $y, $r, $start, $end, $color = 'black', $fill = 'none') { $points = Horde_Image::arcPoints($r, $start, $end); $points['x1'] += $x; $points['x2'] += $x; $points['x3'] += $x; $points['y1'] += $y; $points['y2'] += $y; $points['y3'] += $y; try { $draw = new ImagickDraw(); $draw->setStrokeColor(new ImagickPixel($color)); $draw->setFillColor(new ImagickPixel($fill)); $draw->arc($x - $r, $y - $r, $x + $r, $y + $r, $start, $end); } catch (ImagickDrawException $e) { throw new Horde_Image_Exception($e); } catch (ImagickPixelException $e) { throw new Horde_Image_Exception($e); } // If filled, draw the outline. if (!empty($fill)) { $mid = round(($start + $end) / 2); list($x1, $y1) = Horde_Image::circlePoint($start, $r * 2); list($x2, $y2) = Horde_Image::circlePoint($mid, $r * 2); list($x3, $y3) = Horde_Image::circlePoint($end, $r * 2); $verts = array(array('x' => $x + round($x3), 'y' => $y + round($y3)), array('x' => $x, 'y' => $y), array('x' => $x + round($x1), 'y' => $y + round($y1))); if ($mid > 90) { $verts1 = array(array('x' => $x + round($x2), 'y' => $y + round($y2)), array('x' => $x, 'y' => $y), array('x' => $x + round($x1), 'y' => $y + round($y1))); $verts2 = array(array('x' => $x + round($x3), 'y' => $y + round($y3)), array('x' => $x, 'y' => $y), array('x' => $x + round($x2), 'y' => $y + round($y2))); $this->polygon($verts1, $fill, $fill); $this->polygon($verts2, $fill, $fill); } else { $this->polygon($verts, $fill, $fill); } $this->polyline($verts, $color); } try { $this->_imagick->drawImage($draw); } catch (ImagickException $e) { throw new Horde_Image_Exception($e); } $draw->destroy(); }
public function writeText($text) { if ($this->printer == null) { throw new LogicException("Not attached to a printer."); } if ($text == null) { return; } $text = trim($text, "\n"); /* Create Imagick objects */ $image = new \Imagick(); $draw = new \ImagickDraw(); $color = new \ImagickPixel('#000000'); $background = new \ImagickPixel('white'); /* Create annotation */ if ($this->font !== null) { // Allow fallback on defaults as necessary $draw->setFont($this->font); } /* In Arial, size 21 looks good as a substitute for FONT_B, 24 for FONT_A */ $draw->setFontSize($this->fontSize); $draw->setFillColor($color); $draw->setStrokeAntialias(true); $draw->setTextAntialias(true); $metrics = $image->queryFontMetrics($draw, $text); $draw->annotation(0, $metrics['ascender'], $text); /* Create image & draw annotation on it */ $image->newImage($metrics['textWidth'], $metrics['textHeight'], $background); $image->setImageFormat('png'); $image->drawImage($draw); // debugging if you want to view the images yourself //$image -> writeImage("test.png"); /* Save image */ $escposImage = new ImagickEscposImage(); $escposImage->readImageFromImagick($image); $size = Printer::IMG_DEFAULT; $this->printer->bitImage($escposImage, $size); }
function renderImage() { //Create a ImagickDraw object to draw into. $draw = new \ImagickDraw(); $darkColor = new \ImagickPixel('brown'); //http://www.imagemagick.org/Usage/compose/#compose_terms $draw->setStrokeColor($darkColor); $draw->setFillColor('white'); $draw->setStrokeWidth(2); $draw->setFontSize(72); $draw->setStrokeOpacity(1); $draw->setStrokeColor($darkColor); $draw->setStrokeWidth(2); $draw->setFont("../fonts/CANDY.TTF"); $draw->setFontSize(140); $draw->setFillColor('none'); $draw->rectangle(0, 0, 1000, 300); $draw->setFillColor('white'); $draw->annotation(50, 180, "Lorem Ipsum!"); $imagick = new \Imagick(realpath("images/TestImage.jpg")); // $compositeModes = [ // // \Imagick::COMPOSITE_NO, \Imagick::COMPOSITE_ADD, \Imagick::COMPOSITE_ATOP, \Imagick::COMPOSITE_BLEND, \Imagick::COMPOSITE_BUMPMAP, \Imagick::COMPOSITE_CLEAR, \Imagick::COMPOSITE_COLORBURN, \Imagick::COMPOSITE_COLORDODGE, \Imagick::COMPOSITE_COLORIZE, \Imagick::COMPOSITE_COPYBLACK, \Imagick::COMPOSITE_COPYBLUE, \Imagick::COMPOSITE_COPY, \Imagick::COMPOSITE_COPYCYAN, \Imagick::COMPOSITE_COPYGREEN, \Imagick::COMPOSITE_COPYMAGENTA, \Imagick::COMPOSITE_COPYOPACITY, \Imagick::COMPOSITE_COPYRED, \Imagick::COMPOSITE_COPYYELLOW, \Imagick::COMPOSITE_DARKEN, \Imagick::COMPOSITE_DSTATOP, \Imagick::COMPOSITE_DST, \Imagick::COMPOSITE_DSTIN, \Imagick::COMPOSITE_DSTOUT, \Imagick::COMPOSITE_DSTOVER, \Imagick::COMPOSITE_DIFFERENCE, \Imagick::COMPOSITE_DISPLACE, \Imagick::COMPOSITE_DISSOLVE, \Imagick::COMPOSITE_EXCLUSION, \Imagick::COMPOSITE_HARDLIGHT, \Imagick::COMPOSITE_HUE, \Imagick::COMPOSITE_IN, \Imagick::COMPOSITE_LIGHTEN, \Imagick::COMPOSITE_LUMINIZE, \Imagick::COMPOSITE_MINUS, \Imagick::COMPOSITE_MODULATE, \Imagick::COMPOSITE_MULTIPLY, \Imagick::COMPOSITE_OUT, \Imagick::COMPOSITE_OVER, \Imagick::COMPOSITE_OVERLAY, \Imagick::COMPOSITE_PLUS, \Imagick::COMPOSITE_REPLACE, \Imagick::COMPOSITE_SATURATE, \Imagick::COMPOSITE_SCREEN, \Imagick::COMPOSITE_SOFTLIGHT, \Imagick::COMPOSITE_SRCATOP, \Imagick::COMPOSITE_SRC, \Imagick::COMPOSITE_SRCIN, \Imagick::COMPOSITE_SRCOUT, \Imagick::COMPOSITE_SRCOVER, \Imagick::COMPOSITE_SUBTRACT, \Imagick::COMPOSITE_THRESHOLD, \Imagick::COMPOSITE_XOR, // // ]; $draw->composite(\Imagick::COMPOSITE_MULTIPLY, -500, -200, 2000, 600, $imagick); //Create an image object which the draw commands can be rendered into $imagick = new \Imagick(); $imagick->newImage(1000, 300, "SteelBlue2"); $imagick->setImageFormat("png"); //Render the draw commands in the ImagickDraw object //into the image. $imagick->drawImage($draw); //Send the image to the browser header("Content-Type: image/png"); echo $imagick->getImageBlob(); }
/** * function filter_render_type * Returns HTML markup containing an image with a data: URI * @param string $content The text to be rendered * @param string $font_file The path to a font file in a format ImageMagick can handle * @param integer $font_size The font size, in pixels (defaults to 28) * @param string $font_color The font color (defaults to 'black') * @param string $background_color The background color (defaults to 'transparent') * @param string $output_format The image format to use (defaults to 'png') * @return string HTML markup containing the rendered image **/ public function filter_render_type($content, $font_file, $font_size, $font_color, $background_color, $output_format, $width) { // Preprocessing $content // 1. Strip HTML tags. It would be better to support them, but we just strip them for now. // 2. Decode HTML entities to UTF-8 charaaters. $content = html_entity_decode(strip_tags($content), ENT_QUOTES, 'UTF-8'); // 3. Do word wrap when $width is specified. Not work for double-width characters. if (is_int($width)) { $content = wordwrap($content, floor($width / (0.3 * $font_size))); } $cache_group = strtolower(get_class($this)); $cache_key = $font_file . $font_size . $font_color . $background_color . $output_format . $content . $width; if (!Cache::has(array($cache_group, md5($cache_key)))) { $font_color = new ImagickPixel($font_color); $background_color = new ImagickPixel($background_color); $draw = new ImagickDraw(); $draw->setFont($font_file); $draw->setFontSize($font_size); $draw->setFillColor($font_color); $draw->setTextEncoding('UTF-8'); $draw->annotation(0, $font_size * 2, $content); $canvas = new Imagick(); $canvas->newImage(1000, $font_size * 5, $background_color); $canvas->setImageFormat($output_format); $canvas->drawImage($draw); // The following line ensures that the background color is set in the PNG // metadata, when using that format. This allows you, by specifying an RGBa // background color (e.g. #ffffff00) to create PNGs with a transparent background // for browsers that support it but with a "fallback" background color (the RGB // part of the RGBa color) for IE6, which does not support alpha in PNGs. $canvas->setImageBackgroundColor($background_color); $canvas->trimImage(0); Cache::set(array($cache_group, md5($cache_key)), $canvas->getImageBlob()); } return '<img class="rendered-type" src="' . URL::get('display_rendertype', array('hash' => md5($cache_key), 'format' => $output_format)) . '" title="' . $content . '" alt="' . $content . '">'; }
/** * Color blend filter, more advanced version of colorize. * * Code by olav@redwall.ee on http://php.net/manual/en/imagick.colorizeimage.php * * @param $imageInstance * @param $color * @param int $alpha * @param int $composite_flag */ private function _colorBlend($imagickInstance, $color, $alpha = 1, $composite_flag = \Imagick::COMPOSITE_COLORIZE) { $draw = new \ImagickDraw(); $draw->setFillColor($color); $width = $imagickInstance->getImageWidth(); $height = $imagickInstance->getImageHeight(); $draw->rectangle(0, 0, $width, $height); $temporary = new \Imagick(); $temporary->setBackgroundColor(new \ImagickPixel('transparent')); $temporary->newImage($width, $height, new \ImagickPixel('transparent')); $temporary->setImageFormat('png32'); $temporary->drawImage($draw); $alphaChannel = clone $imagickInstance; $alphaChannel->setImageAlphaChannel(\Imagick::ALPHACHANNEL_EXTRACT); $alphaChannel->negateImage(false, \Imagick::CHANNEL_ALL); $imagickInstance->setImageClipMask($alphaChannel); $clone = clone $imagickInstance; $clone->compositeImage($temporary, $composite_flag, 0, 0); $clone->setImageOpacity($alpha); $imagickInstance->compositeImage($clone, \Imagick::COMPOSITE_DEFAULT, 0, 0); }
/** * 加给图片加水印 * * @param strimg $groundImage 要加水印地址 * @param int $waterPos 水印位置 * @param string $waterImage 水印图片地址 * @param string $waterText 文本文字 * @param int $textFont 文字大小 * @param string $textColor 文字颜色 * @param int $minWidth 小于此值不加水印 * @param int $minHeight 小于此值不加水印 * @param float $alpha 透明度 * @return FALSE */ public static function waterMark($groundImage, $waterPos = 0, $waterImage = "", $waterText = "", $textFont = 15, $textColor = "#FF0000", $minWidth = '100', $minHeight = '100', $alpha = 0.9) { $isWaterImg = FALSE; $bg_h = $bg_w = $water_h = $water_w = 0; //获取背景图的高,宽 if (is_file($groundImage) && !empty($groundImage)) { $bg = new Imagick(); $bg->readImage($groundImage); $bg_h = $bg->getImageHeight(); $bg_w = $bg->getImageWidth(); } //获取水印图的高,宽 if (is_file($waterImage) && !empty($waterImage)) { $water = new Imagick($waterImage); $water_h = $water->getImageHeight(); $water_w = $water->getImageWidth(); } //如果背景图的高宽小于水印图的高宽或指定的高和宽则不加水印 if ($bg_h < $minHeight || $bg_w < $minWidth || $bg_h < $water_h || $bg_w < $water_w) { return; } else { $isWaterImg = TRUE; } //加水印 if ($isWaterImg) { $dw = new ImagickDraw(); //加图片水印 if (is_file($waterImage)) { $water->setImageOpacity($alpha); $dw->setGravity($waterPos); $dw->composite($water->getImageCompose(), 0, 0, 50, 0, $water); $bg->drawImage($dw); if (!$bg->writeImage($groundImage)) { return FALSE; } } else { //加文字水印 $dw->setFontSize($textFont); $dw->setFillColor($textColor); $dw->setGravity($waterPos); $dw->setFillAlpha($alpha); $dw->annotation(0, 0, $waterText); $bg->drawImage($dw); if (!$bg->writeImage($groundImage)) { return FALSE; } } } }
function getWhiteDisc() { $width = $this->width; $height = $this->height; $imagick = new \Imagick(); $imagick->newImage($width, $height, 'black'); $draw = new \ImagickDraw(); $draw->setStrokeOpacity(0); $draw->setFillColor('white'); $draw->circle($width / 2, $height / 2, $width / 4, $height / 2); $imagick->drawImage($draw); return $imagick; }
$runningHt = 15; for ($i = count($legends) - 1; $i >= 0; $i--) { $p = explode("\n", $titles[$i]); $draw = new ImagickDraw(); $draw->setFont('Helvetica'); $draw->setFontSize(12); $draw->annotation(5, $runningHt, $titles[$i]); $canvas->drawImage($draw); $canvas->compositeImage($legends[$i], imagick::COMPOSITE_OVER, 0, $runningHt + 12 * (count($p) - 1)); $runningHt += $legends[$i]->getImageHeight() + 20 + 12 * (count($p) - 1); } $canvas->writeImage($tmp_dir . $id . '.legend.png'); // title $canvas = new Imagick(); $canvas->newImage($w, 30, new ImagickPixel('white')); $canvas->setImageFormat('png'); $draw = new ImagickDraw(); $draw->setFont('Helvetica'); $draw->setFontSize(18); $draw->setGravity(imagick::GRAVITY_CENTER); $draw->annotation(0, 0, $_REQUEST['title']); $canvas->drawImage($draw); $canvas->writeImage($tmp_dir . $id . '.title.png'); function mkLegendUrl($u) { return preg_replace('/&(STYLES|SRS)[^&]+/', '', $u); } $handle = fopen($tmp_dir . $id . '.html', 'w'); fwrite($handle, "<html><head><title>" . $_REQUEST['title'] . "</title><style>td {vertical-align : top} img {border : 1px solid gray}</style></head><body><table><tr><td><img src='{$tmp_url}{$id}.title.png'></td></tr><tr><td><img src='{$tmp_url}{$id}.png'></td><td><img src='{$tmp_url}{$id}.legend.png'></td></tr></table></body></html>"); fclose($handle); echo json_encode(array('html' => "{$tmp_url}{$id}.html", 'map' => "{$tmp_url}{$id}.png", 'legend' => "{$tmp_url}{$id}.legend.png"));
// Get font metrics $metrics = $image->queryFontMetrics($draw, $text); // draw the text $topborder = 2; $spaceatend = 5; echo '<li>generiere Text</li>'; flush(); $draw->annotation(0, $topborder + $metrics['ascender'], $text); // create image of correct size echo '<li>generiere Bild</li>'; flush(); $image->newImage($spaceatend + $metrics['textWidth'], $topborder + $metrics['textHeight'], $background); $image->setImageFormat('png'); echo '<li>zeichne Text ins Bild</li>'; flush(); $image->drawImage($draw); // save to queue dir $queuefilename = strftime('%Y-%m-%d_%H.%M.%S') . '_generated_text'; echo '<li>speichere Bild</li>'; flush(); file_put_contents($imagequeuedir . '/' . $queuefilename, $image); // add to queue echo '<li>Füge Bild zum Strickmuster hinzu</li>'; ayabJsonCall('/queue', array('addFile' => $imagequeuedir . '/' . $queuefilename, 'webURL' => $imagequeueurl . '/' . $queuefilename), true); echo '<li>fertig!</li></ul></div>'; flush(); ob_start(); /* // %%% just output for now header("Content-Type: image/png"); echo $image;
$this->draw->pathMoveToAbsolute($positions[0][0], $positions[0][1]); //Scale that by the 'value' of each point aka the distance from the chart's centre. //Also scale it by how rounded you want the chart. for ($i = 0; $i < $numberOfPoints; $i++) { list($nextPositionX, $nextPositionY) = $positions[($i + 1) % $numberOfPoints]; list($controlPoint1X, $controlPoint1Y) = $this->getControlPoint($points[$i], $positions[$i], $tangents[$i], 1, $roundness, count($points)); list($controlPoint2X, $controlPoint2Y) = $this->getControlPoint($points[($i + 1) % $numberOfPoints], $positions[($i + 1) % $numberOfPoints], $tangents[($i + 1) % $numberOfPoints], -1, $roundness, count($points)); $this->draw->pathCurveToAbsolute($controlPoint1X, $controlPoint1Y, $controlPoint2X, $controlPoint2Y, $nextPositionX, $nextPositionY); } $this->draw->pathClose(); $this->draw->pathFinish(); foreach ($this->points as $point) { $this->draw->circle($point[0], $point[1], $point[2], $point[3]); } } } function getValues() { $coordinates_1 = [200, 200, 100, 200, 220, 200]; return $coordinates_1; } $spiderChart = new SpiderChart(); $spiderChart->drawChartBackground(); $points = getValues(); //$spiderChart->drawBlendedChart($points, 0.2); $spiderChart->drawBezierChart($points, 0.4); $image->drawImage($spiderChart->getDraw()); if (!debug) { header('Content-Type: image/png'); echo $image; }
function renderKernel(ImagickKernel $imagickKernel) { $matrix = $imagickKernel->getMatrix(); $imageMargin = 20; $tileSize = 20; $tileSpace = 4; $shadowSigma = 4; $shadowDropX = 20; $shadowDropY = 0; $radius = $tileSize / 2 * 0.9; $rows = count($matrix); $columns = count($matrix[0]); $imagickDraw = new \ImagickDraw(); $imagickDraw->setFillColor('#afafaf'); $imagickDraw->setStrokeColor('none'); $imagickDraw->translate($imageMargin, $imageMargin); $imagickDraw->push(); ksort($matrix); foreach ($matrix as $row) { ksort($row); $imagickDraw->push(); foreach ($row as $cell) { if ($cell !== false) { $color = intval(255 * $cell); $colorString = sprintf("rgb(%f, %f, %f)", $color, $color, $color); $imagickDraw->setFillColor($colorString); $imagickDraw->rectangle(0, 0, $tileSize, $tileSize); } $imagickDraw->translate($tileSize + $tileSpace, 0); } $imagickDraw->pop(); $imagickDraw->translate(0, $tileSize + $tileSpace); } $imagickDraw->pop(); $width = $columns * $tileSize + ($columns - 1) * $tileSpace; $height = $rows * $tileSize + ($rows - 1) * $tileSpace; $imagickDraw->push(); $imagickDraw->translate($width / 2, $height / 2); $imagickDraw->setFillColor('rgba(0, 0, 0, 0)'); $imagickDraw->setStrokeColor('white'); $imagickDraw->circle(0, 0, $radius - 1, 0); $imagickDraw->setStrokeColor('black'); $imagickDraw->circle(0, 0, $radius, 0); $imagickDraw->pop(); $canvasWidth = $width + 2 * $imageMargin; $canvasHeight = $height + 2 * $imageMargin; $kernel = new \Imagick(); $kernel->newPseudoImage($canvasWidth, $canvasHeight, 'canvas:none'); $kernel->setImageFormat('png'); $kernel->drawImage($imagickDraw); /* create drop shadow on it's own layer */ $canvas = $kernel->clone(); $canvas->setImageBackgroundColor(new \ImagickPixel('rgb(0, 0, 0)')); $canvas->shadowImage(100, $shadowSigma, $shadowDropX, $shadowDropY); $canvas->setImagePage($canvasWidth, $canvasHeight, -5, -5); $canvas->cropImage($canvasWidth, $canvasHeight, 0, 0); /* composite original text_layer onto shadow_layer */ $canvas->compositeImage($kernel, \Imagick::COMPOSITE_OVER, 0, 0); $canvas->setImageFormat('png'); return $canvas; }