Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. function rotate_square($arr) {
  4.   $fontfile = '/usr/share/fonts/TTF/DejaVuSansMono.ttf';
  5.   $fontsize = 12.0;
  6.  
  7.   $rowstrs = $rowheights = array();
  8.   $maxwidth = $totalheight = 0;
  9.   foreach ($arr as $row) {
  10.     $rowstrs[] = $rowstr = implode(' ', $row);
  11.     if (($bbox = imagettfbbox($fontsize, 0.0, $fontfile, $rowstr)) !== FALSE) {
  12.       $width = abs($bbox[4] - $bbox[0]);
  13.       if ($width > $maxwidth) $maxwidth = $width;
  14.       $height = abs($bbox[5] - $bbox[1]);
  15.       $rowheights[] = $height;
  16.       $totalheight += $height;
  17.     } else {
  18.       return FALSE;
  19.     }
  20.   }
  21.   $sidelen = max($maxwidth, $totalheight);
  22.   if (($im = imagecreatetruecolor($sidelen, $sidelen)) === FALSE)
  23.      return $im;
  24.   $white = imagecolorallocate($im, 255, 255, 255);
  25.   $black = imagecolorallocate($im, 0, 0, 0);
  26.   imagefilledrectangle($im, 0, 0, $sidelen - 1, $sidelen - 1, $white);
  27.   $i = $totalheight = 0;
  28.   foreach ($arr as $row) {
  29.     imagettftext($im, $fontsize, 0.0, 0, $totalheight, $black, $fontfile, $rowstr[$i]);
  30.     $totalheight += $rowheights[$i];
  31.     $i++;
  32.   }
  33.   $imr = imagerotate($im, 270.0, $white);
  34.   imagedestroy($im);
  35.   return $imr;
  36. }
  37.  
  38. $m = array(array(1, 0, 0),
  39.            array(0, 1, 0),
  40.            array(0, 0, 1));
  41. $result = rotate_square($m);
  42. if ($result !== FALSE) {
  43.   header('Content-Type: image/png');
  44.   imagepng($result);
  45.   imagedestroy($result);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement