Advertisement
imoda

class.textimage.php

May 3rd, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. <?php
  2.  
  3. require_once("../includes/config.php");
  4. require_once("../includes/class.textimage.php");
  5.  
  6. $image = new TextImage();
  7.  
  8. $image->color('000000');
  9. $image->text(!empty($_GET['text']) ? $_GET['text'] : 'Error:-missing-?text-arguement');
  10.  
  11. header("Content-Type: image/png");
  12.  
  13. /*header("Cache-Control: private, max-age=10800, pre-check=10800");
  14. header("Pragma: private");
  15. header("Expires: " . date(DATE_RFC822, strtotime("2 day")));
  16.  
  17. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  18.  
  19. header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304);
  20. exit;
  21. }*/
  22.  
  23. $image->output();
  24.  
  25. ?>
  26.  
  27. <?php
  28.  
  29. class TextImage {
  30.  
  31. function __construct() {
  32.  
  33. $this->rotationDegree = 0;
  34. $this->font = '../fonts/arial.ttf';
  35. $this->size = (!empty($_GET['size']) ? $_GET['size'] : 150);
  36. $this->fontSize = 24/150*$this->size;
  37. $this->width = $this->size;
  38. $this->height = $this->size;
  39. }
  40.  
  41. public function color($hex) {
  42.  
  43. if ($hex[0] == '#') {
  44.  
  45. $hex = substr($hex, 1);
  46. }
  47.  
  48. if (strlen($hex) == 6) {
  49.  
  50. list($r, $g, $b) = array($hex[0].$hex[1],
  51. $hex[2].$hex[3],
  52. $hex[4].$hex[5]);
  53.  
  54. }
  55. else if (strlen($hex) == 3) {
  56.  
  57. list($r, $g, $b) = array($hex[0].$hex[0], $hex[1].$hex[1], $hex[2].$hex[2]);
  58. }
  59. else {
  60.  
  61. return false;
  62. }
  63.  
  64. $this->fontColor = array(
  65. 'r' => hexdec($r),
  66. 'g' => hexdec($g),
  67. 'b' => hexdec($b)
  68. );
  69. }
  70.  
  71. public function text($text) {
  72.  
  73. $this->text = $text;
  74. }
  75.  
  76. public function output() {
  77.  
  78. $this->create();
  79.  
  80. $this->fitText(urldecode($this->text));
  81.  
  82. $this->display();
  83.  
  84. $this->trash();
  85. }
  86.  
  87. private function create() {
  88.  
  89. $this->newImage = imagecreatetruecolor($this->width, $this->height);
  90. imagealphablending($this->newImage, false);
  91. imagesavealpha($this->newImage, true);
  92. $transparent = imagecolorallocatealpha($this->newImage, 255, 255, 255, 127);
  93. imagefilledrectangle($this->newImage, 0, 0, $this->width, $this->height, $transparent);
  94. }
  95.  
  96. private function fitText($text) {
  97.  
  98. imagealphablending($this->newImage, true);
  99. $num = count(explode('-', $this->text));
  100.  
  101. $i = 1;
  102. foreach(explode('-', $text) as $word) {
  103.  
  104. //$word = ucwords(strtolower($word));
  105.  
  106. $xbox = imagettfbbox($this->fontSize, $this->rotationDegree, $this->font, $word);
  107. $x = ($this->width / 2) - (((($xbox[2] - $xbox[0]) + ($xbox[4] - $xbox[6])) / 2) / 2);
  108.  
  109. $ybox = imagettfbbox($this->fontSize, $this->rotationDegree, $this->font, substr($word, 0, 1));
  110. $y = ($this->height / 2) - (((($ybox[3] - $ybox[5]) + ($ybox[1] - $ybox[7])) / 2) / 2) + ($ybox[3] - $ybox[5]);
  111.  
  112. $color = imagecolorallocate($this->newImage, $this->fontColor['r'], $this->fontColor['g'], $this->fontColor['b']);
  113.  
  114. $mod = $this->fontSize * 1.5;
  115. $y = $y + (($i - $num) * $mod) + ($num - 1) * $mod / 2;
  116.  
  117. imagettftext($this->newImage, $this->fontSize, $this->rotationDegree, $x, $y, $color, $this->font, $word);
  118.  
  119. $i++;
  120. }
  121. }
  122.  
  123. private function display() {
  124.  
  125. imagepng($this->newImage);
  126. }
  127.  
  128. private function trash() {
  129.  
  130. imagedestroy($this->newImage);
  131. }
  132. }
  133.  
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement