Jump to content

Recommended Posts

Hello, I am trying to use the GD Library to draw an image with text in it. Here is the code I am using:

 

$img_handle = ImageCreate (230, 20) or die ("Fatal Error: Cannot create image in PHP library GD."); 
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10); 
$txt_color = ImageColorAllocate ($img_handle, 233, 114, 191); 
ImageString ($img_handle, 31, 5, 5,  $row_customer['approx_number_of_units'], $txt_color);

 

This is simply outputting:

 

‰PNG IHDRæ£þÓêPLTE ér¿`ŽL+IDATxœc`ià@IÆðÈ2œÏäÃ<#‹ßUø}ô!Ÿ^ò%õ ÆƳÚýIEND®B`‚

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/55786-gd-image-lib-not-working/
Share on other sites

Ok the trick to place GD images in your page with other html and php content is to put the GD php code in a seperate file. For example:

 

FileName: gd_create_text_image.php

<?php
header("Content-type: image/png");
$string = $_GET['text'];
$img_handle = ImageCreate(100, 20) or die ("Fatal Error: Cannot create image in PHP library GD."); 
$back_color = ImageColorAllocate ($img_handle, 255, 255, 255); 
$txt_color = ImageColorAllocate ($img_handle, 0, 0, 0); 
ImageString($img_handle, 5, 5, 5,  $string, $txt_color);
imagepng($img_handle);
imagedestroy($img_handle);
?> 

 

Then in your page simply use a standard HTML img tag as follow:

 

echo '<img src="gd_create_text_image.php?text=' . $row['approx_number_of_units'] . '">';

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.