Everywhere is talking about adding text on transparent image. I have successfully added text like watermark (so mean transparent text). Can you suggest me how to give opacity 50% ?
Here is code -
$text = "GIF-KING";
$font = "arial.ttf";
$image = imagecreatefromgif('tmpimg/myimage.gif');
$text_color = imagecolorallocate($image, 198, 60, 147);
//imagestring($image,36, 10, 20, $text, $text_color);
imagettftext($image, 30, 0,10, 290, $text_color, $font, $text);
You'd need imagecolorallocatealpha instead of imagecolorallocate,
alpha blending is not available 100%, so take care of error messages.
imagecolorallocatealpha() behaves identically to imagecolorallocate()
with the addition of the transparency parameter alpha.
http://php.net/imagecolorallocatealpha
Related
Screenshot of problem:
I'm trying to get the same font quality such as Font Squirrel's sample fonts widget, but the font keeps coming out rough. It's smooth in Photoshop. Note: the "The lazy dog" part isn't being bolded by me, its doing it by itself.
Here's the PHP:
<?php
putenv('GDFONTPATH=' . realpath('.'));
$font = $_GET['font'] . '.ttf';
$text = 'The Quick Brown Fox Jumps over the Lazy Dog';
// Create the image
function imageCreateTransparent($x, $y) {
$imageOut = imagecreate($x, $y);
$colourBlack = imagecolorallocate($imageOut, 0, 0, 0);
imagecolortransparent($imageOut, $colourBlack);
return $imageOut;
}
$image = imageCreateTransparent(600, 800);
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$grey = imagecolorallocate($image, 128, 128, 128);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 399, 29, $white);
// Add the text
imagettftext($image, 20, 0, 10, 20, $black, $font, $text);
imagepng($image);
imagealphablending($image, true);
imagedestroy($image);
?>
HTML: <img src="fontgen.php?font=Aller_Rg" alt="" />
How can I get that high quality result for the font?
You've only set part of the background to be white, the rest of it is transparent.
When the font is drawn over a white background, the black text is anti-aliased so that it looks smooth, which results in the pixels around the font being drawn as a blend between the two colours, which also makes the font look smaller.
On the right hand side there is no background colour so the anti-aliasing is not working properly. Instead of blending between the font colour and the background colour, the drawing algorithm is using the original font colour for any pixel that is even partly covered by a letter.
This makes the letters look 'bold' as the edge pixels are now black, instead of shades of grey.
The way to fix this properly is to use an image that has a proper background colour, even if that background colour is transparent. This makes the image library use a proper alpha-channel (which is the only sensible way of doing alpha blending) rather than using an indexed based alpha, where only one 'colour' is transparent and all the others are fully opaque.
$font = '../../fonts/Aller_Rg.ttf';
$text = 'The Quick Brown Fox Jumps over the Lazy Dog';
// Create the image
function imageCreateTransparent($x, $y) {
$imageOut = imagecreatetruecolor($x, $y);
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
return $imageOut;
}
$image = imageCreateTransparent(600, 800);
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$grey = imagecolorallocate($image, 128, 128, 128);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 399, 29, $white);
//// Add the text
imagettftext($image, 20, 0, 10, 20, $black, $font, $text);
//imagealphablending($image, true); //not needed as we created the image with alpha
imagesavealpha($image, true);
//imagepng($image, '../../var/log/wtf5.png');
imagepng($image);
imagedestroy($image);
This will make the font size be right as the anti-aliasing will work correctly* and the image will be transparent where appropriate e.g. the image created with the code above, shown over a red background.
The bits of the image that have a white background are white, the bits of the image that are transparent let the red colour come through, and the text is anti-aliased correctly to both.
*assuming that you want to anti-alias to what the background colour was set to, which is not always the case but probably is here.
I suspect it's because you're making the background explicitly white only in the left 400 px. To the right of that it's probably still transparent and has some side-effects. The r is the first letter that starts beyond the white background you created earlier.
I want to do the following in PHP in combination with GD. ImageMagick is not an option, unfortunately, but this seems like such a common problem that there has to be a solution, I just can't seem to find it.
I want to create a PNG with a transparent background. Then I want to draw a rectangle on it, copy an image on it, and add some text. One way of doing this is as follows:
$image = ImageCreateTrueColor (800, 600);
imagecolortransparent ($image, 0); //0 is pure black, the default fill color
imagerectangle (...);
//code to copy an image
imagettftext ($image, ...);
imagepng ($image);
This works fine, except that part of the copied image might be black, and/or the text might be black. This then also becomes transparent, which is something I don't want.
imagefill ($image, 0,0, 0x7FFF0000);
imagetransparent ($image, 0x7FFF0000);
The above code is something I found online, which fills it with red, then makes red transparent. Again, this causes all red in the image to become transparent. I could choose a color that is unlikely to occur, but I can't guarantee this.
Is there something I'm missing? Can this be fixed?
Thanks for your replies!
imagecolortransparent is probably not what you want here if you're merging images, as single-colour transparency is nasty.
Instead, try it with a transparent fill mask like so:
<?php
$image = imagecreatetruecolor(100, 100);
// Transparent Background
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);
// Drawing over
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 25, 25, 75, 75, $black);
header('Content-Type: image/png');
imagepng($image);
I need some help about PHP GD. Here is my piece of code.
header("Content-type: image/gif");
$image = imagecreatetruecolor(550, 20);
imagealphablending($image, false);
$col=imagecolorallocatealpha($image,255,255,255,127);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image,0,0,550,20,$col);
imagealphablending($image, true);
$font_path = 'font/arial.ttf';
imagettftext($image, 9, 0, 16, 13, $black, $font_path, $lastlisten);
imagesavealpha($image, true);
imagepng($image);
The problem is when I use imagepng, it can show the png just fine like this.
But if I use imagegif instead, it will become this.
I did tried using different header for gif and png. The result for imagegif is still the same.
The question is how do I make in order to display GIF version properly? Thanks you
GIF image supports a maximum of 256 colors. Most importantly, it only supports index transparency: a pixel can be 100% opaque or 100% transparent.
PNG on the other hand supports true (millions of) color images and supports alpha channel transparency. That means a pixel can be 100% opaque, 100% transparent or anything in between.
The PNG image you mentioned probably has its edges partially transparent therefore the browser can easily blend those pixels with the background color giving a smooth effect. PNG is a better choice.
First problem : your characters are ugly: that's because you need to set a palette with less colors when using imagecreatetruecolor.
$image = imagecreatetruecolor(550, 20);
imagetruecolortopalette($image, true, 256);
should solve this problem.
Second problem : there is no transparency.
As you can see on PHP manual,
imagesavealpha() sets the flag to attempt to save full alpha channel
information (as opposed to single-color transparency) when saving PNG
images.
This function does not work with GIF files.
You can use imagecolortransparent instead but this will not be perfect because fonts has anti-aliasing to make their border sweeter.
Here is my code:
<?php
$lastlisten = "test test test test test test";
error_reporting(E_ALL);
header("Content-type: image/gif");
$image = imagecreatetruecolor(550, 20);
imagetruecolortopalette($image, true, 256);
$transparent=imagecolorallocatealpha($image,255,255,255,127);
imagecolortransparent( $image, $transparent);
imagefilledrectangle($image,0,0,550,20,$transparent);
$black = imagecolorallocate($image, 0, 0, 0);
$font_path = dirname(__FILE__) . '/font.ttf';
imagettftext($image, 9, 0, 16, 13, $black, $font_path, $lastlisten);
imagegif($image);
Result here
Hope this helps.
I have written a code for merge two images.
My code is:
$rnd = rand("99000", "99999");
$dst_path = "/home/maxioutl/public_html/images/urunler/";
$dst_file_name = "tresim-{$rnd}.jpg";
$dst_file = $dst_path.$dst_file_name;
$dst = imagecreatetruecolor(250, 375);
imagefill($dst, 0, 0, imagecolorallocate($dst, 255, 255, 255));
$src = imagecreatefromjpeg("http://www.goldstore.com.tr/upload/product/raw/3.72925.0332.JPG");
imagecopymerge($dst, $src, 40, 60, 0, 0, 250, 375, 100);
imagejpeg($dst, $dst_file, 90);
Result:
Black background. Where is it?
It's the imagecopymerge($dst, $src, 40, 60, 0, 0, 250, 375, 100); statement that's doing it.
You're passing it the dimensions 250x375, which isn't the watch's actual dimensions. Therefore the merge bounding box continues on and it uses black. You can see this easily if you comment it out because you'll get your white square from the fill like you were expecting.
you need to get the exact dimensions of your watch graphic (i.e. through getimagesize) and pass those to imagecopymerge so it cuts it exactly when it merges.
$arrSize = getimagesize($originalFile);
imagecopymerge($dst, $src, 40, 60, 0, 0, $arrSize[0], $arrSize[1], 100);
The code you wrote superimposes the images. It does not remove the white pixels from the image with the watch.
They are basically overlapped.
You should cycle through both image dimensions and replace each *white pixel with a black pixel or a transparent pixel.
*white pixel may not necessarily mean rgb(255,255,255) you can choose to treat all pixels with rgb(>235,>235,>235) as being "white".
Function imagecreatetruecolor creates completely black image
Taken from here :
imagecreatetruecolor() returns an image identifier representing a
black image of the specified size.
I'm looking for the way to add inner shadow to text using PHP. I'm not looking for a solution, which includes HTML and/or CSS. Image must be generated using only PHP.
For examle: http://i.imgur.com/jYGvM.png
From the text above ('Original') I want to create modify text, so it will look like text at the bottom ('Shadow').
Effect must be achieved using only GD library or Imagemagick, because I can't install new libraries to the server.
One way is to draw your text twice with different colors and a small offset.
Below is the sample code, mainly taken from the php manual, with some modifications to let the shadow appear. The resulting image is here.
Code:
<?php
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($im, 0x55, 0x55, 0x55);
$gray2 = imagecolorallocate($im, 0xDD, 0xDD, 0xDD);
// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $gray2);
// Path to our ttf font file
$font_file = './ariblk.ttf';
// the text without shadow
imagefttext($im, 40, 0, 10, 45, $white, $font_file, 'Original');
// the shadow for "Shadow"
imagefttext($im, 40, 0, 10, 89, $gray, $font_file, 'Shadow');
// and the word itself
imagefttext($im, 40, 0, 10, 90, $white, $font_file, 'Shadow');
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>