Pages: [1]   Go Down
Send this topic | Print
Author Topic: Text on Image Centered GD PHP [solution impossible on 110mb]  (Read 949 times)
darrenbeige
Authority Member
****
Online Online

Posts: 834


WWW
« on: November 23, 2008, 09:40:50 PM »

I am using an adapted version of the code below, from CodingTuts.com
Code:
<?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
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6669



WWW
« 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
Authority Member
****
Online Online

Posts: 834


WWW
« 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
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6669



WWW
« 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/imagestring

If 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
Authority Member
****
Online Online

Posts: 834


WWW
« Reply #4 on: November 23, 2008, 10:24:09 PM »

I have changed my code to this:

Code:
<?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
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6669



WWW
« 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):
Code:
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
Knock. Knock.
Loyal 110MB Member
*******
Online Online

Posts: 2326


I came, I saw, I strutted.


WWW
« 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
Authority Member
****
Online Online

Posts: 834


WWW
« 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 Offline

Posts: 15644


experimental theologian


WWW
« 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

Hi! I’m a signature virus! Add me to your signature to help me spread.
spam me: ispamspot@gmail.com

blog | my work @ deviantART | Imagine-ng image editor
Piotr GRD
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6669



WWW
« 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
Knock. Knock.
Loyal 110MB Member
*******
Online Online

Posts: 2326


I came, I saw, I strutted.


WWW
« 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:
Code:
<?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... undecided
Logged

Pages: [1]   Go Up
Send this topic | Print
Jump to: