|
darrenbeige
|
 |
« on: November 23, 2008, 09:40:50 PM » |
|
I am using an adapted version of the code below, from CodingTuts.com <?php // Some basic setup $imageWidth = 300; $imageHeight = 200; $textFont = "Cyclo.ttf"; $textSize = 15; $textString = "CodingTuts.com";
// Set the headers content type header("Content-type: image/png");
// Create the image $image = imagecreatetruecolor($imageWidth, $imageHeight);
// Create some colors $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0);
// Fill the background of our image imagefilledrectangle($image, 0, 0, $imageWidth, $imageHeight, $black);
// Get box info $box = imagettfbbox($textSize, 0, $textFont, $textString);
//Find out the width and height of the text box $textW = $box[2] - $box[0]; $textH = $box[5] - $box[3];
// Calculate the positions $positionLeft = ($imageWidth - $textW)/2; $positionTop = (($imageHeight - $textH)/2);
// Add some text imagettftext($image, $textSize, 0, $positionLeft, $positionTop, $white, $textFont, $textString);
// Output the image to the browser imagepng($image);
// Destroy the image imagedestroy($image); ?> It produces centered text on a black background. How can I tweak it to allow for a presaved image to be the background? I have tried changing a few things, but keep producing errors. Thanks,
|
|
|
|
« Last Edit: November 25, 2008, 02:54:43 AM by darrenbeige »
|
Logged
|
|
|
|
|
Piotr GRD
|
 |
« Reply #1 on: November 23, 2008, 09:55:50 PM » |
|
Instead of imagecreatetruecolor() you can use imagecreatefromgif(), imagecreatefrompng() etc. http://php.net/gd
edit: Note that imagettfbbox(), imagettftext() is not working on 110mb, as Free Type library is not implemented into GD. You need to use imagestring() and GD fonts instead of true type fonts.
|
|
|
|
« Last Edit: November 23, 2008, 09:58:13 PM by Piotr GRD »
|
Logged
|
|
|
|
|
darrenbeige
|
 |
« Reply #2 on: November 23, 2008, 10:01:36 PM » |
|
Note that imagettfbbox(), imagettftext() is not working on 110mb, as Free Type library is not implemented into GD. You need to use imagestring() and GD fonts instead of true type fonts.
AAH. How can I convert it to use GD fonts?
|
|
|
|
|
Logged
|
|
|
|
|
Piotr GRD
|
 |
« Reply #3 on: November 23, 2008, 10:17:26 PM » |
|
You have to drop imagettfbbox() and imagettftext() functions from the code and use imagestring() function. How to exactly use this function: php.net/imagestringIf in-built GD font is not enough for you (like in my blue signature or in the avatar) and you need some fancy fonts you have to convert them into GD type of fonts first to use them http://www.110mb.com/forum/where-can-i-find-gd-fonts-t27341.0.html;msg221224#msg221224
|
|
|
|
|
Logged
|
|
|
|
|
darrenbeige
|
 |
« Reply #4 on: November 23, 2008, 10:24:09 PM » |
|
I have changed my code to this: <?php // Some basic setup $imageWidth = 300; $imageHeight = 200; $textFont = "Cyclo.ttf"; $textSize = 15; $textString = "CodingTuts.com";
// Set the headers content type header("Content-type: image/gif");
// Create the image $image = imagecreatefromgif('spots.gif');
// Create some colors $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0);
// Get box info // $box = imagettfbbox($textSize, 0, $textFont, $textString);
//Find out the width and height of the text box $textW = $box[2] - $box[0]; $textH = $box[5] - $box[3];
// Calculate the positions $positionLeft = ($imageWidth - $textW)/2; $positionTop = (($imageHeight - $textH)/2);
// Add some text imagettftext($image, $textSize, 0, $positionLeft, $positionTop, $white, $textFont, $textString); imagestring($image, $textSize, 1, $positionLeft, $positionTop, $white, $textFont, $textString);
// Output the image to the browser imagegif($image);
// Destroy the image imagedestroy($image); ?> but it returns a "The image “http://wonders-of-the-brain.co.uk/js/image.php” cannot be displayed, because it contains errors." error? Any reason why?
|
|
|
|
|
Logged
|
|
|
|
|
Piotr GRD
|
 |
« Reply #5 on: November 23, 2008, 11:01:07 PM » |
|
You still have imagettftext() which is not working on 110mb, I wrote it already. And the actual error is (if you view the source code in the browser): Call to undefined function imagettftext() in /www/110mb.com/*/htdocs/js/image.php on line 31
|
|
|
|
« Last Edit: November 23, 2008, 11:02:55 PM by Piotr GRD »
|
Logged
|
|
|
|
|
thefluffball
|
 |
« Reply #6 on: November 24, 2008, 04:35:47 AM » |
|
Unfortunately this is impossible to do on 110mb, without huge amounts of extensive coding.
|
|
|
|
|
Logged
|
|
|
|
|
darrenbeige
|
 |
« Reply #7 on: November 25, 2008, 02:53:58 AM » |
|
Oh well. Have to do something else then...
|
|
|
|
|
Logged
|
|
|
|
inp o҉rtb
The Gangsta
Global Moderator
Official 110mb Guru
   
Offline
Posts: 15644
experimental theologian
|
 |
« Reply #8 on: November 25, 2008, 03:15:28 AM » |
|
Well, if you know your font's fixed-width, it should be simple arithmetic.
|
|
|
|
|
Logged
|
|
|
|
|
Piotr GRD
|
 |
« Reply #9 on: November 25, 2008, 03:20:08 AM » |
|
Of course. GD fonts are monospaced, so just count how many letters will fit into the box, do a word wrap and voila. Centering should be quite easy, too (both vertical and horizontal).
|
|
|
|
|
Logged
|
|
|
|
|
thefluffball
|
 |
« Reply #10 on: November 25, 2008, 03:31:48 AM » |
|
I was going to attempt to do it something like this, but it didn't work: <?php
function imagecharbox($textSize, $character){ $area = imagecreatetruecolor ( 30 , 30 ); $white = imagecolorallocate ( $area, 255, 255, 255 ); imagestring ( $area, $textSize, 0, 0, $textString, $white ); $i = 0; for($counter = 0; $counter <= $height; $counter = $counter + 1){ for($counter2 = 0; $counter2 <= $width; $counter2 = $counter2 + 1){ $col = imagecolorat($area, $counter2, $counter); if($col == $white){ $pixelposx[$i] = $counter2; $pixelposy[$i] = $counter; } $i = $i + 1; } $i = $i + 1; } $biggestx = 0; foreach ($pixelposx as $key => $value) { if($value > $biggestx){ $biggestx = $value; } } $biggesty = 0; foreach ($pixelposy as $key => $value) { if($value > $biggesty){ $biggesty = $value; } } $boxcoords = array ( $biggestx, $biggesty ); return $boxcoords; }
$charsize = imagecharbox ( 2, "M" ); echo $charsize[0]."x".$charsize[1];
?> Of course to find the length of a string, you would just create a function to scroll through a string and add the numbers together. It says invalid argument for the foreach's, but I don't understand why... 
|
|
|
|
|
Logged
|
|
|
|
|