Jump to content

First play with GD image creation... crashes and burns.... HELP (please) !!


Mouse

Recommended Posts

Well I have had a little play with this and I am not clear on a couple of bits…

<?php
   $point = 18;
   $text = "Bekky"; // to be users name here

   $size = imagettfbbox($point, 0, "ARIAL", $text);
   $xsize = abs($size[0]) + abs($size[2]);
   $ysize = abs($size[5]) + abs($size[1]);

   $image = imagecreatefrompng("images/badge/sml_nm_tg.png");
   $imagesize = getimagesize("images/badge/sml_nm_tg.png");
   $textleftpos = round(($imagesize[0] - $xsize) / 2);
   $texttoppos = round(($imagesize[1] + $ysize) / 2) + 10;

   $red = ImageColorAllocate($image, 255, 51, 0);
   $grey = ImageColorAllocate($image, 102, 153, 153);

   imagettftext($image, $point, 0, $textleftpos + 1, $texttoppos + 1, $grey, "ARIAL", $text);
   imagettftext($image, $point, 0, $textleftpos, $texttoppos, $red, "ARIAL", $text);

   header("content-type: image/png");
   imagepng($image);
   imagedestroy($image);
?>

 

as you can see there is meant to be red writing with a grey drop shadow but as you can see from this local test screen print…

<img src="http://mouse.nodstrum.com/images/badge/demo.jpg" />

it just isn’t doing the colours thing… even worse <a href="http://mouse.nodstrum.com/nametag.php">when I try this online I get gibberish</a> any ideas? PLEASE????

Link to comment
Share on other sites

Try using the function imagecolorclosest() instead of imagecolorallocat().

 

The reason you're getting "gibberish" online is that you're getting warning messages about not being able to find the font you want. Those warnings put out headers which stop the image from displaying as an image. The "gibberish" is the image as displayed with ASCII characters.

 

Ken

 

Link to comment
Share on other sites

Ken, using the function imagecolorclosest() instead of imagecolorallocat() has done the trick, many thanks.

 

The reason you're getting "gibberish" online is that you're getting warning messages about not being able to find the font you want.

 

how can i rectify this, is there a way to find out which fonts are on the server (i assume) or can i put one in there?

 

Many thanks

 

Mouse

Link to comment
Share on other sites

try this (change the font file path as required)

 

<?php

   $point = 18;
   $text = "Bekky"; // to be users name here
   define ("ARIAL", "c:/windows/fonts/ARIAL.TTF");   // font in ttf function is the font file
   
   $size = imagettfbbox($point, 0, ARIAL, $text);
   $xsize = $size[2] - $size[0];      // size is the diff between left and right cooords, not the sum
   $ysize = $size[3] - $size[1];

   $image = imagecreatefrompng("images/badge/sml_nm_tg.png");
   $width = imagesx($image);
   $height = imagesy($image);
   $textleftpos = round(($width - $xsize) / 2);
   $texttoppos = round(($height - $ysize) / 2) + 10;

   $red = ImageColorAllocate($image, 255, 51, 0);
   $grey = ImageColorAllocate($image, 102, 153, 153);

   imagettftext($image, $point, 0, $textleftpos + 1, $texttoppos + 1, $grey, ARIAL, $text);
   imagettftext($image, $point, 0, $textleftpos, $texttoppos, $red, ARIAL, $text);

   header("content-type: image/png");
   imagepng($image);
   imagedestroy($image);
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.