How to account for font swash with PHP and GD - php

I have the following code to print text on an image. I am also adding a debug box around the text. However, I noticed the text on the left lies outside of the box that PHP gives me with imagettfbbox.
This looks like an issue with the font swash. Is there anyway to account for this? Can I figure out the distance between the start of the swash and the actual position imagettfbbox gives to me?
I don't think this is an issue with the font, as I tried it with a few script style fonts and the results were similar.
<?php
$font = 'scriptin.ttf';
$text = 'Ipsum';
$size = 30;
$image = imagecreatetruecolor(200, 200);
$fontColour = imagecolorallocate($image, hexdec('11'), hexdec('11'), hexdec('11'));
$bgColour = imagecolorallocate($image, hexdec('CC'), hexdec('CC'), hexdec('CC'));
imagefilledrectangle($image, 0, 0, 200, 200, $bgColour);
$dimensions = imagettfbbox($size, 0, $font, $text);
imagefilledrectangle(
$image,
$dimensions[0] + 40,
$dimensions[7] + 50,
$dimensions[2] + 40,
$dimensions[3] + 50,
imagecolorallocate($image, mt_rand(1, 180), mt_rand(1, 180), mt_rand(1, 180))
);
imagettftext(
$image,
$size,
0,
40,
50,
$fontColour,
$font,
$text
);
header('Content-Type: image/png');
imagepng($image);
The code and font is available here: https://github.com/AydinHassan/image-swash-example
If you point a VHOST at the repository, you can just hit swash.php

