I can't use transparent background with imagecopymerge - php

I am calling imagecopymerge($dst_r, $logo, 0, 0, 0, 0, $LogoX, $LogoY, 100); where $logo is a png file with transparent background. From some reason the background comes out white instead.
What am I doing wrong?
Thanks.

You need to use imagealphablending($dst_r, TRUE); to allow copying with retaining the transparent colors. Many more comments (...) in the manual suggest using imagecopy instead, because imagecopymerge was never intended to be used with transparency. If you use pct=100 anyway, then the normal imagecopy might be an option.

This is for text, but you can get the point. It would be more helpful if you post entire code.
$font = 25;
$string = "Hello";
$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, "font.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

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);

Clickable links imagecreatefrompng

At this moment i'm trying to create a dynamic PHP image, and i'm not really sure if it is even possible to have clickable links in the following piece of code:
<?php
$image = "hotelview_val13.png";
$src = 'pixel.png';
putenv('GDFONTPATH=' . realpath('.'));
$font = 'font.ttf'; //Ubuntu font
$im = imagecreatefrompng($image);
imagealphablending($im, true);
imagesavealpha($im, true);
imagealphablending($src_to_copy, true);
imagesavealpha($src_to_copy, true);
imagealphablending($pg, true);
imagesavealpha($pg, true);
$wc = ImageColorAllocate ($im, 255, 255, 255);
$red = ImageColorAllocate ($im, 255, 0, 0);
$blk = imagecolorallocate($im, 0, 0, 0);
{
imagettftext($im, 12, 0, 45, 310, $blk, $font , "Link");
imagettftext($im, 12, 0, 45, 330, $blk, $font , "Veel plezier ;)");
}
header("Content-Type: image/png");
Imagepng($im);
ImageDestroy ($im);
?>
What i'm trying to do is to make a clickable link. I've tried to just simply put into the code, but that doesn't work because it displays the code as plain text. Is it possible? If it is, how does it work?
Thanks a lot for your time.
You cannot embed links in an image. The only way to make a region of an image clickable is, when viewed in a browser, to use an HTML map.

Fully looking text when using PHP and imagettftext and angle

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.

Image Overlay in PHP; Black Background?

When I perform and image overlay in php using the GD library, I always get a black background, however, all the images overlay correctly. Can someone help?
<?php
$images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );
$img = imagecreatetruecolor(58, 75);
imagealphablending($img, true);
imagesavealpha($img, true);
imagecolorallocate($img, 255, 205, 255);
imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white);
foreach($images as $fn) {
$cur = imagecreatefrompng($fn);
imagealphablending($cur, true);
imagesavealpha($cur, true);
imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);
imagedestroy($cur);
}
header('Content-Type: image/png');
imagepng($img);
?>
// Create an image
$img = imagecreatetruecolor($imgWidth, $imgHeight);
$white = imagecolorallocate($img, 255, 255, 255);
// Make the background white
imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white);
...could help.
This is a common problem, and the answer is already available on stack overflow; the answer there fixes the problem perfectly. You may want to try searching harder :)
I would suggest that you can make your life easier by using the vastly more powerful (but unfortunately poorly documented) imagick library if you're going to try to do anything more than the most basic image manipulation; it's faster, easier (again, once you get past the documentation) and more powerful.

imagepng() and transparency in GD library with PHP

When using the function imagepng() in PHP, how can I make sure the images that I save are saved with a transparent background?
Simply do this:
imagealphablending($img, false);
imagesavealpha($img, true);
Before outputting. Make sure that all source files (if you used any) are set to PNG 32-bit with transparency - if not the output may differ with black background or transparency does not comply.
Here is the example
$newimage = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
$transparentindex = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
imagefill($newimage, 0, 0, $transparentindex);
Here is an example of the imagecolortransparent function (if it helps):
<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);
// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
There's a function called imagecolortransparent that allows you to set which color is made transparent. I don't know if this answers your question.

Categories