pocobueno1388 Posted September 30, 2007 Share Posted September 30, 2007 I have a script that will upload an image, then create a thumbnail of that image. When I upload an image with a transparent background the normal sized image comes out with a transparent background, but the thumbnail image comes out with a black background. I am thinking I need the function imagecolortransparent(), but I'm not really sure how to incorporate it into the thumbnail generation code. <?php function uploadThumb($name, $filename, $new_w, $new_h, $ext) { if ($ext == 'jpg'){ $src_img = imagecreatefromjpeg($name); } else if ($ext == 'gif'){ $src_img = imagecreatefromgif($name); } else if ($ext == 'png'){ $src_img = imagecreatefrompng($name); } $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if ($ext == 'jpg'){ imagejpeg($dst_img,$filename); } else if ($ext == 'gif'){ imagegif($dst_img,$filename); } else if ($ext == 'png'){ imagepng($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); } ?> Any help with getting the thumbnail image to come out transparent, if the original picture is transparent, will be much appreciated =] Thanks Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/ Share on other sites More sharing options...
Barand Posted September 30, 2007 Share Posted September 30, 2007 Added a couple of lines (commented). Not tested. If it doesn't work we'll try plan B. <?php function uploadThumb($name, $filename, $new_w, $new_h, $ext) { if ($ext == 'jpg'){ $src_img = imagecreatefromjpeg($name); } else if ($ext == 'gif'){ $src_img = imagecreatefromgif($name); } else if ($ext == 'png'){ $src_img = imagecreatefrompng($name); } $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } $transparentColor = imagecolortransparent($src_img); // get transparent color in source $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); imagecolortransparent($dst_img, $transparentColor); // apply it to the dest image if ($ext == 'jpg'){ imagejpeg($dst_img,$filename); } else if ($ext == 'gif'){ imagegif($dst_img,$filename); } else if ($ext == 'png'){ imagepng($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-358712 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Author Share Posted October 1, 2007 Still giving me the black background. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-358731 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Author Share Posted October 1, 2007 Here is the updated code with Barands modifications in it: <?php function uploadThumb($name, $filename, $new_w, $new_h, $ext) { if ($ext == 'jpg'){ $src_img = imagecreatefromjpeg($name); } else if ($ext == 'gif'){ $src_img = imagecreatefromgif($name); } else if ($ext == 'png'){ $src_img = imagecreatefrompng($name); } $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } if ($thumb_w > $old_x && $thumb_h > $old_y){ $thumb_w = $old_x; $thumb_h = $old_y; } $transparentColor = imagecolortransparent($src_img); // get transparent color in source $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); imagecolortransparent($dst_img, $transparentColor); // apply it to the dest image if ($ext == 'jpg'){ imagejpeg($dst_img,$filename); } else if ($ext == 'gif'){ imagegif($dst_img,$filename); } else if ($ext == 'png'){ imagepng($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); } ?> Without Barands modifications: <?php <?php function uploadThumb($name, $filename, $new_w, $new_h, $ext) { if ($ext == 'jpg'){ $src_img = imagecreatefromjpeg($name); } else if ($ext == 'gif'){ $src_img = imagecreatefromgif($name); } else if ($ext == 'png'){ $src_img = imagecreatefrompng($name); } $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } if ($thumb_w > $old_x && $thumb_h > $old_y){ $thumb_w = $old_x; $thumb_h = $old_y; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if ($ext == 'jpg'){ imagejpeg($dst_img,$filename); } else if ($ext == 'gif'){ imagegif($dst_img,$filename); } else if ($ext == 'png'){ imagepng($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-359346 Share on other sites More sharing options...
BlueSkyIS Posted October 1, 2007 Share Posted October 1, 2007 Are you sure each of your output formats supports transparency as a format and/or at the bit-level exported? Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-359353 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Author Share Posted October 1, 2007 The images I have been testing with are GIF files. BEFORE they are resized, they are perfectly transparent...it's just when they are converted to thumbnails the parts that are supposed to be transparent come out black. Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-359368 Share on other sites More sharing options...
Barand Posted October 1, 2007 Share Posted October 1, 2007 does copying the palette help? <?php function uploadThumb($name, $filename, $new_w, $new_h, $ext) { if ($ext == 'jpg'){ $src_img = imagecreatefromjpeg($name); } else if ($ext == 'gif'){ $src_img = imagecreatefromgif($name); } else if ($ext == 'png'){ $src_img = imagecreatefrompng($name); } $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } if ($thumb_w > $old_x && $thumb_h > $old_y){ $thumb_w = $old_x; $thumb_h = $old_y; } $transparentColor = imagecolortransparent($src_img); // get transparent color in source $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); imagepalettecopy($dst_img, $src_img); // copy the pallette imagecolortransparent($dst_img, $transparentColor); // apply it to the dest image if ($ext == 'jpg'){ imagejpeg($dst_img,$filename); } else if ($ext == 'gif'){ imagegif($dst_img,$filename); } else if ($ext == 'png'){ imagepng($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-359376 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Author Share Posted October 1, 2007 does copying the palette help? Nope, still getting the same result :/ Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-359417 Share on other sites More sharing options...
Barand Posted October 1, 2007 Share Posted October 1, 2007 Plan B <?php function uploadThumb($name, $filename, $new_w, $new_h, $ext) { if ($ext == 'jpg'){ $src_img = imagecreatefromjpeg($name); } else if ($ext == 'gif'){ $src_img = imagecreatefromgif($name); } else if ($ext == 'png'){ $src_img = imagecreatefrompng($name); } $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); imagetruecolortopalette($dst_img,false,256); // convert to palletted image $color_00 = imagecolorat($dst_img,0,0); // get background color at 0,0 imagecolortransparent($dst_img, $color_00); // make it transparent if ($ext == 'jpg'){ imagejpeg($dst_img,$filename); } else if ($ext == 'gif'){ imagegif($dst_img,$filename); } else if ($ext == 'png'){ imagepng($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-359609 Share on other sites More sharing options...
marcus Posted October 1, 2007 Share Posted October 1, 2007 Try getting the transparent alpha channels. Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-359612 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Author Share Posted October 1, 2007 Plan B worked perfect Thanks Barand Quote Link to comment https://forums.phpfreaks.com/topic/71287-solved-make-thumbnail-transparent-help/#findComment-359616 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.