Image Overlay in PHP; Black Background? - php

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.

Related

How to make an Image to show only black text

I am trying to do the following:
I Have a picture I want to make that only text which is written in black remains visible and rest of every colour gets transparent.
I tried to do this using PHP imagecolortransparent function but I am not able to figure out how to make it work.
Any help will be great.
Thanks In advance
Updated Answer
There must be an easier way, but until I think of it, this should work:
// Read original image
$start = imagecreatefrompng("input.png");
// Make true colour image same size
$im = imagecreatetruecolor(imagesx($start),imagesy($start));
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
$red=imagecolorallocate($im,255,0,0);
imagesavealpha($im, TRUE);
// Fill the new image with transparency
imagefill($im, 0, 0, $transparent);
// Copy only black pixels across from original to new
for($x=0;$x<imagesx($im);$x++){
for($y=0;$y<imagesy($im);$y++){
$rgb = imagecolorat($start,$x,$y);
if($rgb==0){
imagesetpixel($im,$x,$y,$black);
}
}
}
imagepng($im,"result.png");
Original Answer
I would do this:
// Read original image
$start = imagecreatefrompng("input.png");
// Create a proper truecolour image the same size that can support transparency
$im = imagecreatetruecolor(imagesx($start),imagesy($start));
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, TRUE);
$black = imagecolorallocate($im, 0, 0, 0);
$text="This is some quite black text";
imagestring($im,5,0,75,$text,$black);
imagepng($im,"result.png");
input.png
result.png

Draw single(!) pixel with php

Can someone help me to draw single pixel using php GD library?
I have such code:
$img = imagecreatetruecolor(100, 100);
$c = imagecolorallocate($img, 255, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);
imagesetpixel($img, 0, 0, $c);
imagejpeg($img, 'created.jpg');die();
But the output is really weird (it is approximate image in top-left corner):
Is it possible to draw just one red pixel at x-0 y-0 position?

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.

I can't use transparent background with imagecopymerge

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

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