/** * 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"); }
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); }
/** * 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; }
/** * Translates the Drawer object to a ImagickDraw * @param Drawer $drawer * @return \ImagickDraw */ public function getImagickDraw(Drawer $drawer) { $imdraw = new \ImagickDraw(); $imdraw->setFont($drawer->getFont()); $imdraw->setFillColor($drawer->getFontColor()); $imdraw->setFontSize($drawer->getFontSize()); $imdraw->setTextAlignment($drawer->getTextAlign()); return $imdraw; }
/** * Show image with error text. * * @param string $text * @param int $width * @param int $height */ public static function showError($text, $width, $height) { $canvas = new Imagick(); $canvas->newImage($width, $height, new ImagickPixel("white")); $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel("white")); $draw->setStrokeColor(new ImagickPixel("black")); $draw->polyline(array(array('x' => 0, 'y' => 0), array('x' => $width - 1, 'y' => 0), array('x' => $width - 1, 'y' => $height - 1), array('x' => 0, 'y' => $height - 1), array('x' => 0, 'y' => 0))); $canvas->drawImage($draw); $draw->destroy(); $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel("black")); $draw->setFontSize(12); $draw->setTextAlignment(Imagick::ALIGN_CENTER); $draw->setTextAntialias(true); $draw->setFont(realpath('stuff/consolab.ttf')); $draw->annotation($width / 2, $height / 2, $text); $canvas->drawImage($draw); $canvas->setImageFormat('png'); header("Content-Type: image/png"); echo $canvas; $draw->destroy(); $canvas->clear(); $canvas->destroy(); exit; }
private static function __getImage($img, $type) { $client = new Client(Config::get('services.dropbox.token'), Config::get('services.dropbox.appName')); $fileSystem = new Filesystem(new DropboxAdapter($client, '/images/')); $biggerW = $biggerH = false; if ($img->width > $img->height) { $biggerW = true; } else { $biggerH = true; } $image = $w = $h = null; $public_path = public_path($img->path); //code minh //get image from dropbox if ($img->store == 'dropbox') { try { $file = $fileSystem->read($img->path); $public_path = $file; } catch (Exception $e) { return false; } } //end code if (in_array($type, ['with-logo', 'large-thumb'])) { if ($biggerW) { $w = 450; } else { $h = 450; } } else { if (in_array($type, ['thumb', 'small-thumb'])) { if ($type == 'thumb') { if ($biggerW) { $w = 150; } else { $h = 150; } } else { if ($biggerW) { $w = 100; } else { $h = 100; } } } else { if (in_array($type, ['crop', 'newcrop'])) { $h = 300; $w = 300; if ($type == 'newcrop') { $w = 600; } } } } try { if (in_array($type, ['crop', 'newcrop'])) { $image = Image::make($public_path)->fit($w, $h, function ($constraint) { $constraint->aspectRatio(); }); } else { $image = Image::make($public_path)->resize($w, $h, function ($constraint) { $constraint->aspectRatio(); }); } } catch (Exception $e) { return false; } if ($type == 'with-logo') { if (Cache::has('mask')) { $mask = Cache::get('mask'); } else { $mask = Configure::where('ckey', 'mask')->pluck('cvalue'); if (empty($mask)) { $mask = 'Visual Impact'; } Cache::forever('mask', $mask); } $size = 50; $w = $image->width(); $h = $image->height(); $x = round($w / 2); $y = round($h / 2); $img = Image::canvas($w, $h); $string = wordwrap($mask, 15, '|'); $strings = explode('|', $string); $line = 2; $i = round($y - count($strings) / 2 * ($size + $line)); $from = $i - 20; foreach ($strings as $string) { $draw = new \ImagickDraw(); $draw->setStrokeAntialias(true); $draw->setTextAntialias(true); $draw->setFont(public_path('assets' . DS . 'fonts' . DS . 'times.ttf')); $draw->setFontSize($size); $draw->setFillColor('rgb(0, 0, 0)'); $draw->setFillOpacity(0.2); $draw->setTextAlignment(\Imagick::ALIGN_CENTER); $draw->setStrokeColor('#fff'); $draw->setStrokeOpacity(0.2); $draw->setStrokeWidth(1); $dimensions = $img->getCore()->queryFontMetrics($draw, $string); $posy = $i + $dimensions['textHeight'] * 0.65 / 2; $img->getCore()->annotateImage($draw, $x, $posy, 0, $string); $i += $size + $line; } return $image->insert($img, 'center', $x, $y)->encode('jpg'); } if ($image) { return $image->encode('jpg'); } return false; }
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 setTextAlignment($strokeColor, $fillColor, $backgroundColor) { $draw = new \ImagickDraw(); $draw->setStrokeColor($strokeColor); $draw->setFillColor($fillColor); $draw->setStrokeWidth(1); $draw->setFontSize(36); $draw->setTextAlignment(\Imagick::ALIGN_LEFT); $draw->annotation(250, 75, "Lorem Ipsum!"); $draw->setTextAlignment(\Imagick::ALIGN_CENTER); $draw->annotation(250, 150, "Lorem Ipsum!"); $draw->setTextAlignment(\Imagick::ALIGN_RIGHT); $draw->annotation(250, 225, "Lorem Ipsum!"); $draw->line(250, 0, 250, 500); $imagick = new \Imagick(); $imagick->newImage(500, 500, $backgroundColor); $imagick->setImageFormat("png"); $imagick->drawImage($draw); header("Content-Type: image/png"); echo $imagick->getImageBlob(); }
var_dump($draw->getGravity() === Imagick::GRAVITY_SOUTHEAST); // stroke $draw->setStrokeAntialias(false); var_dump($draw->getStrokeAntialias()); $draw->setStrokeColor(new ImagickPixel('#F02B88')); var_dump($draw->getStrokeColor()->getColor()); $draw->setStrokeDashArray(array(1, 2, 3)); var_dump($draw->getStrokeDashArray()); $draw->setStrokeDashOffset(-1); var_dump($draw->getStrokeDashOffset()); $draw->setStrokeLineCap(Imagick::LINECAP_SQUARE); var_dump($draw->getStrokeLineCap() === Imagick::LINECAP_SQUARE); $draw->setStrokeLineJoin(Imagick::LINEJOIN_BEVEL); var_dump($draw->getStrokeLineJoin() === Imagick::LINEJOIN_BEVEL); $draw->setStrokeMiterLimit(3); var_dump($draw->getStrokeMiterLimit()); $draw->setStrokeOpacity(0.9); printf("%.2f\n", $draw->getStrokeOpacity()); $draw->setStrokeWidth(1.2); printf("%.2f\n", $draw->getStrokeWidth()); // text $draw->setTextAlignment(Imagick::ALIGN_CENTER); var_dump($draw->getTextAlignment() === Imagick::ALIGN_CENTER); $draw->setTextAntialias(false); var_dump($draw->getTextAntialias()); $draw->setTextDecoration(Imagick::DECORATION_LINETROUGH); var_dump($draw->getTextDecoration() === Imagick::DECORATION_LINETROUGH); $draw->setTextEncoding('UTF-8'); var_dump($draw->getTextEncoding()); $draw->setTextUnderColor('cyan'); var_dump($draw->getTextUnderColor()->getColor());
$draw = new ImagickDraw(); $draw->setFont('Nimbus-Sans-Bold'); if ($reqwidth <= 300) { $draw->setFontSize(9); $radius = 5; } else { if ($reqwidth < 800) { $draw->setFontSize(12); $radius = 7; $offset = 1; } else { $draw->setFontSize(14); $radius = 10; $offset = 2; } } foreach ($comments as $comment) { $draw->setFillColor('black'); $x = $comment['x'] * $factor; $y = $comment['y'] * $factor; $draw->circle($x + $radius, $y + $radius, $x + $radius * 2, $y + $radius * 2); $draw->setFillColor('white'); $draw->setTextAlignment(2); $draw->annotation($x + $radius, $y + $radius + $radius / 2 + 1, $comment['nr']); } $image->drawImage($draw); // Output header('Content-Type: image/png'); echo $image; break; }
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->setTextUnderColor($color['bg']); $draw->setTextAlignment(Imagick::ALIGN_CENTER); $draw->setTextAntialias(true); $draw->setFillColor($color['text']); $draw->setFont(realpath('stuff/arialbd.ttf')); $code = $this->_user; $draw->setFontSize(strlen($code) > 8 ? 10 : 11); if (strlen($code) > 10) { $code = substr($code, 0, 9) . '...'; } $draw->annotation(self::WIDTH / 2, self::HEIGHT - 9, $code); $draw->setFont(realpath('stuff/consola.ttf')); $draw->setFontSize(11); $draw->setFillColor($color['neutral']); $draw->annotation(self::WIDTH / 2, 15, "хаброметр"); $text = sprintf('%01.2f', $this->_extremums['karma_min']) . ' / ' . sprintf('%01.2f', $this->_extremums['karma_max']); $draw->setFontSize(9); $draw->setFillColor($color['karma']); $draw->annotation(self::WIDTH / 2, 55, $text); $text = sprintf('%01.2f', $this->_extremums['habraforce_min']) . ' / ' . sprintf('%01.2f', $this->_extremums['habraforce_max']); $draw->setFontSize(9); $draw->setFillColor($color['force']); $draw->annotation(self::WIDTH / 2, 95, $text); $draw->setTextUnderColor($color['karma']); $draw->setFillColor($color['bg']); $draw->setFontSize(14); $draw->setFont(realpath('stuff/consolab.ttf')); $draw->annotation(self::WIDTH / 2, 35, sprintf('%01.2f', $this->_karma)); $draw->setTextUnderColor($color['force']); $draw->setFillColor($color['bg']); $draw->annotation(self::WIDTH / 2, 75, sprintf('%01.2f', $this->_habraforce)); $this->_canvas->drawImage($draw); $draw->destroy(); return true; }
function text2image($text, $width, $margin, $size, $font, $align, $stroke, $bg, $color, $scolor, $out) { $text = html_entity_decode($text); $lines = array(); //break up the text on the line $imageForMetric = new Imagick(); $drawForMetric = new ImagickDraw(); $drawForMetric->setFont($font); $drawForMetric->setFontSize($size); $explicitLines = preg_split('/\\n|\\r\\n?/', $text); $key = 0; foreach ($explicitLines as $line) { $words = explode(" ", $line); $line = $words[0]; for ($i = 1; $i < count($words); $i++) { if ($imageForMetric->queryFontMetrics($drawForMetric, $line . " " . $words[$i])['textWidth'] >= $width - $margin * 2) { $key++; $lines[$key]['text'] = $line; $line = $words[$i]; } else { $line .= " " . $words[$i]; } } $key++; $lines[$key]['text'] = $line; $lines[$key]['type'] = 'last'; } $imageForMetric->destroy(); $drawForMetric->destroy(); //line height $lineHeightPx = $size * 1.5; //margin top and bottom (can leave $pad=$margin;) if ($size < 16) { $pad = $size / 2; } elseif ($size < 26) { $pad = $size / 3; } elseif ($size < 36) { $pad = $size / 4; } else { $pad = 0; } //height image $textHeight = count($lines) * $lineHeightPx + $pad * 2; $image = new Imagick(); $draw = new ImagickDraw(); $pixel = new ImagickPixel('#' . $bg); $image->newImage($width, $textHeight + $lineHeightPx / 2, $pixel); $draw->setFillColor('#' . $color); $draw->setFont($font); $draw->setFontSize($size); //for setTextAlignment if ($align == 'center') { $alignNumber = 2; } elseif ($align == 'right') { $alignNumber = 3; } else { $alignNumber = 1; } $draw->setTextAlignment($alignNumber); $draw->setTextEncoding('UTF-8'); //x start position if ($align == 'center') { $xpos = $width / 2; } elseif ($align == 'right') { $xpos = $width - $margin; } else { $xpos = $margin; } //text stroke if ($stroke) { $drawstroke = new ImagickDraw(); $drawstroke->setFillColor('#' . $scolor); $drawstroke->setFont($font); $drawstroke->setFontSize($size); $drawstroke->setTextAlignment($alignNumber); $drawstroke->setTextEncoding('UTF-8'); } //y start position $ypos = $lineHeightPx + $pad; $i = 0; foreach ($lines as $value => $line) { if ($align == 'justify' && (!isset($lines[$value]['type']) || $lines[$value]['type'] != 'last')) { //justify for all lines (except last) $line = trim($lines[$value]['text']); $sp = 0; $j = 0; $line = preg_replace("/ +/", " ", $line); $spaces = substr_count($line, ' '); $word = explode(' ', $line); //word metrics $metrics = 0; foreach ($word as $w) { $metrics = $metrics + $image->queryFontMetrics($draw, $w)['textWidth']; } $spacewidth = ($width - $margin * 2 - $metrics) / $spaces; foreach ($word as $w) { if ($w !== '') { $j++; if ($j > 1) { $sp = $sp + $spacewidth; } if ($stroke) { //draw stroke $ys = 0; $xs = $stroke; $d = (5 - $stroke * 4) / 4; while ($xs >= $ys) { $image->annotateImage($drawstroke, $xpos + $sp + $stroke, $ypos + $i * $lineHeightPx + $ys, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp + $stroke, $ypos + $i * $lineHeightPx - $ys, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp - $stroke, $ypos + $i * $lineHeightPx + $ys, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp - $stroke, $ypos + $i * $lineHeightPx - $ys, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp + $ys, $ypos + $i * $lineHeightPx + $stroke, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp + $ys, $ypos + $i * $lineHeightPx - $stroke, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp - $ys, $ypos + $i * $lineHeightPx + $stroke, 0, $w); $image->annotateImage($drawstroke, $xpos + $sp - $ys, $ypos + $i * $lineHeightPx - $stroke, 0, $w); if ($d < 0) { $d += 2 * $ys + 1; } else { $d += 2 * ($ys - $xs) + 1; $xs--; } $ys++; } } //draw word $image->annotateImage($draw, $xpos + $sp, $ypos + $i * $lineHeightPx, 0, $w); //space width $sp = $sp + $image->queryFontMetrics($draw, $w)['textWidth']; } } } else { if ($stroke) { //draw stroke $ys = 0; $xs = $stroke; $d = (5 - $stroke * 4) / 4; while ($xs >= $ys) { $image->annotateImage($drawstroke, $xpos + $stroke, $ypos + $i * $lineHeightPx + $ys, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos + $stroke, $ypos + $i * $lineHeightPx - $ys, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos - $stroke, $ypos + $i * $lineHeightPx + $ys, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos - $stroke, $ypos + $i * $lineHeightPx - $ys, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos + $ys, $ypos + $i * $lineHeightPx + $stroke, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos + $ys, $ypos + $i * $lineHeightPx - $stroke, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos - $ys, $ypos + $i * $lineHeightPx + $stroke, 0, $lines[$value]['text']); $image->annotateImage($drawstroke, $xpos - $ys, $ypos + $i * $lineHeightPx - $stroke, 0, $lines[$value]['text']); if ($d < 0) { $d += 2 * $ys + 1; } else { $d += 2 * ($ys - $xs) + 1; $xs--; } $ys++; } } //draw line $image->annotateImage($draw, $xpos, $ypos + $i * $lineHeightPx, 0, $lines[$value]['text']); } $i++; } //save image to png $image->setCompressionQuality(100); $image->setCompression(Imagick::COMPRESSION_NO); $image->setImageFormat('png'); $image->writeImage('png24:' . $out); $image->destroy(); }