Fully looking text when using PHP and imagettftext and angle - php

for some reason PHP's imagettftext creates a funny looking text when I create the text at an angle.
Below the source code. I can't post the image 'cause I don't have enough reputation points but the text looks like parts of the letters are cut off.
Help!!!
$text = 'My Text Is Messed Up!!!';
$font = './fonts/arial.ttf';
$font_size = 20;
$font_multiplier = 0.5;
$x=10;
$y=190;
$angle=45;
$width= ($font_size * $font_multiplier) * strlen($text);
echo $width;
$height=200;
$size = imageTTFBBox($font_size, $angle, $font, $text);
$img = imageCreateTrueColor($width, $height);
imageSaveAlpha($img, true);
ImageAlphaBlending($img, false);
$transparentColor = imagecolorallocatealpha($img, 200, 200, 200, 127);
imagefill($img, 0, 0, $transparentColor);
$white = imagecolorallocate($img, 255, 255, 255);
// Add the text
imagettftext($img, $font_size, $angle, $x, $y, $white, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($img, 'welcome-phrase.png');
imagedestroy($img);
EDIT: here's an example of the output (I changed text colour from white to black to make it visible on the white background - AG):

It seems there is an issue with it rotating each character and leaving a "mask" of sorts that isn't rotated and then obscures the text around it, causing the issue you are seeing. It is more visible when you turn off the transparent image fill.
A workaround could be to rotate the image instead of the text. You will have to fix your coordinates but something like this seems to work:
// Add the text
imagettftext($img, $font_size, 0, $x, $y, $black, $font, $text);
$img = imagerotate($img, $angle, $transparentColor);
imageSaveAlpha($img, true);
ImageAlphaBlending($img, false);
Thus the full code would be:
$text = 'My Text Is Messed Up!!!';
$font = './fonts/arial.ttf';
$font_size = 20;
$font_multiplier = 0.5;
$x=10;
$y=190;
$angle=45.0;
$width = ($font_size * $font_multiplier) * strlen($text);
echo $width;
$height=200;
$size = imageTTFBBox($font_size, $angle, $font, $text);
$img = imageCreateTrueColor($width, $height);
$transparentColor = imagecolorallocatealpha($img, 200, 200, 200, 127);
imagefill($img, 0, 0, $transparentColor);
$white = imagecolorallocate($img, 255, 255, 255);
// Add the text
imagettftext($img, $font_size, 0, $x, $y, $white, $font, $text);
$img = imagerotate($img, $angle, $transparentColor);
imageSaveAlpha($img, true);
ImageAlphaBlending($img, false);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($img, 'welcome-phrase.png');
imagedestroy($img);
I moved the imageSaveAlpha and ImageAlphaBlending to the bottom to take care of all that after the rotation has taken place. It's not the best solution but with some tweaking will provide the correct result.

Related

PHP saving imagecreatefrompng

I want to save my pngs, but my code does not allow me to create new pngs or overwrite the existing ones. Ideally every time the page is loaded the image would be saved.
<?php
$width = 640;
$height = 480;
$font = 23;
$string = "This is my text";
$im = #imagecreatetruecolor($width, $height);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 0, 0, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "./DroidSerif-Bold.ttf", $string);
$im = imagecreatefrompng("test.png");
imagedestroy($im);
?>
imagecreateFROMpng as it names indicates creates an image object by reading a .PNG file. In order to save the image as a PNG, you must use the imagepng function:
...
imagettftext($im, $font, 0, 0, $font - 3, $lime, "./DroidSerif-Bold.ttf", $string);
imagepng($im, "test.png");
imagedestroy($im);

Watermark an Image using Text in PHP

I have an image and I'm trying to watermark the image using text.
I have done with code part, but no image see been viewed with watermark text.
Here is the code below:
$imagetobewatermark="images/muggu.png";
list($width,$height)=getimagesize($imagetobewatermark);
$imagetobewatermark=imagecreatetruecolor($width,$height);
$mark=imagecreatefrompng($imagetobewatermark);
imagecopy($imagetobewatermark,$mark,0,0,0,0,$width,$height);
$wartermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
Tell me If I'm wrong.
Thank you.
One problem I can see straight away is that the $imagetobewatermark variable starts off as a string, then becomes a new blank image object (not an existing image), and when you subsequently create the mark image object, it's not going to work because $imagetobewatermark is no longer a string.
Try:
$imagetobewatermark=imagecreatefrompng("images/muggu.png");
$watermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
EDIT:
I failed to notice a typo in your text variable $wartermarktext, which should be $watermarktext.
Correct this and it should work.
You had some spellings wrong and did not use the ressource. Here corrected:
$imagetobewatermark = "muggu.png";
list ($width, $height) = getimagesize($imagetobewatermark);
$res = imagecreatetruecolor($width, $height);
$mark = imagecreatefrompng($imagetobewatermark);
//make sure here to use the ressource, not the filepath
imagecopy($res, $mark, 0, 0, 0, 0, $width, $height);
$watermarktext = "Muggu";
//$font = "../font/century gothic.ttf";
//I copied it to my local test folder
$font = "GOTHIC.TTF";
$fontsize = "15";
//make sure here to use the ressource, not the filepath
$white = imagecolorallocate($res, 255, 255, 255);
//make sure here to use the ressource, not the filepath
imagettftext($res, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
//make sure here to use the ressource, not the filepath
imagepng($res);
//make sure here to use the ressource, not the filepath
imagedestroy($res);
you have to give Public/Absolute Path for Font arial.ttf.
Solution is tested in Laravel 5.8.
you can use this solution. here [ $SourceFile , $DestinationFile ] are Absolute Path of local directory. $imgUrl is HTTP based URL. Watermark will be placed at the top of the image.
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile,$imgUrl) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 255, 255, 255);
$font = public_path('fonts/arial.ttf');
$font_size = 8;
imagettftext($image_p, $font_size, 0, 10, 20, $black,$font , $WaterMarkText);
if ($DestinationFile <> '') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
return $imgUrl;
}
Try this.
$imagetobewatermark = "images/muggu.png";
$watermarktext = "Muggu";
list($width, $height) = getimagesize($imagetobewatermark);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($imagetobewatermark);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$font = "../font/century gothic.ttf";
$font_size = 15;
$white = imagecolorallocate($image_p, 255, 255, 255);
imagettftext($image_p, $font_size, 0, 20, 20, $white, $font, $watermarktext);
header("Content-Type: image/png");
imagepng($image_p, null, 0);
imagedestroy($image);
imagedestroy($image_p);
Try this code, It helps you to provide the watermark for the images.
<?php
header('Content-type: image/jpeg'); //SET THE FORMATE OF THE IMAGE
$jpg_image = imagecreatefromjpeg('images.jpg'); //GIVE LINK FOR SPECIFIED IMAGE
$color = imagecolorallocate($jpg_image, 500, 500, 500); //GIVE COLOR TO WATERMARK TEXT
$font_location = 'BROADW.TTF'; //WATERMARK FONT
$text = "http://www.sanwebtutorials.blogspot.in/"; //WATERMARK TEXT
$x=200; //X CO-ORDINATE
$y=800; //Y CO-ORDINATE
$size=21; //SIZE OF TEXT
imagettftext($jpg_image,$size, 0, $x, $y, $color, $font_location, $text); //PRINT TEXT ON IMAGE
imagejpeg($jpg_image); //SEND IMAGE TO BROWSER
imagedestroy($jpg_image); //DESTROY THE MEMORY
?>

Generation of antialiased transparent png images with php

I've read many artikles about generating transparent images but still have a problem with the transparency. First of all here is my code:
<?php
$text = 'I feel dirty with those borders';
$color = array( 255, 128, 0);
$font = 'arial.ttf';
$size = 44;
// Create the image
$testImg = imagecreatetruecolor(20, 20);
$testCol = imagecolorallocate($testImg, $color[0], $color[1], $color[2]);
$rect = imagettftext($testImg, $size, 0, 0, 0, $testCol, $font, $text);
$width = $rect[4] - $rect[0] + 2;
$height = $rect[1] - $rect[5];
$yOffset = -$rect[5];
imagedestroy($testImg);
$img = imagecreatetruecolor( $width, $height);
imageantialias ( $img, true);
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
$col = imagecolorallocate($img, $color[0], $color[1], $color[2]);
$rect = imagettftext($img, $size, 0, -1, $yOffset, $col, $font, $text);
imagepng($img, "test.png");
imagedestroy($img);
?>
<html>
<body bgcolor="ffFFa0">
<img src="test.png" />
</body>
</html>
If I generate an image with this code I get black borders around the text where the colour of the letters is not totally opaque or completely transparent. I think the reason is that only the pixels which are exactly 100% black are transparent. But what other way is there? I also tried to use alphablending with no success.
Can anybody give me a hint or a link to a good example?
Thanks in advance
parascus

Create transparent png with text from scratch in php

All the examples I've found on the web seem to create pngs with text from an existing png. Is it possible to create a transparent png from scratch and then add text?
The code ive got so far follows (but it doesnt work. just outputs a blank image source)
<?php
$width = 150;
$height = 30;
$text = "My Text";
$fontsize = 5;
$im = imagecreate($width, $height);
$transcolor = imagecolortransparent($im);
imagestring($im, $fontsize, 0, 0, $text, $transcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
<?php
$font = 25;
$string = 'My Text';
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "droid_mono.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Use imagestring instead of imagettftext if you don't want custom font.
Here's the solution based on your original code.
<?php
$width = 640;
$height = 480;
$text = "My Text";
$fontsize = 5;
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
I Think GD is one of the most popular for generating images, with imagettftext.
<?php
$text = 'SOME TEXT';
$font="c:/windows/Fonts/Latinwd.ttf"; //Load font file for windows
$im = ImageCreate(700, 140);
$bla = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $bla); //transparent background
$black = imagecolorallocate($im, 255,255,255);
ImageTTFText ($im, 38, 0, 10, 40, $black, $font, $text);
header('Content-Type: image/png');
ImagePNG($im, 'name.png');
imagedestroy($im);
?>

Create White Box around Text GD-Lib

i want to add a white box around some text i add to an image via GD-Lib.
but i don't know how to do this best.
Here is my current code:
<?php
$textImg = imagecreatefromjpeg($tempImage);
$black = imagecolorallocate($textImg, 0, 0, 0);
$font = 'lib/verdana.ttf';
// Add the text
imagettftext($textImg, 20, 0, imagesx($textImg)*$textData['x']/100, imagesy($textImg)*$textData['y']/100, $black, $font, $textData['text']);
imagejpeg($textImg,$tempImage,$jpegQuality);
?>
I hope you can help me out.
You can use imagettfbbox() to get the coordinates of the bounding box by passing the same settings you use for the text itself (same text, font and size etc).
Once you have these coordinates you can use imagerectangle() to draw a border around the text, or you can use imagefilledrectangle() to draw a solid rectangle. Be sure to call it before you render the text with imagettftext()
A basic example is below but will need some tweaking as most of it is from memory and I suspect the $x and $y calculation could be done better as it probably doesn't work with varying canvas sizes as it is now. However, it demonstrates the principle.
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// The text to draw
$text = 'Testing';
// Replace path by your own font path
$font = 'verdana.ttf';
// Add the text
$bbox = imagettfbbox(20, 0, $font, $text);
$x = $bbox[1] + (imagesx($im) / 2) - ($bbox[4]);
$y = $bbox[3] + (imagesy($im) / 2) - ($bbox[5]);
imagerectangle($im, 0, 0, $x, $y, $white);
imagettftext($im, 20, 0, 0, 20, $white, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);

Categories