Pomoc - Szukaj - Uytkownicy - Kalendarz
Pena wersja: rodkowanie tekstu, automatyczna zmiana rozmiaru PHP GD
Forum PHP.pl > Forum > PHP
tomeknh
WITAM!

Mam taki kod:

  1. <?php
  2.  
  3. header("Content-type: image/png");
  4.  
  5. define('GDBB_TOP', 5);
  6. define('GDBB_LEFT', 0);
  7. define('GDBB_BOTTOM', 1);
  8. define('GDBB_RIGHT', 2);
  9.  
  10.  
  11. class DrawFont {
  12.  
  13. function DrawFont($details) {
  14. // Get size in pixels, must convert to points for GD2.
  15. // Because GD2 assumes 96 pixels per inch and we use more "standard" 72.
  16. $this->textSizeMax = $details['size'];
  17. $this->font = $details['font'];
  18. $this->text = $details['text'];
  19. $this->image = $details['image'];
  20. $this->color = $details['color'];
  21. $this->shadowColor = $details['shadowColor'];
  22. $this->textAlign = "C";
  23.  
  24. $this->imageHSize = $details['imageHSize'];
  25. $this->imageVSize = $details['imageVSize'];
  26.  
  27. $this->marginL = $details['marginL'];
  28. $this->marginR = $details['marginR'];
  29.  
  30. $this->marginT = $details['marginT'];
  31. $this->marginB = $details['marginB'];
  32.  
  33. $this->ComputeTextDimensions($this->font, $this->text);
  34.  
  35. }
  36.  
  37.  
  38. /**
  39.   * Compute the dimensions of the text.
  40.   */
  41. function ComputeTextDimensions($fontFile, $text)
  42. {
  43. $this->textAreaWidth = $this->imageHSize - $this->marginL - $this->marginR;
  44. $this->textAreaHeight = $this->imageVSize - $this->marginT - $this->marginB;
  45.  
  46. // Handle text on several lines
  47. $this->lines = explode(' ', $text);
  48. $this->lineNb = count($this->lines);
  49.  
  50.  
  51.  
  52.  
  53. if ($this->lineNb == 1)
  54. {
  55.  
  56. $this->textSize[0] = $this->textSizeMax;
  57.  
  58. $bb = ImageTTFBBox($this->textSize[0], 0, $fontFile, $text);
  59. $this->textWidth[0] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
  60. $this->maxTextWidth = $this->textWidth[0];
  61. $this->textHeight[0] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
  62.  
  63.  
  64. $this->textSize[0] = $this->textSizeMax;
  65. while ($this->textWidth[0] > $this->textAreaWidth && $this->textSize[0] > 1) {
  66.  
  67. $this->textSize[0]--;
  68.  
  69. $bb = ImageTTFBBox($this->textWidth[$i], 0, $fontFile, $text);
  70. $this->textWidth[0] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
  71. $this->maxTextWidth = $this->textWidth[0];
  72. $this->textHeight[0] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
  73.  
  74. }
  75.  
  76.  
  77. }
  78. else
  79. {
  80. for ($i = 0; $i < $this->lineNb; $i++)
  81. {
  82. $this->textSize[$i] = $this->textSizeMax;
  83.  
  84. $bb = ImageTTFBBox($this->textSize[$i], 0, $fontFile, $this->lines[$i]);
  85. $this->textWidth[$i] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
  86. $this->maxTextWidth = max($this->maxTextWidth, $this->textWidth[$i]);
  87. $this->textHeight[$i] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
  88.  
  89.  
  90.  
  91. while ($this->textWidth[$i] > $this->textAreaWidth && $this->textSize[$i] > 1) {
  92.  
  93. $this->textSize[$i]--;
  94.  
  95. $bb = ImageTTFBBox($this->textSize[$i], 0, $fontFile, $this->lines[$i]);
  96. $this->textWidth[$i] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
  97. $this->maxTextWidth = max($this->maxTextWidth, $this->textWidth[$i]);
  98. $this->textHeight[$i] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
  99.  
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
  106. }
  107. }
  108.  
  109. /*
  110.   // Is the given text area width too small for asked text?
  111.   if ($this->maxTextWidth > $this->textAreaWidth)
  112.   {
  113.   // Yes! Increase button size
  114.   $this->textAreaWidth = $this->maxTextWidth;
  115.   $this->imageHSize = $this->textAreaWidth + $this->marginL + $this->marginR;
  116.  
  117.  
  118.   }
  119.   */
  120.  
  121. // Now compute the text positions given the new (?) text area width
  122. if ($this->lineNb == 1)
  123. {
  124. $this->ComputeTextPosition(0, $this->textSize[0], $fontFile, $text, false);
  125. }
  126. else
  127. {
  128. for ($i = 0; $i < $this->lineNb; $i++)
  129. {
  130. $this->ComputeTextPosition($i, $this->textSize[$i], $fontFile, $this->lines[$i], false);
  131. }
  132. }
  133. }
  134.  
  135. /**
  136.   * Compute xText and yText (text position) for the given text.
  137.   */
  138. function ComputeTextPosition($index, $textSize, $fontFile, $text, $centerAscDesc)
  139. {
  140. switch ($this->textAlign)
  141. {
  142. case 'L':
  143. $this->xText[$index] = $this->marginL;
  144. break;
  145. case 'R':
  146. $this->xText[$index] = $this->marginL +
  147. $this->textAreaWidth - $this->textWidth[$index];
  148. break;
  149. case 'C':
  150. default:
  151. $this->xText[$index] = $this->marginL +
  152. ($this->textAreaWidth - $this->textWidth[$index]) / 2;
  153. break;
  154. }
  155.  
  156. if ($centerAscDesc)
  157. {
  158. // Must compute the difference between baseline and bottom of BB.
  159. // I have to use a temporary image, as ImageTTFBBox doesn't use coordinates
  160. // providing offset from the baseline.
  161. $tmpBaseline = 5;
  162. // Image size isn't important here, GD2 still computes correct BB
  163. $tmpImage = ImageCreate(5, 5);
  164. $bbt = ImageTTFText($tmpImage, $this->textSizeMax, 0, 0, $tmpBaseline,
  165. $this->color, $fontFile, $text);
  166. // Bottom to Baseline
  167. $baselinePos = $bbt[GDBB_BOTTOM] - $tmpBaseline;
  168. ImageDestroy($tmpImage);
  169. $this->yText[$index] = $this->marginT + $this->textAreaHeight -
  170. ($this->textAreaHeight - $this->textHeight) / 2 - $baselinePos + 0.5;
  171. }
  172. else
  173. {
  174. // Actually, we want to center the x-height, ie. to keep the baseline at same pos.
  175. // whatever the text is really, ie. independantly of ascenders and descenders.
  176. // This provide better looking buttons, as they are more consistent.
  177. $bbt = ImageTTFBBox($textSize, 0, $fontFile, "moxun");
  178. $tmpHeight = $bbt[GDBB_BOTTOM] - $bbt[GDBB_TOP];
  179. $this->yText[$index] = $this->marginT + $this->textAreaHeight -
  180. ($this->textAreaHeight - $tmpHeight) / 2 + 0.5;
  181. }
  182. }
  183.  
  184. /**
  185.   * Add the text to the button.
  186.   */
  187. function DrawText()
  188. {
  189.  
  190. $this->maxTextHeight = 0;
  191.  
  192. // find maxTextHeight
  193. for ($i = 0; $i < $this->lineNb; $i++)
  194. {
  195. if ($this->textHeight[$i] > $this->maxTextHeight) {
  196. $this->maxTextHeight = $this->textHeight[$i];
  197. }
  198. }
  199.  
  200. for ($i = 0; $i < $this->lineNb; $i++)
  201. {
  202. // Increase slightly line height
  203. $yText = $this->yText[$i] + $this->maxTextHeight * 1.1 *
  204. ($i - ($this->lineNb - 1) / 2);
  205.  
  206. ImageTTFText($this->image, $this->textSize[$i], 0,
  207. $this->xText[$i]+2, $yText+2, $this->shadowColor, $this->font, $this->lines[$i]);
  208.  
  209. ImageTTFText($this->image, $this->textSize[$i], 0,
  210. $this->xText[$i], $yText, $this->color, $this->font, $this->lines[$i]);
  211.  
  212.  
  213. }
  214. }
  215.  
  216. }
  217.  
  218.  
  219.  
  220.  
  221.  
  222. // Script starts here
  223.  
  224. $im = imagecreatefromjpeg("bg/bg3.jpg");
  225.  
  226. $color = imagecolorallocate($im, 235, 235, 235);
  227. $shadowColor = imagecolorallocate($im, 90, 90, 90);
  228.  
  229. $details = array("image" => $im,
  230. "font" => "fonts/calibri.ttf",
  231. "text" => "rapgra",
  232. "color" => $color,
  233. "shadowColor" => $shadowColor,
  234. "size" => 40,
  235. "imageHSize" => 200,
  236. "imageVSize" => 250,
  237. "marginL" => 5,
  238. "marginR" => 5,
  239. "marginT" => 5,
  240. "marginB" => 5);
  241.  
  242. $dofontobj = new DrawFont($details);
  243. $dofontobj->DrawText();
  244.  
  245. imagepng($im);
  246. imagedestroy($im);
  247. unset($px);
  248.  
  249. ?>


Jednak za nic nie udaj mi si to uruchomi - cay czas pokazuj komunikat:

  1. Obrazek &#8222;http://localhost/tablice/mds/platecreator/processImage.php” nie moe zosta wywietlony, poniewa zawiera bdy.



a po ukryciu nagwka od image/png ukazuj si takie co:

  1. PNG  IHDRfOd IDATx]W9&5$-!%RhFq* "5P"PqaE"XƂ$")nI5S\I(MJ;?͝l,½7{&%3c7M+Q 0&C=Ο?%bb߻KZk[z|`C 3O:uʕp|^؈Y!Zl ,Ç_vGSƫW/\u6/0oK)Oi㟸~g<\ngΜy}h.qgG57{o1tG#ݑ~H?@w ;tG#ݑ~H?@w ;tG#ݑ~H?@w ;tG#ݑ~H?@w ;tG#ݑ~H?@w ;tG#ݑ~H?@w ;tG#ݑ~H?@w ;tgR^OFdD)%3#Z͓tRJkRJ7 `<`b>3ܛ&Dv0Ius߷or?"2sXK'Ncǎ1<+~G̓Os pCNIYsg/} GHfF-cOK@{Pmi(?d5VWo" P3ڰOCq?+;Q[}lx`k"J-G=OrьX~+#^}m}}=3Ϩe˛–2<ֺܲ;Q3d.2wK/Z{}1׭Ǐ_>;Ɉw/d;׿[o۱s6 _sy|鵵'JD,|SP" g䟚hIENDB`









Przejdmy do rzeczy. Potrzebuj:
wyrodkowa tekst
doda ewentualny margines do niego
zmieni automatycznie rozmiar czcionki mniejszy/wikszy tak aby zawsze si zmieci na obrazku

w/w klasa zapewnia te wszystkie udogodnienia tylko za nic nie udaj mi si jej odpali. Co mona z tym zrobi? znacie jakie inne rozwizania do tych problemw?

Z gry dzikuj za pomoc!!
markonix
Przed znacznikiem nie ma przypadkiem biaego znaku (spacji)?
tomeknh
A ja siedziaem nad tym w nocy tyle czasu :-) Dzikujexclamation.gif!!
Polecam robi przerwy ;-)
To jest wersja lo-fi gwnej zawartoci. Aby zobaczy pen wersj z wiksz zawartoci, obrazkami i formatowaniem prosz kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.