Edit: This appears to be fixed in PHP 7.0.12 (bug #53504) so the code below shouldn't be required.
Based on a comment in the PHP manual I've written the following function to calculate and return the difference between where GD thinks the left side of the bounding box is and where the leftmost pixel is found:
function xadjust($size, $angle, $fontfile, $text)
{
$bbox = imagettfbbox($size, $angle, $fontfile, $text);
$width = $bbox[4] - $bbox[6]; // upper right x - upper left x;
$height = $bbox[1] - $bbox[7]; // lower left y - upper left y;
// create an image with height and width doubled to fit any 'swash'.
$im = imagecreatetruecolor($width * 2, $height * 2);
// set background color to opaque black.
imagefill($im, 0, 0, 0x00000000);
// draw the text in opaque white.
imagettftext(
$im,
$size,
0,
$width / 2,
$height,
0x00ffffff,
$fontfile,
$text
);
// set the min-width to its possible maximum.
$min_x = $width * 2;
for ($x = 0; $x < $width * 2; $x++) {
// each x-pixel (horizontal)
for ($y = 0; $y < $height * 2; $y++) {
// each y-pixel (vertical)
if (imagecolorat($im, $x, $y) > 0) {
// non-black pixel found!
$min_x = min($x, $min_x);
}
}
}
imagedestroy($im);
// return the difference between where GD thinks the bounding box is and
// where we found the leftmost non-black pixel.
return (($width / 2) - $min_x) - abs($bbox[0]);
}
This can be integrated to your script fairly easily:
$font = 'scriptin.ttf';
$text = 'Ipsum';
$size = 30;
$image = imagecreatetruecolor(200, 200);
$fontColour = imagecolorallocate($image, hexdec('11'), hexdec('11'), hexdec('11'));
$bgColour = imagecolorallocate($image, hexdec('CC'), hexdec('CC'), hexdec('CC'));
imagefilledrectangle($image, 0, 0, 200, 200, $bgColour);
$xadjust = xadjust($size, 0, $font, $text); // 1. get the adjust value.
$dimensions = imagettfbbox($size, 0, $font, $text);
imagefilledrectangle(
$image,
$dimensions[0] + 40 - $xadjust, // 2. move the left-side of the box to the left.
$dimensions[7] + 50,
$dimensions[2] + 40 - $xadjust, // 3. move the right-side of the box to the left.
$dimensions[3] + 50,
imagecolorallocate($image, mt_rand(1, 180), mt_rand(1, 180), mt_rand(1, 180))
);
imagettftext(
$image,
$size,
0,
40,
50,
$fontColour,
$font,
$text
);
header('Content-Type: image/png');
imagepng($image);
This gives me the following output:
I've run it with a few other fonts and sizes and it seems to be accurate to within 1 pixel.

Related

Trying to generate 3D bumpmap text in PHP

I have a website I monetize with numerous original pictures on it, I want people to visit the website to see the original pictures and have search engines only show the pictures with transparent watermarks.
The following is an example of what I mean by a transparent watermark. Except of course, I replace "Test!" with the company name.
Here's the PHP code I created so far:
<?php
$txt = "TEST!";
header( "Content-type: image/jpeg", true );
$w = imagefontwidth(5) * ( strlen( $txt ) + 1 );
$h = imagefontheight(5) * 2;
$i2 = imagecreatetruecolor( $w,$h );
imagesavealpha( $i2, true );
imagealphablending( $i2, false );
imagefill( $i2, 0, 0, imagecolorallocatealpha( $i2, 255, 255, 255, 127 ) );
imagestring( $i2, 3, 10, 10, $txt, imagecolorallocate( $i2, 255, 0, 0) );
$i = imagecreatefromjpeg( "someimage.jpg" );
imagecopyresized( $i, $i2, 2, 2, 0, 0, $w * 5, $h * 7, $w, $h );
imagejpeg( $i, null, 100 );
imagedestroy( $i );
imagedestroy( $i2 );
?>
$i2 is the resource variable for the image box in which I added the text, and $i1 is for the large image to place the text on. $w and $h represent width and height of the text image box. This code is able to produce the text on top of the image without the background box showing, but it doesn't produce the bump-map effect like what is shown in the above image.
Can anyone guide me as to what functions, mathematics or code I need to use to create the bump-map effect?
I feel my solution requires special manipulation of a block of pixels but I don't know how to do it for this effect.
I also want to stick with the PHP GD Image library.
I'd recommend using a semi-transparent PNG and adding it as a watermark rather than using the imagestring functions.
With the imagestring functions you'd theoretically needs to create two strings, one slightly higher and to the left than the other, and then somehow calculate the pixels that where both the strings are present and make that transparent. This will then mean the left-over parts would be black / white and give that 3d effect.
Example 1
If the watermark is the same for each image, then you can use a semi-transparent PNG. The benefit of this is that it allows for far more effects, and as a result a better result.
<?php
// Watermark will take up 80% of the photo's width
$size = 80;
$im = imagecreatefromjpeg(dirname(__FILE__) . '/photo.jpg');
$oi = imagecreatefrompng(dirname(__FILE__) . '/watermark.png');
$w1 = imagesx($im);
$h1 = imagesy($im);
$w2 = imagesx($oi);
$h2 = imagesy($oi);
$w = $w1 - (($w1 / 100) * (100 - $size));
$h = ($w * $h2) / $w2;
imagecopyresampled($im, $oi, $w1 / 2 - $w / 2, $h1 / 2 - $h / 2, 0, 0, $w, $h, $w2, $h2);
header('Content-type: image/jpg');
imagejpeg($im, null, 100);
imagedestroy($im);
Will produce the following output:
Original Photo / Watermark
Example 2
If the watermark needs to be different for each image, then you can use imagettftext to create the text using a font of your choosing (much better than imagestring but limited as to what effects you can do).
<?php
$size = 55;
$angle = 15;
$text = uniqid();
$font = 'BebasNeue.otf'; // http://www.dafont.com/bebas-neue.font
$im = imagecreatefromjpeg(dirname(__FILE__) . '/photo.jpg');
$w = imagesx($im);
$h = imagesy($im);
$text_colour_1 = imagecolorallocatealpha($im, 0, 0, 0, 99);
$text_colour_2 = imagecolorallocatealpha($im, 255, 255, 255, 90);
$box = imagettfbbox($size, $angle, dirname(__FILE__) . '/' . $font, $text);
imagettftext($im, $size, $angle, (($w - $box[4]) / 2) - 1, (($h - $box[5]) / 2) - 1, $text_colour_1, dirname(__FILE__).'/' . $font, $text);
imagettftext($im, $size, $angle, ($w - $box[4]) / 2, ($h - $box[5]) / 2, $text_colour_2, dirname(__FILE__).'/' . $font, $text);
header('Content-type: image/jpg');
imagejpeg($im, null, 100);
Output:

Barcode generated does not get read

I use the following code to create an EAN13 barcode. The barcode is created without any issues, but when I scan it it is not recognized.
What is the problem?
include('php-barcode.php');
$font = 'fonts/GSMT.TTF';
$fontSize = 16; // GD1 in px ; GD2 in point
$marge = 10; // between barcode and hri in pixel
$x = 80; // barcode center
$y = 80; // barcode center
$height = 50; // barcode height in 1D ; module size in 2
$width = 2; // barcode height in 1D ; not use in 2D
$angle = 0;
$code = "111110001001"; // barcode, of course ;)
$type = "ean13"; //
$im = imagecreatetruecolor(180, 180);
$black = ImageColorAllocate($im,0x00,0x00,0x00);
$white = ImageColorAllocate($im,0xff,0xff,0xff);
imagefilledrectangle($im, 0, 0, 180, 180, $white);
$data = Barcode::gd($im, $black, $x, $y, $angle, $type, array('code'=>$code), $width, $height);
if ( isset($font) ){
$box = imagettfbbox($fontSize, 0, $font, $data['hri']);
$len = $box[2] - $box[0];
Barcode::rotate(-$len / 2, ($data['height'] / 2) + $fontSize + $marge, $angle, $xt, $yt);
imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $black, $font, $data['hri']);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

Unable to horizontally center 'm' with GD2

My goal is to draw a horizontally centered m. I therefore calculate the width of the letter, substract that value from the total width and finally divide by 2. The result should be the distance from the left (or equally from the right).
However, the 'm' is always misplaced. I also noticed that some fonts may not trigger the problematic behavior. Note that my script correctly works for all other latin characters.
Arial:
Bitstream Vera Sans:
<?php
$totalWidth = 100;
$totalHeight = 100;
$font = 'Arial.ttf';
$img = imagecreatetruecolor($totalWidth, $totalHeight);
$red = imagecolorallocate($img, 255, 0, 0);
$fontSize = 100;
$bbox = imagettfbbox($fontSize, 0, $font, 'm');
$width = max($bbox[2], $bbox[4]) - max($bbox[0], $bbox[6]);
$centeredX = ($totalWidth - $width) / 2;
imagettftext($img, 100, 0, $centeredX, 100, $red, $font, 'm');
imagepng($img, 'testcase.png');
imagedestroy($img);
There is a small space left of each letter, and this is different each letter. Somebody on PHP.net wrote a solution for this: http://www.php.net/manual/en/function.imagettfbbox.php#97357
You need to adjust your code a little bit.
$totalWidth = 100;
$totalHeight = 100;
$font = 'Arial.ttf';
// change letter to see it with different letters
$letter = "m";
$img = imagecreatetruecolor($totalWidth, $totalHeight);
$red = imagecolorallocate($img, 255, 0, 0);
$fontSize = 100;
$bbox = calculateTextBox($fontSize, 0, $font, $letter);
$centeredX = (($totalWidth - $bbox['width']) / 2);
// here left coordinate is subtracted (+ negative value) from centeredX
imagettftext($img, 100, 0, $centeredX + $bbox['left'], 100, $red, $font, $letter);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);

