Advertisement
croc

Image Generator Script

Jan 15th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. <?php
  2.  
  3. /* Implement word wrapping... Ughhh... why is this NOT done for me!!!
  4.     OK... I know the algorithm sucks at efficiency, but it's for short messages, okay?
  5.  
  6.     Make sure to set the font on the ImagickDraw Object first!
  7.     @param image the Imagick Image Object
  8.     @param draw the ImagickDraw Object
  9.     @param text the text you want to wrap
  10.     @param maxWidth the maximum width in pixels for your wrapped "virtual" text box
  11.     @return an array of lines and line heights
  12. */
  13. function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
  14. {
  15.     $words = explode(" ", $text);
  16.     $lines = array();
  17.     $i = 0;
  18.     $lineHeight = 0;
  19.     while($i < count($words) )
  20.     {
  21.         $currentLine = $words[$i];
  22.         if($i+1 >= count($words))
  23.         {
  24.             $lines[] = $currentLine;
  25.             break;
  26.         }
  27.         //Check to see if we can add another word to this line
  28.         $metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
  29.         while($metrics['textWidth'] <= $maxWidth)
  30.         {
  31.             //If so, do it and keep doing it!
  32.             $currentLine .= ' ' . $words[++$i];
  33.             if($i+1 >= count($words))
  34.                 break;
  35.             $metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
  36.         }
  37.         //We can't add the next word to this line, so loop to the next line
  38.         $lines[] = $currentLine;
  39.         $i++;
  40.         //Finally, update line height
  41.         if($metrics['textHeight'] > $lineHeight)
  42.             $lineHeight = $metrics['textHeight'];
  43.     }
  44.     return array($lines, $lineHeight);
  45. }
  46.  
  47. /* Create some objects */
  48. $image = new Imagick();
  49. $draw = new ImagickDraw();
  50. $pixel = new ImagickPixel( '#2F6EB9' );
  51.  
  52. $filename = "products.txt";
  53. $dir = "product_images/";
  54.  
  55. $lines = file($filename, FILE_IGNORE_NEW_LINES);
  56.  
  57. foreach ($lines as $line) {
  58.    /* New image */
  59.     $image->newImage(300, 300, $pixel);
  60.  
  61.     /* Black text */
  62.     $draw->setFillColor('white');
  63.  
  64.     /* Font properties */
  65.     $draw->setFont('Arial');
  66.     $draw->setFontSize( 30 );
  67.  
  68.     $draw->setGravity (Imagick::GRAVITY_CENTER);
  69.  
  70.     list($titles, $lineHeight) = wordWrapAnnotation($image, $draw, $line, 280);
  71.  
  72.     for($i = 0; $i < count($titles); $i++) {
  73.         $image->annotateImage($draw, 0, 0 + $i*$lineHeight, 0, $titles[$i]);
  74.     }
  75.  
  76.     /* Create text */
  77.     //$image->annotateImage($draw, 0, 0, 0, $line);
  78.  
  79.     /* Give image a format */
  80.     $image->setImageFormat('png');
  81.  
  82.     $image->readImageBlob($image);
  83.  
  84.     file_put_contents( $dir . preg_replace('/\//', ' ', $line, 2) . ".png", $image);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement