John6854654654 Posted June 16, 2009 Share Posted June 16, 2009 I am using a script to manipulate images. Sometimes, a user will upload a transparent image. I want to make the transparent part of the image white instead of transparent. I cant find a single tutorial on how to fill in transparency, only on how to keep it. Without doing anything, it makes the transparency black. How can i tell it to make it white instead? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 16, 2009 Share Posted June 16, 2009 Why not merge the original image with a white image of same size? Quote Link to comment Share on other sites More sharing options...
John6854654654 Posted June 16, 2009 Author Share Posted June 16, 2009 Good call, but it wouldn't work for my situation. Heres why: I am already merging this image with another image. I am then sending it to TCPDF to be converted to a PDF. This script does not like transparent backgrounds, so it makes them black. The way i am doing it in TCPDF, i cannot change it to white. This is why i want to change it to white in PHP first. So, merging it with a white background would still result in black within TCPDF because somehow it still sees the image as a transparent image on top of a another image. If it didnt see it like this, i wouldnt have a problem because the background would be the one i merged it with in the first place. But, since it can somehow still see the transparency and know to change it to black, i need to somehow make it white first. Thanks! Quote Link to comment Share on other sites More sharing options...
.josh Posted June 16, 2009 Share Posted June 16, 2009 what about making it 254,254,254 instead of full white? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 16, 2009 Share Posted June 16, 2009 I don't get how the script knows that part of the image used to be transparent. Doesn't make much sense, 'cause you can't 'dismerge' an image, right? I guess you should post some code, so we can have a proper look at it. what about making it 254,254,254 instead of full white? I don't see how that would solve the problem he's describing (that is, if he's right about why this happens). Quote Link to comment Share on other sites More sharing options...
.josh Posted June 16, 2009 Share Posted June 16, 2009 well I'm just sort of assuming that this TCPDF thing he has might try to "autodetect" backgrounds. I mean, what other explanation is there for a white background suddenly going black? Quote Link to comment Share on other sites More sharing options...
John6854654654 Posted June 16, 2009 Author Share Posted June 16, 2009 Ok ill post the code that handles the image upload before it gets sent to TCPDF for conversion into a PDF. <?php $file_realname = trim($_FILES['imgfile']['name']); // This is the temporary file created by PHP $uploadedfile = $_FILES['imgfile']['tmp_name']; $uploadedfile_type = $_FILES['imgfile']['type']; // Create an Image from it so we can do the resize if($uploadedfile_type == "image/gif"){$src = imagecreatefromgif($uploadedfile);} else if ($uploadedfile_type == "image/jpg"){$src = imagecreatefromjpeg($uploadedfile);} else if ($uploadedfile_type == "image/jpeg"){$src = imagecreatefromjpeg($uploadedfile);} else if ($uploadedfile_type == "image/png"){$src = imagecreatefrompng($uploadedfile);} else{ //Not JPG, JPEG, GIF, or PNG! header("Location: ./../../index.php?section=2&flyerid=$flyerid&fname=$fname&logo=invalid&preview=false"); } // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); $max_height = 63; $max_width = 71; if($width >= $height){ $newwidth = $max_width; $newheight = $height * ($max_width / $width); $bgwidth = 0; $bgheight = $newheight; } else{ $newwidth = $width * ($max_height / $height); $newheight = $max_height; $bgwidth = $newwidth; $bgheight = 0; } $tmp=imagecreatetruecolor($max_width,$max_height); // Resize image and preserve alpha (transparency) // imagecolortransparent($tmp, imagecolorallocate($tmp, 0, 0, 0)); // imagealphablending($tmp, false); // imagesavealpha($tmp, true); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); //Merge background to image for the picky PDF converter $dest = imagecreatefromgif("images/logo.gif"); imagecopy($tmp, $dest, $bgwidth, $bgheight, $bgwidth, $bgheight, $max_width, $max_height); // Now write the resized image to the server. If its a GIF or a PNG, make it a PNG to keep transparency and quality $filename = "pages/logos/". $_FILES['imgfile']['name']; if($uploadedfile_type == "image/gif"){imagegif($tmp,$filename);} else if ($uploadedfile_type == "image/jpg"){imagejpeg($tmp,$filename);} else if ($uploadedfile_type == "image/jpeg"){imagejpeg($tmp,$filename);} else if ($uploadedfile_type == "image/png"){imagepng($tmp,$filename);} imagedestroy($dest); imagedestroy($src); imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request has completed. ?> So, as you can see, i have my transparency code in there commented out. This is because i am trying to fill in the background now. If you allow that transparency code to be executed again, it seems like it would work perfectly. However, when sent to TCPDF, the transparent image gets a background. PNG's turn white, GIF's turn black. So the obvious solution is to convery GIFs to PNGs. But for some reason, the TCPDF script fails completely when i try and convert the image. This script is really killing me, but im doing be best to get around its flaws. And as far as changing the value to 254 instead of 255, where could i do that? It sounds like your assuming i know how to make the background white in the first place? And if you are referring to merging again, that wont work because somehow TCPDF knows that the image is transparent on top of another image. So frustrating! But thank you! Quote Link to comment Share on other sites More sharing options...
Alex Posted June 16, 2009 Share Posted June 16, 2009 I think he meant merging an off-white image instead of a completely white one. Either way it's impossible for it to detect that it was transparent previously. It must be something else. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 16, 2009 Share Posted June 16, 2009 I think you're doing it wrong. Where are you setting the white color? imagecreatetruecolor() creates a black image by default. Try: $tmp=imagecreatetruecolor($max_width,$max_height); $white = imagecolorallocate($tmp, 255, 255, 255); imagefill($tmp, 0, 0, $white); And what's images/logo.gif? Edit: Either way it's impossible for it to detect that it was transparent previously. It must be something else. Exactly. After the file has been saved to the server, there's no possible way that the PDF script can differentiate the original 'layers'. Quote Link to comment Share on other sites More sharing options...
John6854654654 Posted June 16, 2009 Author Share Posted June 16, 2009 I think you're doing it wrong. Where are you setting the white color? imagecreatetruecolor() creates a black image by default. Try: $tmp=imagecreatetruecolor($max_width,$max_height); $white = imagecolorallocate($tmp, 255, 255, 255); imagefill($tmp, 0, 0, $white); And what's images/logo.gif? Edit: Either way it's impossible for it to detect that it was transparent previously. It must be something else. Exactly. After the file has been saved to the server, there's no possible way that the PDF script can differentiate the original 'layers'. Yes! That did it, i was doing it wrong. Thanks! The fact that you asked what the logo.gif was means that i did a terrible job of explaining haha. You see, before that fix you sent me, i was putting logo.gif behind the transparent uploaded image by merging them together. TCPDF was somehow still seeing the transparent image as transparent even though looking at the image in my browser before turning it into a pdf resulted in what i wanted. TCPDF still found a way to recognize that the image was transparent and make the background black. But, now, thanks to you guys, i made it white instead and it works well. Not perfectly, but as well as it can while conforming to TCPDF's weird standards. I love the tool, but its hard to use. Thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.