Question

I'm creating images with different sizes. How can I write text on those images so that the texts always fit to the images?

$text = "some text as example";
$font = "arialbd.ttf"
$fontsize = 26;
offset_x = 0;
offset_y = 0;

$image01 = imagecreate( 1120 , 900 );
$image02 = imagecreate( 400 , 300 ); 
$image03 = imagecreate( 1120 , 900 );

I know that there is the imagefttext function that can apply text to the images but with that function I can only change the font size to make the text bigger. How can I find out that it fits into my image?

Was it helpful?

Solution

If you are looking to scale the font size so that text string fills the image width, try this. Using imagettfbbox, determine the current text box width:

 $bbox = imagettfbbox($fontsize,0,$fontpath,$string);
 $tbwidth = $bbox[2];

Then divide the image width by the $tbwidth to get a factor

 $factor = round(($imgwidth / $tbwidth), 0, PHP_ROUND_HALF_DOWN); //ie. 800/240=3

Multiple the $factor by you $fontsize to get an approximate increase.

 $newfontsize = $fontsize * $factor; //ie. 12(pt) * 3 = 36

Keep in minds, if you're using GD 2.0, fontsize is in Points and not pixels. Your algorithm is going to calculate the difference between your default font size text box (expressed as a text box width) and the image width.

OTHER TIPS

// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(800, 600);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 0, 0, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font_file = 'arial.ttf';
$font_size = '15';

$bbox   = imageftbbox($font_size, 0, $font_file, $text);

$width  = $bbox[2] - $bbox[6];
$height = $bbox[3] - $bbox[7];

// Add the text
imagettftext($im, $font_size, 0, 10, 20, $black, $font_file, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

I recently came across the same situation with transparent backgrounds but the current examples are either not a solution but clues or a solution that doesn't work, so hereby a combined and working solution if anyone need it.

function imagecreater($width = 600, $height = 600) {

    //Create an empty transparent image
    $img = imagecreatetruecolor($width, $height);
    imagealphablending($img, false);
    imagesavealpha($img, true);
    $transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
    imagefill($img, 0, 0, $transparent);

    //Text information
    $text = "some text as example";
    $font = "arialbd.ttf"
    $fontsize = 26; //default font to be altered later


    //simulate a complete text box and get the width
    $bbox = imageftbbox($fontsize, 0, $font, $text);
    $tbwidth = $bbox[2]; 

    //Calculate different between our transparent image and text box
    //I've added a little padding (20px) since the text sometimes crossing the edge.. 
    $factor = (($width - 20) / $tbwidth);

    //Alter font size with difference to fit fully image
    $newfontsize = $fontsize * $factor;

    //Find the horisontal center
    $bbox = imageftbbox($newfontsize, 0, $font, $text);
    $newheight = ($height / 2) + (($bbox[3] - $bbox[7]) / 2);

    //Set Color of text
    $color = imagecolorallocate($img, 200, 0, 0);

    //Produce our image
    imagettftext($img, $newfontsize, 0, 0, $newheight, $color, $font, $text);

    //Copy image to file and free the cached image
    $target = "testimage.png";
    imagepng($img, $target);
    imagedestroy($img);
}

As rwhite35 mentioning here, please keep in mind that GD 2.0 write font size is in points and not pixels.

Use the imageftbbox function to get the size of the bounding box of the text. You can then adjust the text size to fit the size of the image exactly.

http://www.php.net/manual/en/function.imageftbbox.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top