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
Related
I've tried a few times but I can't seem to get 2 centered lines of text at the bottom of this image on the transparent background
Any suggestions?
<?php
$filePath = "adopt.png"; //full path to your png, including filename and extension
$img = #imagecreatefrompng($filePath);
$width = imagesx($img);
$height = imagesy($img);
//create new image and fill with background color
$backgroundImg = #imagecreatetruecolor($width, $height);
imagecopy($backgroundImg, $img, 0, 0, 0, 0, 100, 130);
$color = imagecolorallocatealpha($backgroundImg, 0, 0, 0, 127); //fill transparent back
imagefill($backgroundImg, 0, 0, $color);
//save as png
header( "Content-type: image/png" );
imagepng( $backgroundImg );
imagedestroy( $backgroundImg );
?>
This process works. Without your code I can only regurgitate the example from the manual to you. http://php.net/manual/en/function.imagestring.php. But it works, I have used the imagestring function. It accepts co-ordinates for the text so I can not see why it would not work for you.
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
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*/
?>
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.
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);
?>
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