imagecreatefrompng() + imagettftext() low quality text - how to anti alias - php

Take the following base image (PNG-24):
We're trying to write text to the image as follows:
<?
ini_set('display_errors', 1);
error_reporting(E_ALL);
//#### Load the base image
$im = imagecreatefrompng("images/SpecialClearanceBlank.png");
imagealphablending($im, false);
imagesavealpha($im, true);
//#### Create the badge
if($im) {
//#### define some colours to use with the image
$white = imagecolorallocate($im, 255, 255, 255);
//#### get the width and the height of the base image
$width = imagesx($im);
$height = imagesy($im);
//#### Define font and text
$font = "/var/www/arial.ttf";
$fontSize = 13;
$angle = 0;
$text = "15%";
//#### calculate the left position of the text:
$dimensions = imagettfbbox($fontSize, $angle, $font, $text);
$textWidth = abs($dimensions[4] - $dimensions[0]);
$leftTextPos = ( $width - $textWidth ) / 2;
//#### finally, write the string:
//imagestring($im, 5, $leftTextPos, $topTextPos, $text, $white);
imagettftext($im, $fontSize, $angle, $leftTextPos + 1, 29, $white, $font, $text);
// output the image
// tell the browser what we're sending it
Header('Content-type: image/png');
// output the image as a png
imagepng($im);
// tidy up
imagedestroy($im);
}
?>
This is producing low quality text (very blocky) - how would one anti alias the text so it looks smooth?
This is the blocky version:
Upon closer analysis of the rendered png (magnified in photoshop) i can see that the text i am writing has no anti aliasing and the pixels being written are almost transparent?
What is causing this - how do i get smooth text?

Explanation:
imagealphablending must be on when using imagettftext on a true-color image, otherwise the aliasing is calculated against the images brackground color instead of the color of each destination pixel.
The correct (explicit) setting would be:
//#### Load the base image
$im = imagecreatefrompng("images/SpecialClearanceBlank.png");
imagealphablending($im, true);
^^^^
Your picture has it enabled by default, that is why setting it to false previously created the non-aliased effect.

Figured it out:
My call to imagealphablending() and imagesavealpha() is what's causing it! If i call these after writing the text it's fine!
(not sure why though - would be interested in an explanation)
Below is the working code to produce this:
<?
Header('Content-type: image/png');
$Percentage = round(#$_GET["value"]);
$root = dirname(__FILE__) . "\\";
//#### Check the Cache
if (file_exists("images/Badges_Discounts/" . $Percentage . ".png") === true) {
//#### Serve image from cache
$im = imagecreatefrompng("images/Badges_Discounts/" . $Percentage . ".png");
//#### Fix transparency
imagealphablending($im, false);
imagesavealpha($im, true);
//#### Output from cache
imagepng($im);
//#### tidy up
imagedestroy($im);
} else {
//#### Load the base image
$im = imagecreatefrompng("images/SpecialClearanceBlank.png");
//#### Create the badge
if($im) {
//#### define some colours to use with the image
$white = imagecolorallocate($im, 255, 255, 255);
//#### get the width and the height of the base image
$width = imagesx($im);
$height = imagesy($im);
//#### Define font and text
$font = $root . "arial.ttf";
$fontSize = 15;
$angle = 0;
$text = $Percentage . "%";
//#### calculate the left position of the text:
$dimensions = imagettfbbox($fontSize, $angle, $font, $text);
$textWidth = abs($dimensions[4] - $dimensions[0]);
$leftTextPos = ( $width - $textWidth ) / 2;
//#### write the XX%
imagettftext($im, $fontSize, $angle, $leftTextPos + 1, 26, $white, $font, $text);
//#### write the word "off"
$dimensions = imagettfbbox($fontSize, $angle, $font, "off!");
$textWidth = abs($dimensions[4] - $dimensions[0]);
$leftTextPos = ( $width - $textWidth ) / 2;
imagettftext($im, 13, $angle, $leftTextPos + 4, 41, $white, $font, "off");
//#### Fix transparency
imagealphablending($im, false);
imagesavealpha($im, true);
//#### Save to cache
imagepng($im, $root . "images\\Badges_Discounts\\" . str_replace("%","",$text) . ".png");
//#### Output to browser
imagepng($im);
//#### tidy up
imagedestroy($im);
}
}
?>

Related

How to write text to images with different sizes using PHP GD

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?
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.
// 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

write text to an image

I have an image on my server and I want to write text to it. Like a watermark. I am able to write text to the image, but I want to add a background to the text so it's easy to read. Here is what I have so far.
header("Content-type: image/jpeg");
$imgPath = 'pic.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 224, 73, 87);
$string = "Please type the word in the circle.";
$fontSize = 8;
$x = 25;
$y = 200;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
in this class you need to have a background base.png and font arial.ttf you can have a diffident font but must be a ttf .if you want to have diffident font format you must make change on code
class SecurityImg{
static function Image_Create($basename){//Create image
$im =imagecreatefrompng ($basename);
//only replace imagecreatefrompng with imagecreatefromjpeg for open jpg instead of png
return($im);
}
static function PutTextOnImage($text,$baseimage,$angel,$xi,$yi){
// Create some colors
$text_color= imagecolorallocate($baseimage, 255, 50, 150);
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($baseimage, 15, $angel, $xi, $yi, $text_color, $font, $text);
return($baseimage);
}
static function Create($imgbase,$TEXT){
$ifp=self::Image_Create($imgbase);
$im=self::PutTextOnImage($TEXT,$ifp,0,10,20);
return($im);
}
}
$Securityimg=new SecurityImg();
$im=$Securityimg->Create("base.png","test");
// Output the image
// Set the content-type
header('Content-Type: image/jpeg');
imagejpeg($im);
// Using imagepng() results in clearer text compared with imagejpeg()
imagedestroy($im);
Check out this :
<?php
$stamp = imagecreatefrompng('stampimg.png');
$im = imagecreatefrompng('mainimage.png');
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);
imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

center text in image

So I have an image, and I am writing text and a color box onto the image. It works but it's being added to the image in the top right corner, but I need it in the center of the image. I tried changing the x and y variables, but it only moves the text and not the white box.
Here is code
$image_filepath = './kenshin.jpg';
saveImageWithText("Welcome to Eureka!", $color, $image_filepath);
function saveImageWithText($text, $color, $source_file) {
$public_file_path = '.';
// Copy and resample the imag
list($width, $height) = getimagesize($source_file);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($source_file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
// Prepare font size and colors
$text_color = imagecolorallocate($image_p, 0, 0, 0);
$bg_color = imagecolorallocate($image_p, 255, 255, 255);
$font = $public_file_path . '/arial.ttf';
$font_size = 12;
// Set the offset x and y for the text position
$offset_x = 0;
$offset_y = 20;
// Get the size of the text area
$dims = imagettfbbox($font_size, 0, $font, $text);
$text_width = $dims[4] - $dims[6] + $offset_x;
$text_height = $dims[3] - $dims[5] + $offset_y;
// Add text background
imagefilledrectangle($image_p, 0, 0, $text_width, $text_height, $bg_color);
// Add text
imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $text);
// Save the picture
imagejpeg($image_p, $public_file_path . '/output.jpg', 100);
// Clear
imagedestroy($image);
imagedestroy($image_p);
};
Here is output
Try this. It will help you…
<?php
$img = imagecreatefromjpeg("girl-hugging-the-globe.jpg"); // image.jpg is the image on which we are going to write text ,you can replace this iamge name with your
if(!$img) die("Unabe to load image");
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$width = 600;// it will store width of image
$height = 100; //it will store height of image
$fontsize = 6; // size of font
$text = "Block Prints Online"; // Define the text
$pos = ( $width - imagefontwidth($fontsize)*STRLEN($text) );// calculate the left position of the text:
imagestring($img, $fontsize, 200, 150, $text, $red);
header('Content-type: image/jpeg');
imagejpeg($img);//it will output a jpeg image
imagedestroy($img);//it will destroy $img*/
?>

Applying text to png transparency issue PHP

I am trying to write text onto a png, however when I do it puts a dark border around it, I am not sure why.
The original image:
The processed image:
Code:
// Load the image
$im = imagecreatefrompng("admin/public/images/map/order/wally.png");
// If there's an error, gtfo
if(!$im) {
die("");
}
$textColor = imagecolorallocate($im, 68, 68, 68);
$width = imagesx($im);
$height = imagesy($im);
$fontSize = 5;
$text = "AC";
// Calculate the left position of the text
$leftTextPos = ($width - imagefontwidth($fontSize)*strlen($text)) / 2;
// Write the string
imagestring($im, $fontSize, $leftTextPos, $height-28, $text, $textColor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
I've had this issue several times, let me find the answer...
Ok, found something:
imagesavealpha($im, true);
imagealphablending($im, true);
Write that before imagepng.
Yes, saving with alpha is important but loading it is important as well. Your PNG image might have transparency but it is good practice to account for that as well.
You'd need to create true color image, set alpha color and then draw your loaded image with text over it. So something like this:
// create true color image
$img = imagecreatetruecolor($width, $height);
$transparent_color = imagecolorallocatealpha($img, 255, 255, 255, 0);
imagealphablending($img, false);
imagefillrectangle($img, 0, 0, $width, $height, $transparent_color);
imagealphablending($img, true);
// draw previously loaded PNG image
imagecopy($img, $loaded_img, 0, 0, 0, 0, $width, $height);
// draw your text
// save the whole thing
imagesavealpha($img, true);
imagepng($img, $file);

Generate image thumbnails with watermarks on the fly in PHP

I'm looking for PHP class (solution) for generating image thumbnails with watermarks on the fly. Any idea ?
I've successfully used this code to add text to an (thumbnail) image:
(note that you'll need to provide a font)
function createImage($in_filename, $out_filename, $width, $height)
{
$src_img = ImageCreateFromJpeg($in_filename);
$old_x = ImageSX($src_img);
$old_y = ImageSY($src_img);
$dst_img = ImageCreateTrueColor($width, $height);
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $old_x, $old_y);
addWatermark($dst_img);
ImageJpeg($dst_img, $out_filename, 80);
ImageDestroy($dst_img);
ImageDestroy($src_img);
}
function addWatermark($image)
{
$text = "watermark text";
$font = realpath($_SERVER["DOCUMENT_ROOT"] . "/code/COURBD.TTF"); // case sensitive
if ($font == false) return;
$fontSize = 11;
$borderOffset = 4;
$dimensions = ImageTtfBBox($fontSize, 0, $font, $text . "#");
$lineWidth = ($dimensions[2] - $dimensions[0]);
$textX = (ImageSx($image) - $lineWidth) / 2;
$textY = $borderOffset - $dimensions[7];
$white = ImageColorAllocate($image, 240, 240, 240);
ImageTtfText($image, $fontSize, 0, $textX, $textY, $white, $font, $text);
}
Feedback welcome.
You can do this using using the imagecopyresampled() function. Heres a easy and clear tutorial of adding watermark to thumbnails . Also you can use imagettftext() function to use fonts as your watermark
Tutorial Link:
http://www.phpjabbers.com/phpexample.php?eid=20

Categories