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.
Related
I have GD library installed as you can see.(image)
but when i try to create a image it shows special characters instead of the image.(image)
I'm using laravel and Xampp.
Anyone Know what is the problem ?
<?php
// create a blank image
$image = imagecreatetruecolor(400, 300);
// fill the background color
$bg = imagecolorallocate($image, 0, 0, 0);
// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);
// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);
// output the picture
header("Content-type: image/png");
imagepng($image); ?>
If you want your image to show as an image, you need to remove any output before sending a header. Remove var_dump(gd_info()); from your code to see the actual image:
<?php
// create a blank image
$image = imagecreatetruecolor(400, 300);
// fill the background color
$bg = imagecolorallocate($image, 0, 0, 0);
// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, 255, 255, 255);
// draw the white ellipse
imagefilledellipse($image, 200, 150, 300, 200, $col_ellipse);
// output the picture
header("Content-type: image/png");
imagepng($image); ?>
From https://www.php.net/manual/en/function.header.php:
"Remember that header() must be called before any actual output is sent"
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);
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
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);
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.