SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation GD2 + getting PNG's background to be transparent

    I have a function I use to resize images (jpg/png), but if I upload a png with a transparent background, it turns the background black. I don't know much about GD2 and my googling efforts haven't produced any results. Here is the function:

    PHP Code:
    function resizeimage($forcedwidth$forcedheight$sourcefile$destfile$file_ext) {
        
    $fw $forcedwidth;
        
    $fh $forcedheight;
        
    $is getimagesize$sourcefile );
        if( 
    $is[0] >= $is[1] ) {
            
    $orientation 0;
        } else {
            
    $orientation 1;
            
    $fw $forcedheight;
            
    $fh $forcedwidth;
        }
        if ( 
    $is[0] > $fw || $is[1] > $fh ) {
            if( ( 
    $is[0] - $fw ) >= ( $is[1] - $fh ) ) {
                
    $iw $fw;
                
    $ih = ( $fw $is[0] ) * $is[1];
            } else {
                
    $ih $fh;
                
    $iw = ( $ih $is[1] ) * $is[0];
            }
            
    $t 1;
        } else {
            
    $iw $is[0];
            
    $ih $is[1];
            
    $t 2;
        } if ( 
    $t == ) {
            if(
    $file_ext == "jpg") {
                
    $img_src imagecreatefromjpeg$sourcefile );
            } elseif(
    $file_ext == "png") {
                
    $img_src imagecreatefrompng$sourcefile );
            }
                
    $img_dst imagecreatetruecolor$iw$ih );
                
    imagecopyresampled$img_dst$img_src0000$iw$ih$is[0], $is[1] );
                if( !
    imagejpeg$img_dst$destfile90 ) ) {
                    exit( );
                }
        } else if ( 
    $t == ) {
            
    copy$sourcefile$destfile );
        }

    Thanks for any help.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think one of these
    imagealphablending()
    imagesavealpha()

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    any idea what I should do with those or where I should put them? Maybe in this if statement?

    PHP Code:
      } elseif($file_ext == "png") {
                
    $img_src imagecreatefrompng$sourcefile );
            } 

  4. #4
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Wichita, KS
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't had a chance to test it, but this should do it:

    PHP Code:
    <?php
    function resizeimage($forcedwidth$forcedheight$sourcefile$destfile$file_ext) {
        
    $fw $forcedwidth;
        
    $fh $forcedheight;
        
    $is getimagesize$sourcefile );
        if( 
    $is[0] >= $is[1] ) {
            
    $orientation 0;
        } else {
            
    $orientation 1;
            
    $fw $forcedheight;
            
    $fh $forcedwidth;
        }
        if ( 
    $is[0] > $fw || $is[1] > $fh ) {
            if( ( 
    $is[0] - $fw ) >= ( $is[1] - $fh ) ) {
                
    $iw $fw;
                
    $ih = ( $fw $is[0] ) * $is[1];
            } else {
                
    $ih $fh;
                
    $iw = ( $ih $is[1] ) * $is[0];
            }
            
    $t 1;
        } else {
            
    $iw $is[0];
            
    $ih $is[1];
            
    $t 2;
        } if ( 
    $t == ) {
            
    $img_dst imagecreatetruecolor$iw$ih );
            if(
    $file_ext == "jpg") {
                
    $img_src imagecreatefromjpeg$sourcefile );
            } elseif(
    $file_ext == "png") {
                
    $img_src imagecreatefrompng$sourcefile );
                
    imagecolortransparent($img_dst,imagecolorallocate($img_dst000));
                
    imagealphablending($img_dst,false);
                
    // imagesavealpha($img_dst,true); // you probably don't need this line
            
    }
                
    imagecopyresampled$img_dst$img_src0000$iw$ih$is[0], $is[1] );
                if( !
    imagejpeg$img_dst$destfile90 ) ) {
                    exit( );
                }
        } else if ( 
    $t == ) {
            
    copy$sourcefile$destfile );
        }
    }
    ?>

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chad, thanks for the response, but that only made the png look really low quality with jagged edges and it still has the black background. I also tried un-commenting that extra line of code, but it had the same results. Any ideas?

  6. #6
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Wichita, KS
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah I think I see why..

    PHP Code:
                if( !imagejpeg$img_dst$destfile90 ) ) {
                    exit( );
                } 
    Probably needs to be
    PHP Code:
                if($file_ext == "jpg") {
                    if( !
    imagejpeg$img_dst$destfile90 ) )
                        exit( );
                    }
                }
                else {
                    if( !
    imagepng$img_dst$destfile ) )
                        exit( );
                    }
                } 

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Hey thanks again! That worked the transparent background is showing up now, but the only problem is that image quality is now very poor.

    Here is the code I am using now:
    PHP Code:
    function resizeimage($forcedwidth$forcedheight$sourcefile$destfile$file_ext) {

        
    $fw $forcedwidth;

        
    $fh $forcedheight;

        
    $is getimagesize$sourcefile );

        if( 
    $is[0] >= $is[1] ) {

            
    $orientation 0;

        } else {

            
    $orientation 1;

            
    $fw $forcedheight;

            
    $fh $forcedwidth;

        }

        if ( 
    $is[0] > $fw || $is[1] > $fh ) {

            if( ( 
    $is[0] - $fw ) >= ( $is[1] - $fh ) ) {

                
    $iw $fw;

                
    $ih = ( $fw $is[0] ) * $is[1];

            } else {

                
    $ih $fh;

                
    $iw = ( $ih $is[1] ) * $is[0];

            }

            
    $t 1;

        } else {

            
    $iw $is[0];

            
    $ih $is[1];

            
    $t 2;

        } if ( 
    $t == ) {

            
    $img_dst imagecreatetruecolor$iw$ih );

            if(
    $file_ext == "jpg") {

                
    $img_src imagecreatefromjpeg$sourcefile );

            } elseif(
    $file_ext == "png") {

                
    $img_src imagecreatefrompng$sourcefile );

                
    imagecolortransparent($img_dst,imagecolorallocate($img_dst000));

                
    imagealphablending($img_dst,false);

                
    // imagesavealpha($img_dst,true); // you probably don't need this line

            
    }

                
    imagecopyresampled$img_dst$img_src0000$iw$ih$is[0], $is[1] );

                 if(
    $file_ext == "jpg") {

                    if( !
    imagejpeg$img_dst$destfile90 ) ) {

                        exit( );

                    }

                }

                else {

                    if( !
    imagepng$img_dst$destfile ) ) {

                        exit( );

                    }

                } 


        } else if ( 
    $t == ) {

            
    copy$sourcefile$destfile );

        }



  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You should only have to apply the imagesavealpha() function to your resource, try the following:-

    PHP Code:
    <?php
    function resizeimage($forcedwidth$forcedheight$sourcefile$destfile$file_ext) {

        
    $fw $forcedwidth;

        
    $fh $forcedheight;

        
    $is getimagesize$sourcefile );

        if( 
    $is[0] >= $is[1] ) {

            
    $orientation 0;

        } else {

            
    $orientation 1;

            
    $fw $forcedheight;

            
    $fh $forcedwidth;

        }

        if ( 
    $is[0] > $fw || $is[1] > $fh ) {

            if( ( 
    $is[0] - $fw ) >= ( $is[1] - $fh ) ) {

                
    $iw $fw;

                
    $ih = ( $fw $is[0] ) * $is[1];

            } else {

                
    $ih $fh;

                
    $iw = ( $ih $is[1] ) * $is[0];

            }

            
    $t 1;

        } else {

            
    $iw $is[0];

            
    $ih $is[1];

            
    $t 2;

        } if ( 
    $t == ) {

            if(
    $file_ext == "jpg") {

                
    $img_src imagecreatefromjpeg$sourcefile );

            } elseif(
    $file_ext == "png") {

                
    $img_src imagecreatefrompng$sourcefile );
                
    imagesavealpha$img_src true );

            }

                
    $img_dst imagecreatetruecolor$iw$ih );

                
    imagecopyresampled$img_dst$img_src0000$iw$ih$is[0], $is[1] );
                
    imagesavealpha$img_dst true );

                if( !
    imagejpeg$img_dst$destfile90 ) ) {

                    exit( );

                }

        } else if ( 
    $t == ) {

            
    copy$sourcefile$destfile );

        }


    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Wichita, KS
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bigimaginations View Post
    Hey thanks again! That worked the transparent background is showing up now, but the only problem is that image quality is now very poor.
    I finally tested the code. With my test image it looks a lot better with

    PHP Code:
    imagesavealpha($img_dst,true); 
    Uncommented. It also does not work without imagecolortransparent and imagealphablending.

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    You guys both rock, that worked great! Chad, I must not have cleared my cache after I un-commented the "imagesavealpha" line because that did the trick.

    Thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •