Posted By

alexdahlem on 06/19/11


Tagged

image png images alpha dynamic manipulation generation barcode qr gdLib QR-Code


Versions (?)

Creating dynamic images whith QR Codes


 / Published in: PHP
 

  1. <?php
  2. /*
  3.  * by Alex Dahlem
  4.  * http://twitter.com/alexdahlem
  5.  *
  6.  * Dynamically creates a nice picture with a QR-Code on it
  7.  *
  8.  * Use this png as background: http://twitpic.com/5ds1iz
  9.  * Copy the background.png in the same folder as your script
  10.  *
  11.  * You can use this code in any project you want. Mention me if you like it :)
  12.  *
  13.  * Love to qrserver.com for their API! Take a look at their services!
  14.  *
  15.  *** Death to false markup! *** Cheers!
  16.  */
  17.  
  18. // Tell the browser this script is an image
  19. header("Content-Type: image/png");
  20.  
  21. // Our imagecontainer
  22. $imagecontainer = imagecreatetruecolor(500, 550);
  23. // We want to use transparency, so let's tell the image
  24. imagesavealpha ($imagecontainer, true);
  25. // Now we fill the imagecontainer with a transparent color
  26. $alphacolor = imagecolorallocatealpha($imagecontainer, 0,0,0,127);
  27. imagefill($imagecontainer,0,0,$alphacolor);
  28.  
  29.  
  30. // Our background graphic
  31. $background = imagecreatefrompng('background.png');
  32. // Copy the background into the container
  33. imagecopyresampled($imagecontainer, $background, 0, 0, 0, 0, 500, 550, 500, 550);
  34.  
  35.  
  36. // Our QR-Code
  37. $qrimage = imagecreatefrompng('http://api.qrserver.com/v1/create-qr-code/?size=265x265&data=http://twitter.com/alexdahlem&bgcolor=808080');
  38. // We need a photoshop-style layer effect
  39. imagelayereffect($imagecontainer, IMG_EFFECT_OVERLAY);
  40. for ($i = 0; $i < 3; $i++) {
  41. // Copy the QR image three time for besser contrast
  42. imagecopyresampled($imagecontainer, $qrimage, 115, 150, 0, 0, 265, 265, 265, 265);
  43. }
  44.  
  45. // Finally render the container
  46. imagepng($imagecontainer);
  47. ?>

Report this snippet  

You need to login to post a comment.