/** * アバター自動生成処理 * * @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; }
private function createImage() { /* Create Imagick object */ $this->Imagick = new \Imagick(); /* Create the ImagickPixel object (used to set the background color on image) */ $bg = new \ImagickPixel(); /* Set the pixel color to white */ $bg->setColor($this->bg_color); /* Create a drawing object and set the font size */ $ImagickDraw = new \ImagickDraw(); /* Set font and font size. You can also specify /path/to/font.ttf */ if ($this->font) { $ImagickDraw->setFont($this->font); } $ImagickDraw->setFontSize($this->font_size); /* Create new empty image */ $this->Imagick->newImage($this->image_width, $this->image_height, $bg); /* Write the text on the image */ $this->Imagick->annotateImage($ImagickDraw, $this->text_position_left, $this->text_position_top, 0, $this->getCaptchaText()); /* Add some swirl */ $this->Imagick->swirlImage(20); /* Create a few random lines */ $ImagickDraw->line(rand(0, $this->image_width), rand(0, $this->image_height), rand(0, $this->image_width), rand(0, $this->image_height)); $ImagickDraw->line(rand(0, $this->image_width), rand(0, $this->image_height), rand(0, $this->image_width), rand(0, $this->image_height)); $ImagickDraw->line(rand(0, $this->image_width), rand(0, $this->image_height), rand(0, $this->image_width), rand(0, $this->image_height)); $ImagickDraw->line(rand(0, $this->image_width), rand(0, $this->image_height), rand(0, $this->image_width), rand(0, $this->image_height)); $ImagickDraw->line(rand(0, $this->image_width), rand(0, $this->image_height), rand(0, $this->image_width), rand(0, $this->image_height)); /* Draw the ImagickDraw object contents to the image. */ $this->Imagick->drawImage($ImagickDraw); /* Give the image a format */ $this->Imagick->setImageFormat($this->image_format); }
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(); }
function page_testfonts() { //load background $im = new Imagick(); $im->newimage(800, 10000, 'lightgray'); $y = 10; $i = 1; foreach ($im->queryFonts() as $font) { $insert_image = new Imagick(); $insert_image->newImage(600, 30, 'whitesmoke'); $insert_image->setImageFormat("png"); $draw = new ImagickDraw(); $draw->setFont($font); $draw->setFontSize(25); $draw->setFillColor(new ImagickPixel('black')); $draw->setgravity(imagick::GRAVITY_NORTH); $insert_image->annotateImage($draw, 0, 0, 0, $i . '.' . $font); $im->compositeImage($insert_image, $insert_image->getImageCompose(), 100, $y); $y += 30; $i++; } $im->setImageFormat('jpg'); header('Content-Type: image/jpg'); echo $im; }
/** * Calculates size for bounding box of string written in given font. * * @param string $string * @param FontInterface $font * * @return bool|Box Instance of Box object, false on error */ public static function calculateTextSize($string, FontInterface $font) { $imagine = Tygh::$app['image']; if ($imagine instanceof \Imagine\Imagick\Imagine && class_exists('ImagickDraw')) { $text = new \ImagickDraw(); $text->setFont($font->getFile()); if (version_compare(phpversion("imagick"), "3.0.2", ">=")) { $text->setResolution(96, 96); $text->setFontSize($font->getSize()); } else { $text->setFontSize((int) ($font->getSize() * (96 / 72))); } $imagick = new \Imagick(); $info = $imagick->queryFontMetrics($text, $string); $text->clear(); $text->destroy(); $imagick->clear(); $imagick->destroy(); return new Box($info['textWidth'], $info['textHeight']); } if ($imagine instanceof \Imagine\Gd\Imagine && function_exists('imagettfbbox')) { $ttfbbox = imagettfbbox($font->getSize(), 0, $font->getFile(), $string); return new Box(abs($ttfbbox[2]), abs($ttfbbox[7])); } return false; }
/** * Creates a new Label component. * * @param OsuSignature $signature The base signature * @param int $x The X position of this label * @param int $y The Y position of this label * @param string $text The text of this label * @param string $font The font to use for this label; can be a string or the constants defined in {@link ComponentLabel} * @param string $colour The colour of the text of the label * @param int $fontSize The size of the font of the label * @param int $textAlignment The text alignment * @param int $width Width of the label, set to -1 to use the text size, anything bigger can be used to spoof the component system. -2 makes the component sizeless * @param int $height Height of the label, set to -1 to use the text size, anything bigger can be used to spoof the component system */ public function __construct(OsuSignature $signature, $x = 0, $y = 0, $text, $font = self::FONT_REGULAR, $colour = '#555555', $fontSize = 14, $textAlignment = Imagick::ALIGN_UNDEFINED, $width = -1, $height = -1) { parent::__construct($signature, $x, $y); $this->text = $text; $this->font = $font; $this->colour = $colour; $this->fontSize = $fontSize; $this->textAlignment = $textAlignment; $this->width = $width; $this->height = $height; $this->drawSettings = new ImagickDraw(); $this->drawSettings->setFont(self::FONT_DIRECTORY . $font); $this->drawSettings->setFontSize($fontSize); $this->drawSettings->setTextAlignment($textAlignment); $this->drawSettings->setFillColor($colour); $this->usesSpace = $width != -2; if ($width <= -1 || $height <= -1) { $tempImg = new Imagick(); $metrics = $tempImg->queryFontMetrics($this->drawSettings, $this->text); $this->width = $width <= -1 ? $metrics['textWidth'] : $width; // yeah i have to do some bullshit $this->actualWidth = $metrics['textWidth']; $this->height = $metrics['boundingBox']['y2'] - $this->y; } }
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"); }
/** * (non-PHPdoc) * @see Imagine\Image\FontInterface::box() */ public function box($string, $angle = 0) { $text = new \ImagickDraw(); $text->setFont($this->file); $text->setFontSize($this->size); $info = $this->imagick->queryFontMetrics($text, $string); $box = new Box($info['textWidth'], $info['textHeight']); return $box; }
public function printText($canvas, $size, $angle, $x, $y, $color, $fontfile, $text, $opacity = 100) { $draw = new ImagickDraw(); $draw->setFillColor(self::rgb($color)); $draw->setFontSize($size * $this->fontSizeScale); $draw->setFont($fontfile); $draw->setFillOpacity($opacity / 100); $canvas->annotateImage($draw, $x, $y, $angle, $text); }
function test_annotation(&$canvas) { $draw = new ImagickDraw(); $font = __DIR__ . '/php_imagick_tests/anonymous_pro_minus.ttf'; $draw->setFont($font); $draw->setFontSize(40); $draw->annotation(50, 440, 'Yukkuri shiteitte ne!!!'); $canvas->drawImage($draw); }
function render() { $text = "Lorem ipsum"; $im = new \Imagick(); $draw = new \ImagickDraw(); $draw->setStrokeColor("none"); $draw->setFont("../fonts/Arial.ttf"); $draw->setFontSize(96); $draw->setTextAlignment(\Imagick::ALIGN_LEFT); $metrics = $im->queryFontMetrics($draw, $text); return print_table($metrics); }
public function getImagineImage(ImagineInterface $imagine, FontCollection $fontCollection, $width, $height) { $fontPath = $fontCollection->getFontById($this->font)->getPath(); if ($this->getAbsoluteSize() !== null) { $fontSize = $this->getAbsoluteSize(); } elseif ($this->getRelativeSize() !== null) { $fontSize = (int) $this->getRelativeSize() / 100 * $height; } else { throw new \LogicException('Either relative or absolute watermark size must be set!'); } if (true || !class_exists('ImagickDraw')) { // Fall back to ugly image. $palette = new \Imagine\Image\Palette\RGB(); $font = $imagine->font($fontPath, $fontSize, $palette->color('#000')); $box = $font->box($this->getText()); $watermarkImage = $imagine->create($box, $palette->color('#FFF')); $watermarkImage->draw()->text($this->text, $font, new \Imagine\Image\Point(0, 0)); } else { // CURRENTLY DISABLED. // Use nicer Imagick implementation. // Untested! // @todo Test and implement it! $draw = new \ImagickDraw(); $draw->setFont($fontPath); $draw->setFontSize($fontSize); $draw->setStrokeAntialias(true); //try with and without $draw->setTextAntialias(true); //try with and without $draw->setFillColor('#fff'); $textOnly = new \Imagick(); $textOnly->newImage(1400, 400, "transparent"); //transparent canvas $textOnly->annotateImage($draw, 0, 0, 0, $this->text); //Create stroke $draw->setFillColor('#000'); //same as stroke color $draw->setStrokeColor('#000'); $draw->setStrokeWidth(8); $strokeImage = new \Imagick(); $strokeImage->newImage(1400, 400, "transparent"); $strokeImage->annotateImage($draw, 0, 0, 0, $this->text); //Composite text over stroke $strokeImage->compositeImage($textOnly, \Imagick::COMPOSITE_OVER, 0, 0, \Imagick::CHANNEL_ALPHA); $strokeImage->trimImage(0); //cut transparent border $watermarkImage = $imagine->load($strokeImage->getImageBlob()); //$strokeImage->resizeImage(300,0, \Imagick::FILTER_CATROM, 0.9, false); //resize to final size } return $watermarkImage; }
function AboveText() { $abovetextimage = new Imagick(); $draw = new ImagickDraw(); $pixel = new ImagickPixel('transparent'); $abovetextimage->newImage(600, 600, $pixel); $draw->setFillColor('black'); $draw->setFont('Bookman-DemiItalic'); $draw->setFontSize(20); $myfirstName = explode(" ", $this->{$userName}); $fname = $myfirstName[0]; $abovetextimage->annotateImage($draw, 5, 20, 0, "{$this}->{$name} has a secret crush on {$fname}"); $abovetextimage->setImageFormat('png'); $abovetextimage->writeImage("abovetext.png"); $this->{$aboveText} = imagecreatefrompng("abovetext.png"); }
/** * Draws font to given image at given position * * @param Image $image * @param integer $posx * @param integer $posy * @return void */ public function applyToImage(Image $image, $posx = 0, $posy = 0) { // build draw object $draw = new \ImagickDraw(); $draw->setStrokeAntialias(true); $draw->setTextAntialias(true); // set font file if ($this->hasApplicableFontFile()) { $draw->setFont($this->file); } else { throw new \Intervention\Image\Exception\RuntimeException("Font file must be provided to apply text to image."); } // parse text color $color = new Color($this->color); $draw->setFontSize($this->size); $draw->setFillColor($color->getPixel()); // align horizontal switch (strtolower($this->align)) { case 'center': $align = \Imagick::ALIGN_CENTER; break; case 'right': $align = \Imagick::ALIGN_RIGHT; break; default: $align = \Imagick::ALIGN_LEFT; break; } $draw->setTextAlignment($align); // align vertical if (strtolower($this->valign) != 'bottom') { // calculate box size $dimensions = $image->getCore()->queryFontMetrics($draw, $this->text); // corrections on y-position switch (strtolower($this->valign)) { case 'center': case 'middle': $posy = $posy + $dimensions['textHeight'] * 0.65 / 2; break; case 'top': $posy = $posy + $dimensions['textHeight'] * 0.65; break; } } // apply to image $image->getCore()->annotateImage($draw, $posx, $posy, $this->angle * -1, $this->text); }
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)"); $color['habr'] = new ImagickPixel("rgb(83, 121, 139)"); // Prepare canvas for drawing main graph $draw = new ImagickDraw(); $draw->setStrokeAntialias(true); $draw->setStrokeWidth(2); // Prepare values for drawing main graph define('PADDING', 10); // Draw bottom bg define('TOP_SPACER', 10); $draw = new ImagickDraw(); $draw->setFillColor($color['bg']); $draw->setStrokeColor($color['habr']); $draw->polyline(array(array('x' => 0, 'y' => 0), array('x' => self::WIDTH - 1, 'y' => 0), array('x' => self::WIDTH - 1, 'y' => self::HEIGHT - 1), array('x' => 0, 'y' => self::HEIGHT - 1), array('x' => 0, 'y' => 0))); $this->_canvas->drawImage($draw); $draw->destroy(); // Draw texts $draw = new ImagickDraw(); $draw->setTextAlignment(Imagick::ALIGN_CENTER); $draw->setTextAntialias(true); $draw->setFillColor($color['karma']); $draw->polyline(array(array('x' => 1, 'y' => 1), array('x' => self::WIDTH / 2, 'y' => 1), array('x' => self::WIDTH / 2, 'y' => self::HEIGHT - 2), array('x' => 1, 'y' => self::HEIGHT - 2), array('x' => 1, 'y' => 1))); $draw->setFillColor($color['bg']); $draw->setFontSize(11); $draw->setFont(realpath('stuff/consolab.ttf')); $draw->annotation(self::WIDTH / 4, 11, sprintf('%01.2f', $this->_karma)); $draw->setFillColor($color['force']); $draw->polyline(array(array('x' => self::WIDTH / 2 + 1, 'y' => 1), array('x' => self::WIDTH - 2, 'y' => 1), array('x' => self::WIDTH - 2, 'y' => self::HEIGHT - 2), array('x' => self::WIDTH / 2 + 1, 'y' => self::HEIGHT - 2), array('x' => self::WIDTH / 2 + 1, 'y' => 1))); $draw->setFillColor($color['bg']); $draw->annotation(self::WIDTH / 4 * 3, 11, sprintf('%01.2f', $this->_habraforce)); $this->_canvas->drawImage($draw); $draw->destroy(); return true; }
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; }
function TextOverlay() { $textimage = new Imagick(); $draw = new ImagickDraw(); $pixel = new ImagickPixel('transparent'); /* New image */ $textimage->newImage(600, 600, $pixel); /* Black text */ $draw->setFillColor('blue'); /* Font properties */ $draw->setFont('Bookman-DemiItalic'); $draw->setFontSize(20); /* Create text */ $textimage->annotateImage($draw, 5, 20, 0, "{$name} has a secret CRUSH on YOU!"); /* Give image a format */ $textimage->setImageFormat('png'); $textimage->writeImage("rounded3.png"); $top_image3 = imagecreatefrompng("rounded3.png"); }
/** * {@inheritdoc} */ public function box($string, $angle = 0) { $text = new \ImagickDraw(); $text->setFont($this->file); /** * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027 * * ensure font resolution is the same as GD's hard-coded 96 */ if (version_compare(phpversion("imagick"), "3.0.2", ">=")) { $text->setResolution(96, 96); $text->setFontSize($this->size); } else { $text->setFontSize((int) ($this->size * (96 / 72))); } $info = $this->imagick->queryFontMetrics($text, $string); $box = new Box($info['textWidth'], $info['textHeight']); return $box; }
function fl_text_render($_file, $id, $text, $fontname, $fontsize, $color = "#000000", $out_image_file_type = "png") { $font = locate_font($fontname); if ($font === false) { fllog('fl_text_render: font `' . $fontname . '` not found at `' . flvault . $fontname . '`'); return false; } $render = false; $out_image_file_type = strtolower($out_image_file_type); $cachefile = flcache . $id . '.' . $out_image_file_type; if ($_file !== false) { if (file1_is_older($cachefile, $_file)) { $render = true; } } else { $render = true; } if ($render === true) { try { $draw = new ImagickDraw(); $draw->setFont($font); $draw->setFontSize(intval($fontsize)); $draw->setGravity(Imagick::GRAVITY_CENTER); $draw->setFillColor($color); $canvas = new Imagick(); $m = $canvas->queryFontMetrics($draw, htmlspecialchars_decode($text)); $canvas->newImage($m['textWidth'], $m['textHeight'], "transparent", $out_image_file_type); $canvas->annotateImage($draw, 0, 0, 0, $text); $canvas->setImageFormat(strtoupper($out_image_file_type)); $canvas->writeImage($cachefile); fllog('Writing to: ' . $cachefile); $canvas->clear(); $canvas->destroy(); $draw->clear(); $draw->destroy(); } catch (Exception $e) { fllog('fl_text_render() Error: ', $e->getMessage()); return false; } } return $cachefile; }
protected function _generateImage($text, $filePath) { $image = new Imagick(); $draw = new ImagickDraw(); $draw->setFont('lib/SwiftOtter/OpenSans-Regular.ttf'); $draw->setFontSize('13'); $metrics = $image->queryFontMetrics($draw, $text, false); $width = 100; $padding = 10; if (isset($metrics['textWidth'])) { $width = $metrics['textWidth'] + $padding * 2; } $image->newImage($width, 17, new ImagickPixel('#f98b25')); $draw->setFillColor('#ffffff'); $image->annotateImage($draw, $padding / 2 + 3, $padding + 3, 0, $text); $draw->setFillColor('#a04300'); $image->borderImage('#a04300', 1, 1); $image->setFormat('gif'); $image->writeImage($filePath); return $image; }
/** * Generates and dies with an image containing the heading and the text of the error. * * @param string $headingText The heading of the error. * @param string $errorText The text of the error. */ public function generate($headingText, $errorText) { $draw = new ImagickDraw(); $draw->setFillColor('#777777'); $draw->setFontSize(15); $draw->setFont('fonts/exo2bold.ttf'); $headingMetrics = $this->canvas->queryFontMetrics($draw, $headingText); $draw->setFont('fonts/exo2regular.ttf'); $textMetrics = $this->canvas->queryFontMetrics($draw, $errorText); $this->canvas->newImage(max($textMetrics['textWidth'], $headingMetrics['textWidth']) + 6, $textMetrics['textHeight'] + $headingMetrics['textHeight'] + 6, new ImagickPixel('white')); $this->canvas->annotateImage($draw, 3, $headingMetrics['textHeight'] * 2, 0, $errorText); $draw->setFont('fonts/exo2bold.ttf'); $draw->setFillColor('#333333'); $draw->setGravity(Imagick::GRAVITY_NORTH); $this->canvas->annotateImage($draw, 3, 3, 0, $headingText); $this->canvas->setImageFormat('png'); header('Content-Type: image/' . $this->canvas->getImageFormat()); header("Cache-Control: max-age=60"); header("Expires: " . gmdate("D, d M Y H:i:s", time() + 60) . " GMT"); die($this->canvas); }
public function renderImage() { $imagick = new \Imagick(realpath("images/TestImage.jpg")); $draw = new \ImagickDraw(); $darkColor = new \ImagickPixel('brown'); $lightColor = new \ImagickPixel('LightCoral'); $draw->setStrokeColor($darkColor); $draw->setFillColor($lightColor); $draw->setStrokeWidth(2); $draw->setFontSize(36); $draw->setFont("../fonts/Arial.ttf"); $draw->annotation(50, 50, "Lorem Ipsum!"); $msg = "Danack"; $xpos = 0; $ypos = 0; list($lines, $lineHeight) = wordWrapAnnotation($imagick, $draw, $msg, 140); for ($i = 0; $i < count($lines); $i++) { $imagick->annotateImage($draw, $xpos, $ypos + $i * $lineHeight, 0, $lines[$i]); } header("Content-Type: image/jpg"); echo $imagick->getImageBlob(); }
function drawText(\Imagick $imagick, $shadow = false) { $draw = new \ImagickDraw(); if ($shadow == true) { $draw->setStrokeColor('black'); $draw->setStrokeWidth(8); $draw->setFillColor('black'); } else { $draw->setStrokeColor('none'); $draw->setStrokeWidth(1); $draw->setFillColor('lightblue'); } $draw->setFontSize(96); $text = "Imagick\nExample"; $draw->setFont("../fonts/CANDY.TTF"); $draw->setGravity(\Imagick::GRAVITY_SOUTHWEST); $imagick->annotateimage($draw, 40, 40, 0, $text); if ($shadow == true) { $imagick->blurImage(10, 5); } return $imagick; }
public function stage() { $this->init(); $this->background(); $color = $this->font; $color = new \ImagickPixel(sprintf('rgba(%d, %d, %d, %.2f)', $color['r'], $color['g'], $color['b'], (100 - $this->config['opacity']) / 100)); $text = new \ImagickDraw(); $text->setFillColor($color); $text->setFont($this->config['font']); $size = 1; $text->setFontSize($size); $height = $this->options['size'] / 100 * $this->config['prop']; $metrics = $this->ivatar->queryFontMetrics($text, $this->text, false); while ($metrics['boundingBox']['y2'] - $metrics['boundingBox']['y1'] <= $height) { $size++; $text->setFontSize($size); $metrics = $this->ivatar->queryFontMetrics($text, $this->text, false); } $x = ($this->options['size'] - $metrics['textWidth']) / 2 + $this->config['offset']['x'] / 100 * $this->options['size']; $y = ($this->options['size'] - ($metrics['boundingBox']['y2'] - $metrics['boundingBox']['y1'])) / 2 + ($metrics['boundingBox']['y2'] - $metrics['boundingBox']['y1']) + $this->config['offset']['y'] / 100 * $this->options['size']; $this->ivatar->annotateImage($text, $x, $y, null, $this->text); }
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 add_text($image, $text, $x = 0, $y = 0, $angle = 0, $style = array()) { $draw = new ImagickDraw(); if (isset($style['font'])) { $draw->setFont($style['font']); } if (isset($style['font_size'])) { $draw->setFontSize($style['font_size']); } if (isset($style['fill_color'])) { $draw->setFillColor($style['fill_color']); } if (isset($style['back_color'])) { $draw->setTextUnderColor($style['back_color']); } if ($image->getFormat() == 'gif') { foreach ($image as $frame) { $frame->annotateImage($draw, $x, $y, $angle, $text); } } else { $image->annotateImage($draw, $x, $y, $angle, $text); } #return $image; }
/** * 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 . '">'; }
/** * Renders the CAPTCHA image based on the code using ImageMagick library. * @param string $code the verification code * @return string image contents in PNG format. */ protected function renderImageByImagick($code) { $backColor = $this->transparent ? new \ImagickPixel('transparent') : new \ImagickPixel('#' . dechex($this->backColor)); $foreColor = new \ImagickPixel('#' . dechex($this->foreColor)); $image = new \Imagick(); $image->newImage($this->width, $this->height, $backColor); $draw = new \ImagickDraw(); $draw->setFont($this->fontFile); $draw->setFontSize(30); $fontMetrics = $image->queryFontMetrics($draw, $code); $length = strlen($code); $w = (int) $fontMetrics['textWidth'] - 8 + $this->offset * ($length - 1); $h = (int) $fontMetrics['textHeight'] - 8; $scale = min(($this->width - $this->padding * 2) / $w, ($this->height - $this->padding * 2) / $h); $x = 10; $y = round($this->height * 27 / 40); for ($i = 0; $i < $length; ++$i) { $draw = new \ImagickDraw(); $draw->setFont($this->fontFile); $draw->setFontSize((int) (rand(26, 32) * $scale * 0.8)); $draw->setFillColor($foreColor); $image->annotateImage($draw, $x, $y, rand(-10, 10), $code[$i]); $fontMetrics = $image->queryFontMetrics($draw, $code[$i]); $x += (int) $fontMetrics['textWidth'] + $this->offset; } $image->setImageFormat('png'); return $image->getImageBlob(); }
/** * 图像添加文字 * @param string $text 添加的文字 * @param string $font 字体路径 * @param integer $size 字号 * @param string $color 文字颜色 * @param integer $locate 文字写入位置 * @param integer $offset 文字相对当前位置的偏移量 * @param integer $angle 文字倾斜角度 */ public function text($text, $font, $size, $color = '#00000000', $locate = Image::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0) { //资源检测 if (empty($this->img)) { throw new Exception('没有可以被写入文字的图像资源'); } if (!is_file($font)) { throw new Exception("不存在的字体文件:{$font}"); } //获取颜色和透明度 if (is_array($color)) { $color = array_map('dechex', $color); foreach ($color as &$value) { $value = str_pad($value, 2, '0', STR_PAD_LEFT); } $color = '#' . implode('', $color); } elseif (!is_string($color) || 0 !== strpos($color, '#')) { throw new Exception('错误的颜色值'); } $col = substr($color, 0, 7); $alp = strlen($color) == 9 ? substr($color, -2) : 0; //获取文字信息 $draw = new ImagickDraw(); $draw->setFont(realpath($font)); $draw->setFontSize($size); $draw->setFillColor($col); $draw->setFillAlpha(1 - hexdec($alp) / 127); $draw->setTextAntialias(true); $draw->setStrokeAntialias(true); $metrics = $this->img->queryFontMetrics($draw, $text); /* 计算文字初始坐标和尺寸 */ $x = 0; $y = $metrics['ascender']; $w = $metrics['textWidth']; $h = $metrics['textHeight']; /* 设定文字位置 */ switch ($locate) { /* 右下角文字 */ case Image::IMAGE_WATER_SOUTHEAST: $x += $this->info['width'] - $w; $y += $this->info['height'] - $h; break; /* 左下角文字 */ /* 左下角文字 */ case Image::IMAGE_WATER_SOUTHWEST: $y += $this->info['height'] - $h; break; /* 左上角文字 */ /* 左上角文字 */ case Image::IMAGE_WATER_NORTHWEST: // 起始坐标即为左上角坐标,无需调整 break; /* 右上角文字 */ /* 右上角文字 */ case Image::IMAGE_WATER_NORTHEAST: $x += $this->info['width'] - $w; break; /* 居中文字 */ /* 居中文字 */ case Image::IMAGE_WATER_CENTER: $x += ($this->info['width'] - $w) / 2; $y += ($this->info['height'] - $h) / 2; break; /* 下居中文字 */ /* 下居中文字 */ case Image::IMAGE_WATER_SOUTH: $x += ($this->info['width'] - $w) / 2; $y += $this->info['height'] - $h; break; /* 右居中文字 */ /* 右居中文字 */ case Image::IMAGE_WATER_EAST: $x += $this->info['width'] - $w; $y += ($this->info['height'] - $h) / 2; break; /* 上居中文字 */ /* 上居中文字 */ case Image::IMAGE_WATER_NORTH: $x += ($this->info['width'] - $w) / 2; break; /* 左居中文字 */ /* 左居中文字 */ case Image::IMAGE_WATER_WEST: $y += ($this->info['height'] - $h) / 2; break; default: /* 自定义文字坐标 */ if (is_array($locate)) { list($posx, $posy) = $locate; $x += $posx; $y += $posy; } else { throw new Exception('不支持的文字位置类型'); } } /* 设置偏移量 */ if (is_array($offset)) { $offset = array_map('intval', $offset); list($ox, $oy) = $offset; } else { $offset = intval($offset); $ox = $oy = $offset; } /* 写入文字 */ if ('gif' == $this->info['type']) { $img = $this->img->coalesceImages(); $this->img->destroy(); //销毁原图 do { $img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text); } while ($img->nextImage()); //压缩图片 $this->img = $img->deconstructImages(); $img->destroy(); //销毁零时图片 } else { $this->img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text); } $draw->destroy(); }