get lowermost left corner of an iamge and write text there

I'm trying to get the lowermost left (x,y) coordinates of an image.
I'm doing that to be able to write a text in different-sized picture, in the left lowermost corner. Below is the code. Could you please help?
<?php
$white = imagecolorallocate($image2, 255, 255, 255);
$grey = imagecolorallocate($image2, 128, 128, 128);
$black = imagecolorallocate($image2, 0, 0, 0);
$textsize = 30;
$size = imagettfbbox($textsize, 0, $font, $text);
$xsize = abs($size[0]) + abs($size[2]);
$ysize = abs($size[5]) + abs($size[1]);
$image2size = getimagesize("image2.jpg");
$textleftpos = round(($image2size[0] - $xsize) / 2);
$texttoppos = round(($image2size[1] + $ysize) / 2);
imagettftext($image2, $textsize, 0, $textleftpos, $texttoppos, $white, $font, $text);
imagejpeg($image2, "image3.jpg");
?>
$indentfromedge = 5; // or whatever you want for an indent
$textleftpos = $indentfromedge;
$texttoppos = $image2size[1] - $ysize - $indentfromedge;
I think is what you're going for. Replace the two lines with $text*pos in them with the above code.
On the left edge means an x-coordinate of 0
On the bottom edge means an y-coordinate equal to the height of the image minus the height of the text
So, say your text size is 30px:
$size = imagesize($img);
$x = 0;
$y = $size[1] - 30;
// assuming you're using GD1
imagettftext($image, 30, 0, $x, $y, $color, $font, "sample text");

