TextImageWriter.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /** @package verysimple::Util */
  3. /**
  4. * Utility to stream an image containing text to the browser
  5. *
  6. * @package verysimple::Util
  7. * @author Jason Hinkle
  8. * @copyright 1997-2011 VerySimple, Inc.
  9. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  10. * @version 1.0
  11. */
  12. class TextImageWriter
  13. {
  14. /**
  15. *
  16. * @param $message
  17. */
  18. /**
  19. * Output a png image to the browser, including headers
  20. * @param string $message
  21. * @param int $width
  22. * @param int $height
  23. * @param array $backgroundColor RGB values from 0-255. example: array(0,0,0);
  24. * @param array $fontColor RGB values from 0-255. example: array(0,0,0);
  25. * @param int $fontId (number between 1 and 5);
  26. */
  27. static function Write($message, $width = 250, $height = 150, $backgroundColor = null, $fontColor = null, $fontId = 1)
  28. {
  29. if ($backgroundColor == null) $backgroundColor = array(255,255,255);
  30. if ($backgroundColor == null) $backgroundColor = array(0,0,0);
  31. $im = self::GetErrorImage($message, $width, $height, $backgroundColor, $fontColor, $fontId);
  32. header('Content-type: image/png');
  33. imagepng($im);
  34. imagedestroy($im);
  35. }
  36. /**
  37. * Given text, returns an image reference with the text included in the image
  38. * @param string $message
  39. * @param int $width
  40. * @param int $height
  41. * @param array $backgroundColor RGB values from 0-255. example: array(0,0,0);
  42. * @param array $fontColor RGB values from 0-255. example: array(0,0,0);
  43. * @param int $fontId (number between 1 and 5);
  44. * @return int image reference
  45. */
  46. static function GetErrorImage($message, $width = 250, $height = 150, $backgroundColor = null, $fontColor = null, $fontId = 1)
  47. {
  48. if ($backgroundColor == null) $backgroundColor = array(255,255,255);
  49. if ($backgroundColor == null) $backgroundColor = array(0,0,0);
  50. $msg = str_replace("\n","",$message);
  51. $im = imagecreate($width, $height);
  52. $bgColor = imagecolorallocate($im, $backgroundColor[0],$backgroundColor[1],$backgroundColor[2]);
  53. $fontColor = imagecolorallocate($im, $fontColor[0],$fontColor[1],$fontColor[2]);
  54. $lines = explode( "\r", wordwrap($msg,($width/5),"\r"));
  55. $count = 0;
  56. foreach ($lines as $line)
  57. {
  58. imagestring($im, $fontId, 2, 2 + ($count * 12) , $line, $fontColor);
  59. $count++;
  60. }
  61. return $im;
  62. }
  63. }
  64. ?>