Exemplo n.º 1
0
 function createQrPng($firstName, $lastName, $id)
 {
     $filename = ABSPATH . 'qr/' . $id . '-' . $firstName . '-' . $lastName . '.png';
     $size = 6;
     $margin = 1;
     QRcode::png($id, $filename, "L", $size, $margin);
     //create the blank image
     $im = imagecreatefrompng($filename);
     $width = imagesx($im);
     $height = imagesy($im);
     //setting up variables for putting text on the image
     $fontfile = ABSPATH . "my-includes/fonts/Avenir Next Condensed.ttc";
     $string = $firstName . " " . $lastName;
     $fontsize = 16;
     $bbox = imagettfbbox($fontsize, 0, $fontfile, $string);
     $text_width = $bbox[4] - $bbox[0];
     $maxchars = 40;
     $margin = 20;
     //10 pixels each side
     //adjust fontsize for size of text until it fits
     while ($text_width + $margin > $width) {
         $fontsize -= 1;
         $bbox = imagettfbbox($fontsize, 0, $fontfile, $string);
         $text_width = $bbox[4] - $bbox[0];
     }
     //Set up the canvas with the size and black fill
     $newwidth = 144;
     $newheight = 216;
     $bordersize = 1;
     $wwidth = $newwidth - 2 * $bordersize;
     $wheight = $newheight - 2 * $bordersize;
     $white_space = imagecreatetruecolor($wwidth, $wheight);
     $output = imagecreatetruecolor($newwidth, $newheight);
     $white = imagecolorallocate($output, 255, 255, 255);
     $black = imagecolorallocate($output, 0, 0, 0);
     imagefill($output, 0, 0, $black);
     imagefill($white_space, 0, 0, $white);
     //Put border by filling with white box except for border
     imagecopy($output, $white_space, $bordersize, $bordersize, 0, 0, $wwidth, $wheight);
     //overlay png image of qr code
     imagecopy($output, $im, ($newwidth - $width) / 2, 5, 0, 0, $width, $height);
     //output image with qr code
     imagepng($output, $filename);
     //add text to image
     $image = new \NMC\ImageWithText\Image($filename);
     $text = new \NMC\ImageWithText\Text($string, 1, $maxchars);
     $text->font = $fontfile;
     $text->align = 'left';
     $text->size = $fontsize;
     $text->startY = (int) ($newheight * (215 / 300));
     //below qr code. Adjusted for dif image sizes
     $text->startX = (int) (($newwidth - $text_width) / 2);
     //center
     $image->addText($text);
     $image->render($filename);
     //clean up
     imagedestroy($output);
     imagedestroy($im);
 }
Exemplo n.º 2
0
<?php

require '../vendor/autoload.php';
// Create image
$image = new \NMC\ImageWithText\Image(dirname(__FILE__) . '/source.jpg');
// Add styled text to image
$text1 = new \NMC\ImageWithText\Text('Thanks for using our image text PHP library!', 3, 25);
$text1->align = 'left';
$text1->color = 'FFFFFF';
$text1->font = dirname(__FILE__) . '/Ubuntu-Medium.ttf';
$text1->lineHeight = 36;
$text1->size = 24;
$text1->startX = 40;
$text1->startY = 40;
$image->addText($text1);
// Add another styled text to image
$text2 = new \NMC\ImageWithText\Text('No, really, thanks!', 1, 30);
$text2->align = 'left';
$text2->color = '000000';
$text2->font = dirname(__FILE__) . '/Ubuntu-Medium.ttf';
$text2->lineHeight = 20;
$text2->size = 14;
$text2->startX = 40;
$text2->startY = 140;
$image->addText($text2);
// Render image
$image->render(dirname(__FILE__) . '/destination.jpg');