Jump to content

Help with imagecreate() ?


Abuda

Recommended Posts

Hi,

I have this tiny file that receives some text via $_GET then returns an image including that text.

The problem is, i might send it a very long string which will result in it being cropped.

 

<?php
$my_img = imagecreate(100, 30);
$background = imagecolorallocate( $my_img, 255, 255, 255 );
$text_colour = imagecolorallocate( $my_img, 0, 0, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagecolortransparent($my_img, $background);
imagestring( $my_img, 4, 0, 0, "yellowwwwwwwww", $text_colour );
header('Content-Type: image/jpeg');
imagepng( $my_img );
?>

 

Is there some way to tell php to set whatever width and height so that my text won't get cropped?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/239327-help-with-imagecreate/
Share on other sites

Painfully brief, but helpful thank you.

 

I ended up with this:

 

<?php
$size = 13;
$font = 'arial.ttf';
$char = '[email protected]';
$rect = imagettfbbox($size, 0, $font, $char);
$imh = 17; // fixed
$imw = $rect[2];

$my_img = imagecreate($imw, $imh);
$background = imagecolorallocate( $my_img, 255, 255, 255 );
$text_colour = imagecolorallocate( $my_img, 0, 0, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagecolortransparent($my_img, $background);
imagestring( $my_img, 3, 0, 0, $char, $text_colour );
header('Content-Type: image/jpeg');
imagepng( $my_img );
?>

 

The only problem now emerges from the difference between the number (13) used in line 1 and between the number (3) used in line 13.

It's causing extra margin for long strings, and cropping for very long strings.

I'll struggle with it for the next few hours and see what pops up.

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.