Jump to content

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 = 'fffffddddddfffff@gmail.com';
$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.

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.