GIF transparent background turned to black - php

I have a script that resizes uploaded images. It works fine for PNGs and JPGs but with GIFs it renders the transparency in the resized GIF black.
$src = imagecreatefromgif($file);
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagegif($dst, $file);

From the manual page http://us3.php.net/manual/en/function.imagecopyresampled.php#104028
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
is used by one poster to preserve transparency.
Here's a tip... Always check out user comments on php.net as they are generally very helpful in understanding the nuances of a function and providing tips for handling common tasks.

I'm assuming this goes to a web-page, which if it does you could just output the image tag with the correct attributes?
echo "<img src='$file' width='$newWidth' height='$newHeight' />";

You still have to set the transparency in the new image. The following is taken from the php docs:
<?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);
// ^^ This is the command you are missing.
// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);
// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
In your case, you want to make the transparency the same color as the original transaprency - which I always made a hideous purple or something that would a) stick out like a sore thumb in my image manipulation software and secondly, have a RGB key that was almost impossible to have included in an image by mistake.

Related

Fill png transparency with background color

I'm refactoring an old image crop/resize library i wrote about 5 years ago and i'm stuck trying to restore one of it's functionalities. The funny part is that i'm not even sure it worked back then since i probably never actually used it.
I need to be able to work on png images while keeping transparency (which works), but i also wan't to be able to fill the transparent part of the image with a color.
Creating a blank image and filling it with a color works fine, but when i try to paste my png over it, the background is transparent again.
Here's a simplified version of my code:
<?php
$src = imagecreatefrompng($pathToSomePngFile);
imagealphablending($src, false);
imagesavealpha($src, true);
$output = imagecreatetruecolor($width, $height);
if ($backgroundColor) {
$fillColor = imagecolorallocate(
$output,
$backgroundColor['r'],
$backgroundColor['g'],
$backgroundColor['b']
);
imagefilledrectangle(
$output,
0,
0,
$width,
$height,
$fillColor
);
} else {
imagealphablending($output, false);
imagesavealpha($output, true);
}
imagecopyresampled(
$output,
$src,
0,
0,
0,
0,
$width,
$height,
$width,
$height
);
imagepng($output, $pathToWhereImageIsSaved);
UPDATE
Updated with delboy1978uk's solution to get it to work without changing my other settings.
Something like this should work.
<?php
// open original image
$img = imagecreatefrompng($originalTransparentImage);
$width = imagesx($img);
$height = imagesy($img);
// make a plain background with the dimensions
$background = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($background, 127, 127, 127); // grey background
imagefill($background, 0, 0, $color);
// place image on top of background
imagecopy($background, $img, 0, 0, 0, 0, $width, $height);
//save as png
imagepng($background, '/path/to/new.png', 0);

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

Copy one image on top of the next, while retaining transparency

I'm struggling with PHP's GDLib just a little bit here. I'm trying to overlay two .PNG images on top of eachother, which so far works fine.
The one problem I run into is that sometimes, the overlay image comes with a white background. I can make this transparent (using the imagecolortransparent function), but this transparency isn't saved when I copy this image onto a new one.
// Load the background image first
$background = imagecreatefrompng($this->background);
// Load the overlaying image next, and set white as a transparent color
$overlay = imagecreatefrompng($this->image);
imagecolortransparent($overlay, imagecolorallocate($overlay, 255, 255, 255));
// So far, this all works. But when I create a new image,
// and paste both $background and $overlay into it,
// $overlay loses transparency and reverts to a white fill.
$image = imagecreatetruecolor(16, 16);
imagesavealpha($image, true);
$trans_colour = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $trans_colour);
imagecopyresampled($image, $background, 0, 0, 0, 0, 16, 16, 16, 16);
imagecopyresampled($image, $overlay, 0, 0, 0, 0, 16, 16, 16, 16);
#mkdir(dirname($file), 0777, true);
imagepng($image, $file);
// The new $image is now mostly white. The transparency on $overlay
// was lost, meaning that the $background image is completely invisible.
How can I keep the transparency when copying $overlay into a new image?
$photo_to_paste = "photo_to_paste.png";
$white_image = "white_image.png";
$im = imagecreatefrompng($white_image);
$im2 = imagecreatefrompng($photo_to_paste);
imagecopy($im, $im2, (imagesx($im) / 2) - (imagesx($im2) / 2), (imagesy($im) / 2) - (imagesy($im2) / 2), 0, 0, imagesx($im2), imagesy($im2));
// Save alpha for the destination image.
imagesavealpha($im, true);
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);
// Save final image after placing one on another image
imagepng($im, "output.png", 0);
imagedestroy($im);
imagedestroy($im2);

Applying text to png transparency issue PHP

I am trying to write text onto a png, however when I do it puts a dark border around it, I am not sure why.
The original image:
The processed image:
Code:
// Load the image
$im = imagecreatefrompng("admin/public/images/map/order/wally.png");
// If there's an error, gtfo
if(!$im) {
die("");
}
$textColor = imagecolorallocate($im, 68, 68, 68);
$width = imagesx($im);
$height = imagesy($im);
$fontSize = 5;
$text = "AC";
// Calculate the left position of the text
$leftTextPos = ($width - imagefontwidth($fontSize)*strlen($text)) / 2;
// Write the string
imagestring($im, $fontSize, $leftTextPos, $height-28, $text, $textColor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
I've had this issue several times, let me find the answer...
Ok, found something:
imagesavealpha($im, true);
imagealphablending($im, true);
Write that before imagepng.
Yes, saving with alpha is important but loading it is important as well. Your PNG image might have transparency but it is good practice to account for that as well.
You'd need to create true color image, set alpha color and then draw your loaded image with text over it. So something like this:
// create true color image
$img = imagecreatetruecolor($width, $height);
$transparent_color = imagecolorallocatealpha($img, 255, 255, 255, 0);
imagealphablending($img, false);
imagefillrectangle($img, 0, 0, $width, $height, $transparent_color);
imagealphablending($img, true);
// draw previously loaded PNG image
imagecopy($img, $loaded_img, 0, 0, 0, 0, $width, $height);
// draw your text
// save the whole thing
imagesavealpha($img, true);
imagepng($img, $file);

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