Png with alpha background - php

I'm trying to create a URL, that spits back a 1x1px png with a certain alpha value e.g. www.mysite/com/png.php?alpha=50 which will output a 1x1 png, with the background colour set to black, and having the opacity at 50%.
I've searched through lots of tutorials and posts trying to figure out how to do this, but I can't find anything that works. Is this possible to do with PHP alone?
Here's the closest I've gotten
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(500, 300);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocatealpha($im, 255, 255, 255, 50);
imagefilledrectangle($im, 0, 0, 500, 300, $black);
// Save the image
imagepng($im);
imagedestroy($im);
?>
However, that seems to overlay a 50% black colour on top of a white background.

Set the white to transparent, by adding these lines after $black = ...
$colourWhite = imagecolorallocate($im,255,255,255);
imagecolortransparent($im,$colourWhite);

Related

php gd is returning black background and the image creator function return images in the center of the page

I am working with PHP-GD.When i try to create image using "imagecreatetruecolor" function in PHP the image is generated.But the main problem here is the image thus generated has a black background as specified in the code but the rest of the whole page also turns grey and the image is displayed at the center of the window regardless of the attributes given in the imagecreatetruecolor function.I have been working since four months on PHP-GD it never happened to me. But form the last week its behaving awkward.Please help me to solve.
<?php
header("Content-type: image/png");
$img = imagecreatetruecolor(400, 400);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
imagefilledellipse($img,100,100,10,10,$green);
imagefilledellipse($img,200,200,10,10,$green);
imagepng($img);
imagedestroy($img);
?>
This is the result of Chrome and not GD. The latest version of Chrome centers the image in your window and gives the rest of the window a black background.
If you want to see this in actions, add the following code to give your image a white background:
imagefill($img, 0, 0, $white);
Which will give you the following:

I'm trying to make a image transparent using GD library from PHP but running following code, only a portion will be transparent

I'm trying to make a image transparent using GD library from PHP but running following code, only a portion will be transparent.
$image = imagecreatefrompng("$second");
imagealphablending($image, false);
$col_transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $col_transparent); // set the transparent colour as the background.
imagecolortransparent ($image, $col_transparent); // actually make it transparent
imagesavealpha($image, TRUE);
header( 'Content-Type: image/png' );
imagepng($image);
Here you have original image: https://postimg.org/image/y68nw57z1/
Here it's the resulting image: https://postimg.org/image/o4n3t6ic7/
As you can see, exists parts from the resulting image that remain white.
How i can resolve this?
You are flood-fill replacing the white pixels of your image but that won't work on pixels that are completely enclosed by non-white pixels (as in any paint program). Instead you can modify the definition of the colour white to make it transparent:
$image = imagecreatefrompng($second);
imagetruecolortopalette($image, false, 255);
$index = imagecolorclosest($image, 255, 255, 255); // find index of white.
imagecolorset($image, $index, 0, 0, 0, 127); // replace white with transparent black.
header('Content-Type: image/png');
imagepng($image);

PHP remove a section of image leaving a transparent space

I have an transparent image with a bar through the middle...
http://i66.tinypic.com/2zyeg4h.png
Using php I would like to remove the middle part on the bar, leaving a transparent space in the middle of the bar. I try this code...
$im = imagecreatefrompng('****root****/image.png');
//make a yellow box
$transparent = imagecolorallocate($im, 255, 255, 0);
//make the yellow box transparent
imagecolortransparent($im, $transparent);
imagefilledrectangle($im, 200, 115, 300, 137, $transparent);
imagealphablending($im, false);
imagesavealpha($im, true);
header ('Content-Type: image/png');
imagepng($im);
$save = "****root****/test.png";
imagepng($im, $save);
imagedestroy($im);
?>
But the output image has the rectangle retain the yellow colour, rather than turn it transparent.
http://i67.tinypic.com/1ny6j4.png
Where am I going wrong? If I remove...
imagealphablending($im, false);
imagesavealpha($im, true);
I get the transparent box in the center (in the browser), but the rest of the image loses it's transparency and has a white background instead, also when I download that image and open in a photo editor, the transparency box in the center is gone. I have GD libraries set up on my server.
You need to use the gd function imagecolorallocatealpha to create a transparent color.
You could generate the whole image with PHP.
e.g.
<?php
// generate empty image
$image = imagecreate(500, 262);
// define black
$black = imagecolorallocate($image, 0, 0, 0);
// create left part of the bar
imagefilledrectangle($image, 0, 110, 200, 162, $black);
// create right part of the bar
imagefilledrectangle($image, 300, 110, 500, 162, $black);
// save image
imagepng($image, 'outimage.png');
If you want to stick with an existing image
<?php
// generate empty image
$image = imagecreatefrompng('input.png');
// define transparent
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
// create the transparent area
imagefilledrectangle($image, 200, 110, 300, 162, $transparent);
// save image
imagepng($image, 'output.png');
input.png
output.png
You don't need to send a header if you're not submitting the image as a result to a browser.
e.g.
header('Content-Type: image/png');
imagepng($image);
Would display the image in the requesting browser rather than saving it to a file.

transparent image in php

I am executing my following code for creating a transparent image but everytime it shows me black background.
kindly tell me my my fault in the code.
<?php
//set the content type
header('Content-type: image/jpeg');
//create the image
$im = imagecreatetruecolor(250, 200);
$black = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);
// Make the background transparent
imagecolortransparent($im, $black);
//text to draw
$text=$_POST['text'];
//font path
$font = '/usr/share/fonts/truetype/droid/DroidSans.ttf';
// Add the text
imagettftext($im, 15, 0, 50, 50, -$blue, $font, $text);
//view the image
imagejpeg($im);
imagedestroy($im);
?>
You cannot make jpeg images transparent. Use png instead
Change below 2 lines:
header('Content-type: image/png');
imagepng($im);
Update
Reference Link: create transparent png image

PNG transparency issue

I have the following problem.
I have a PNG file (http://meteopage.com/beta/image.png), which is already partially transparent and I would like to make the "pink" border also transparent.
I have read some of the instructions here and came up with a code, which does replace the border with transparent point, but all the rest, which was already transparent, turns into black: http://meteopage.com/beta/radar2.php
My code looks like this:
<?php
$file="image.png";
$im = imagecreatefrompng($file);
imagealphablending($im, false);
$new = imagecolorclosest($im, 255, 0, 255);
imagecolortransparent($im, $new);
imagesavealpha($im, true);
header('Content-type: image/png');
imagepng($im);
?>
I tried adding those imagesavealpha and imagealphablending commands, but still no luck, it is black as you can see in the link Ive attached. Would anyone know how to maintain the original transparency and just "add" those pink pixels to it?
Your image is palette-based, not true colour, so the calls to imagealphablending() and imagesavealpha() aren't needed.
The simplest way to do this is to replace the pink colour in the palette-index with a transparent one, like so:
$file = 'image.png';
$im = imagecreatefrompng($file);
$new = imagecolorclosest($im, 255, 0, 255);
imagecolorset($im, $new, 255, 0, 255, 127);
header('Content-type: image/png');
imagepng($im);
Note that this requires PHP >= 5.4 otherwise imagecolorset() won't accept the alpha parameter.

Categories