PHP GD ttftext center alignment

I'm using imagettftext to make a bar graph and at the top of each bar I want to put the value.
I have the following variables for each bar (which are really rectangles)
$x1
$y1
$x2
$y2
$imagesx
$imagesy
$font_size
Also, The fontsize should decrease as the string length increases.
Do it like this. Remember to place the font file "arial.ttf" in current directory:
<?php
// Create a 650x150 image and create two colors
$im = imagecreatetruecolor(650, 150);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// Set the background to be white
imagefilledrectangle($im, 0, 0, 649, 149, $white);
// Path to our font file
$font = './arial.ttf';
//test it out
for($i=2;$i<10;$i++)
WriteTextForMe($im, $font, str_repeat($i, $i), -140 + ($i*80), 70 + rand(-30, 30), -160 + (($i+1)*80), 150, $black);
//this function does the magic
function WriteTextForMe($im, $font, $text, $x1, $y1, $x2, $y2, $allocatedcolor)
{
//draw bars
imagesetthickness($im, 2);
imagerectangle($im, $x1, $y1, $x2, $y2, imagecolorallocate($im, 100,100,100));
//draw text with dynamic stretching
$maxwidth = $x2 - $x1;
for($size = 1; true; $size+=1)
{
$bbox = imagettfbbox($size, 0, $font, $text);
$width = $bbox[2] - $bbox[0];
if($width - $maxwidth > 0)
{
$drawsize = $size - 1;
$drawX = $x1 + $lastdifference / 2;
break;
}
$lastdifference = $maxwidth - $width;
}
$size--;
imagettftext($im, $drawsize, 0, $drawX, $y1 - 2, $allocatedcolor, $font, $text);
}
// Output to browser
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
It uses imagettfbbox function to get width of the text, then loops over the font size to get correct size, centers it and displays it.
So, it outputs the following:
You can calculate the center of your text by using PHP's imagettfbbox-function:
// define text and font
$text = 'some bar label';
$font = 'path/to/some/font.ttf';
// calculate text size (this needs to be adjusted to suit your needs)
$size = 10 / (strlen($text) * 0.1);
// calculate bar center
$barCenter = $x1 + ($x2 - $x1) / 2;
// calculate text position (centered)
$bbox = imagettfbbox($size, 0, $font, $text);
$textWidth = $bbox[2] - $bbox[0];
$positionX = $textWidth / 2 + $barCenter;
$positionY = $y1 - $size;
EDIT: Updated the code to do all the work for you.

Categories