The Little Guy Posted December 19, 2006 Share Posted December 19, 2006 OK... I have this as my code, it seems to work, but it doesn't make my PNG images have a transparent background, it just makes the background black.In the last 9 lines is where the PNG portion is located[code]<?php// The filesession_start();$img_id = $_GET['image_id'];include"db.php";$img = mysql_query("SELECT * FROM files where file_id='$img_id'")or die(mysql_query());$imgs = mysql_fetch_array($img);//$imgname = $_GET['image_id'];$filename = "users/".$_SESSION['user']."/$imgs[file_name]";#$filename = "users/ryan/screen2.jpg";//$filename = 'images/user_images/geoff.jpg';$percent = $_GET['percent'];switch($percent){case ".25"; $percent = .25;break;case ".50"; $percent = .50;break;case ".75"; $percent = .75;break;case "1"; $percent = 1;break;default: $percent = .5;break;}function getext($file) { $pos = strrpos($file,'.'); $str = substr($file, $pos); return strtolower($str);}// Content typeif(getext($filename)=='.jpg'){ header('Content-type: image/jpeg');}elseif(getext($filename)=='.gif'){ header('Content-type: image/gif');}elseif(getext($filename)=='.png'){ header('Content-type: image/png');}// Get new sizeslist($width, $height) = getimagesize($filename);$newwidth = $width * $percent;$newheight = $height * $percent;// Load$thumb = imagecreatetruecolor($newwidth, $newheight);if(getext($filename)=='.jpg'){ $source = imagecreatefromjpeg($filename);}elseif(getext($filename)=='.gif'){ $source = imagecreatefromgif($filename);}elseif(getext($filename)=='.png'){ $source = imagecreatefrompng($filename); imageAlphaBlending($source, true); imageSaveAlpha($source, true);}// Resizeimagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);// Outputimagejpeg($thumb);?>[/code]So how do I make it transparent? If I do this: [b]$source = imagecreatefrompng($filename); $source = imageAlphaBlending($source, true); $source = imageSaveAlpha($source, true);[/b]It doesn't even work. Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/ Share on other sites More sharing options...
monkey_05_06 Posted December 19, 2006 Share Posted December 19, 2006 If it's a PNG...why the hell are you using [b]imagejpeg[/b]:[code]// Outputimagejpeg($thumb);?>[/code] :o[b][EDIT:][/b]Wait...you're using imagejpeg for ALL your images? It won't work that way. You'll have to use imagejpeg for your JPEGs imagegif for your GIFs and imagepng for your PNGs. Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144302 Share on other sites More sharing options...
The Little Guy Posted December 19, 2006 Author Share Posted December 19, 2006 OK, I changed that line to this:[code]// Outputif(getext($filename)=='.jpg'){ imagejpeg($thumb);}elseif(getext($filename)=='.gif'){ imagegif($thumb);}elseif(getext($filename)=='.png'){ imagepng($thumb);}[/code]It still doesn't do anything useful, it is still non-transparentI'm using FF 2 btw.It worked just fine even with imgjpeg on gif, and png images. Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144304 Share on other sites More sharing options...
monkey_05_06 Posted December 19, 2006 Share Posted December 19, 2006 Hmmm...as for that...I don't know. Are you sending the proper headers?i.e.,[code]header("Content-type: image/" . (getext($filename) === ".jpg" ? "jpeg" : (getext($filename) === ".gif" ? "gif" : "png"))); // send the appropriate image header since you are printing the image's contents directly to the browser[/code]Notice I've used === instead of == I'm not sure why but you should use it instead. Also just make sure you're sending the headers (since you're printing the raw image data to the screen)...either that or write it to a file first and print out the image from the file using HTML tags.However it appears to me you're just trying to resize your images to make thumbnails. If this is the case, why not just set the WIDTH/HEIGHT attributes of your IMG tags? Unless you really want to just want/need a script that will make thumbnails of your images... Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144308 Share on other sites More sharing options...
The Little Guy Posted December 19, 2006 Author Share Posted December 19, 2006 I don't know it that matters, but anyways, I got the code from here: http://us2.php.net/manual/en/function.imagecopyresized.phpso basically it is the exact same way that PHP.net has it, only this has my own modifications. Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144311 Share on other sites More sharing options...
monkey_05_06 Posted December 19, 2006 Share Posted December 19, 2006 Well that makes sense of some of it. Sorry if I seemed harsh before...I just thought you had written the entire code snippet which would have made it a rather stupid mistake. ;)I've noticed that you are indeed sending the proper headers...my mistake for not taking the time to read over your code properly.However I'm fairly certain that using == won't work properly with strings. At least I think I read that somewhere. In any case, try using === instead. If nothing else it will make for better coding practice.Also, have you tried using [url=http://us2.php.net/manual/en/function.imagecopyresampled.php]imagecopyresampled[/url] instead? I don't know if it would make a difference, but it could be worth a try.P.S. you can make links by putting [ url=http://link.com ]link.com[ /url ] (without the spaces) ;) Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144327 Share on other sites More sharing options...
The Little Guy Posted December 20, 2006 Author Share Posted December 20, 2006 the === thing didn't do anything. Any other suggestions?Once again here is the code:[code=php:0]<?php// The filesession_start();$img_id = $_GET['image_id'];include"db.php";$img = mysql_query("SELECT * FROM files where file_id='$img_id'")or die(mysql_query());$imgs = mysql_fetch_array($img);//$imgname = $_GET['image_id'];$filename = "users/".$_SESSION['user']."/$imgs[file_name]";#$filename = "users/ryan/screen2.jpg";//$filename = 'images/user_images/geoff.jpg';$percent = $_GET['percent'];switch($percent){case ".25"; $percent = .25;break;case ".50"; $percent = .50;break;case ".75"; $percent = .75;break;case "1"; $percent = 1;break;default: $percent = .5;break;}function getext($file) { $pos = strrpos($file,'.'); $str = substr($file, $pos); return strtolower($str);}// Content typeif(getext($filename)=='.jpg'){ header('Content-type: image/jpeg');}elseif(getext($filename)=='.gif'){ header('Content-type: image/gif');}elseif(getext($filename)=='.png'){ header('Content-type: image/png');}// Get new sizeslist($width, $height) = getimagesize($filename);$newwidth = $width * $percent;$newheight = $height * $percent;// Load$thumb = imagecreatetruecolor($newwidth, $newheight);if(getext($filename)=='.jpg'){ $source = imagecreatefromjpeg($filename);}elseif(getext($filename)=='.gif'){ $source = imagecreatefromgif($filename);}elseif(getext($filename)=='.png'){ $source = imagecreatefrompng($filename); imageAlphaBlending($source, true); imageSaveAlpha($source, true);}// Resizeimagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);// Outputif(getext($filename)=='.jpg'){ imagejpeg($thumb);}elseif(getext($filename)=='.gif'){ imagegif($thumb);}elseif(getext($filename)=='.png'){ imagepng($thumb);}?>[/code] Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144932 Share on other sites More sharing options...
monkey_05_06 Posted December 20, 2006 Share Posted December 20, 2006 Just to see what's going on...why not throw a die in there somewhere...i.e.:[code]if (getext($filename) === ".jpg") die("JPEG");else if (getext($filename) === ".gif") die("GIF");else if (getext($filename) === ".png") die("PNG");[/code]I'm telling you...you really need to use triple equal signs here. You said it didn't make a difference, but you should still change it. Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144941 Share on other sites More sharing options...
The Little Guy Posted December 20, 2006 Author Share Posted December 20, 2006 This is my alt text in the img tag when I run yours:The image “http://71.82.126.113:6251/tzfiles/resize.php?image_id=43” cannot be displayed, because it contains errors.When I run mine, It outputs the image, only it has a black background instead of a transparent one. Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144956 Share on other sites More sharing options...
The Little Guy Posted December 20, 2006 Author Share Posted December 20, 2006 anyone help? Link to comment https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-145280 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.