0

Hey all i am wondering where i am missing the size of the textbox where the text is inside?

$img = imagecreatefromjpeg("Desert.jpg"); //http://s7.postimage.org/ceb440itn/Desert.jpg
$width = imagesx($img);
$height = imagesy($img);

$new_width = $width;
$new_height = $height;

$tmp_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Create some colors
$black = imagecolorallocate($tmp_img, 65, 173, 196);
// Define Font
$font = 'ARIAL.TTF';
// The text to draw
$text = 'This is a test here here blah';
//font size
$size = 75;
// Compute size of text and get the x and y for centering the text into the image
$box = imagettfbbox( $size, 0, $font, $text );   
$x = 5;//($width - $box[2]) / 2 - 5;
$y = 75;//($height - $box[5]) / 2;

// Add some shadow to the text
imagettftext($tmp_img, $size, 0, $x+1, $y-1, $grey, $font, $text);

// Add the text
imagettftext($tmp_img, $size, 0, $x, $y, $black, $font, $text);

imagejpeg($tmp_img, "testing.jpg", 100);

imagedestroy($img);
imagedestroy($tmp_img);

The output image looks like this:

--------------------------
|This is a test here here|blah
|                        |
|                        |--Image
|                      --+----Inside image
|                        |
--------------------------

Notice it does not contain the word blah after here *within* the image itself.

This is what it should look like:

--------------------------
|This is a test here here|
|          blah          |
|                        |--Image
|                      --+----Inside image
|                        |
--------------------------

Where am i missing where it defines the size of the textbox?

StealthRT
  • 10,108
  • 40
  • 183
  • 342

1 Answers1

1

Using a minimum number of edits, I get an undefined var for $grey. The output image is here http://www.laprbass.com/RAY_junk/testing.jpg

So my next question is, eh? I don't have the Arial font. Is this what you're seeing?

$desert = 'http://s7.postimage.org/ceb440itn/Desert.jpg';

$img = imagecreatefromjpeg($desert); //http://s7.postimage.org/ceb440itn/Desert.jpg
$width = imagesx($img);
$height = imagesy($img);

$new_width = $width;
$new_height = $height;

$tmp_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Create some colors
$black = imagecolorallocate($tmp_img, 65, 173, 196);

// Define Font
$font = 'ARIAL.TTF';
$font = 'fonts/verdana.ttf';

// The text to draw
$text = 'This is a test here here blah';

//font size
$size = 22;

// Compute size of text and get the x and y for centering the text into the image
$box = imagettfbbox( $size, 0, $font, $text );   
$x = 5;//($width - $box[2]) / 2 - 5;
$y = 75;//($height - $box[5]) / 2;

// Add some shadow to the text
imagettftext($tmp_img, $size, 0, $x+1, $y-1, $grey, $font, $text);

// Add the text
imagettftext($tmp_img, $size, 0, $x, $y, $black, $font, $text);

imagejpeg($tmp_img, "RAY_junk/testing.jpg", 100);

imagedestroy($img);
imagedestroy($tmp_img);
Ray Paseur
  • 2,106
  • 2
  • 13
  • 18