Mam taki kod:
<?php class DrawFont { function DrawFont($details) { // Get size in pixels, must convert to points for GD2. // Because GD2 assumes 96 pixels per inch and we use more "standard" 72. $this->textSizeMax = $details['size']; $this->font = $details['font']; $this->text = $details['text']; $this->image = $details['image']; $this->color = $details['color']; $this->shadowColor = $details['shadowColor']; $this->textAlign = "C"; $this->imageHSize = $details['imageHSize']; $this->imageVSize = $details['imageVSize']; $this->marginL = $details['marginL']; $this->marginR = $details['marginR']; $this->marginT = $details['marginT']; $this->marginB = $details['marginB']; $this->ComputeTextDimensions($this->font, $this->text); } /** * Compute the dimensions of the text. */ function ComputeTextDimensions($fontFile, $text) { $this->textAreaWidth = $this->imageHSize - $this->marginL - $this->marginR; $this->textAreaHeight = $this->imageVSize - $this->marginT - $this->marginB; // Handle text on several lines if ($this->lineNb == 1) { $this->textSize[0] = $this->textSizeMax; $bb = ImageTTFBBox($this->textSize[0], 0, $fontFile, $text); $this->textWidth[0] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT]; $this->maxTextWidth = $this->textWidth[0]; $this->textHeight[0] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP]; $this->textSize[0] = $this->textSizeMax; while ($this->textWidth[0] > $this->textAreaWidth && $this->textSize[0] > 1) { $this->textSize[0]--; $bb = ImageTTFBBox($this->textWidth[$i], 0, $fontFile, $text); $this->textWidth[0] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT]; $this->maxTextWidth = $this->textWidth[0]; $this->textHeight[0] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP]; } } else { for ($i = 0; $i < $this->lineNb; $i++) { $this->textSize[$i] = $this->textSizeMax; $bb = ImageTTFBBox($this->textSize[$i], 0, $fontFile, $this->lines[$i]); $this->textWidth[$i] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT]; $this->textHeight[$i] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP]; while ($this->textWidth[$i] > $this->textAreaWidth && $this->textSize[$i] > 1) { $this->textSize[$i]--; $bb = ImageTTFBBox($this->textSize[$i], 0, $fontFile, $this->lines[$i]); $this->textWidth[$i] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT]; $this->textHeight[$i] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP]; } } } /* // Is the given text area width too small for asked text? if ($this->maxTextWidth > $this->textAreaWidth) { // Yes! Increase button size $this->textAreaWidth = $this->maxTextWidth; $this->imageHSize = $this->textAreaWidth + $this->marginL + $this->marginR; } */ // Now compute the text positions given the new (?) text area width if ($this->lineNb == 1) { $this->ComputeTextPosition(0, $this->textSize[0], $fontFile, $text, false); } else { for ($i = 0; $i < $this->lineNb; $i++) { $this->ComputeTextPosition($i, $this->textSize[$i], $fontFile, $this->lines[$i], false); } } } /** * Compute xText and yText (text position) for the given text. */ function ComputeTextPosition($index, $textSize, $fontFile, $text, $centerAscDesc) { switch ($this->textAlign) { case 'L': $this->xText[$index] = $this->marginL; break; case 'R': $this->xText[$index] = $this->marginL + $this->textAreaWidth - $this->textWidth[$index]; break; case 'C': default: $this->xText[$index] = $this->marginL + ($this->textAreaWidth - $this->textWidth[$index]) / 2; break; } if ($centerAscDesc) { // Must compute the difference between baseline and bottom of BB. // I have to use a temporary image, as ImageTTFBBox doesn't use coordinates // providing offset from the baseline. $tmpBaseline = 5; // Image size isn't important here, GD2 still computes correct BB $tmpImage = ImageCreate(5, 5); $bbt = ImageTTFText($tmpImage, $this->textSizeMax, 0, 0, $tmpBaseline, $this->color, $fontFile, $text); // Bottom to Baseline $baselinePos = $bbt[GDBB_BOTTOM] - $tmpBaseline; ImageDestroy($tmpImage); $this->yText[$index] = $this->marginT + $this->textAreaHeight - ($this->textAreaHeight - $this->textHeight) / 2 - $baselinePos + 0.5; } else { // Actually, we want to center the x-height, ie. to keep the baseline at same pos. // whatever the text is really, ie. independantly of ascenders and descenders. // This provide better looking buttons, as they are more consistent. $bbt = ImageTTFBBox($textSize, 0, $fontFile, "moxun"); $tmpHeight = $bbt[GDBB_BOTTOM] - $bbt[GDBB_TOP]; $this->yText[$index] = $this->marginT + $this->textAreaHeight - ($this->textAreaHeight - $tmpHeight) / 2 + 0.5; } } /** * Add the text to the button. */ function DrawText() { $this->maxTextHeight = 0; // find maxTextHeight for ($i = 0; $i < $this->lineNb; $i++) { if ($this->textHeight[$i] > $this->maxTextHeight) { $this->maxTextHeight = $this->textHeight[$i]; } } for ($i = 0; $i < $this->lineNb; $i++) { // Increase slightly line height $yText = $this->yText[$i] + $this->maxTextHeight * 1.1 * ($i - ($this->lineNb - 1) / 2); ImageTTFText($this->image, $this->textSize[$i], 0, $this->xText[$i]+2, $yText+2, $this->shadowColor, $this->font, $this->lines[$i]); ImageTTFText($this->image, $this->textSize[$i], 0, $this->xText[$i], $yText, $this->color, $this->font, $this->lines[$i]); } } } // Script starts here $im = imagecreatefromjpeg("bg/bg3.jpg"); $color = imagecolorallocate($im, 235, 235, 235); $shadowColor = imagecolorallocate($im, 90, 90, 90); "font" => "fonts/calibri.ttf", "text" => "rapgra", "color" => $color, "shadowColor" => $shadowColor, "size" => 40, "imageHSize" => 200, "imageVSize" => 250, "marginL" => 5, "marginR" => 5, "marginT" => 5, "marginB" => 5); $dofontobj = new DrawFont($details); $dofontobj->DrawText(); imagepng($im); imagedestroy($im); ?>
Jednak za nic nie udaj mi si to uruchomi - cay czas pokazuj komunikat:
Obrazek „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:
PNG IHDRfOdIDATx]W9&5$-!%RhFq*"5P"PqaE"XƂ$")nI5S\I(MJ;?͝l,½7{&%3c7M+Q0&C=Ο?%bbKZk[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!!