in reply to Resizing images with transparent background?

The problem is that Image::Resizer creates a new default image into which it resizes the original. It takes no account of the attributes--truecolor .v. palette; transparancy color etc.--of the original image.

It could (should) query these attributes of the original image and use them to create a compatible image before performing the copyResize(). Something like this (untested) might do the trick:

sub resize { my $self = shift; my ($width, $height, $constraint) = @_; unless ( defined $constraint ) { $constraint = 1; } unless ( $width && $height ) { croak "Image::Resize->resize(): usa +ge error"; } if ( $constraint ) { my $k_h = $height / $self->height; my $k_w = $width / $self->width; my $k = ($k_h < $k_w ? $k_h : $k_w); $height = int($self->height * $k); $width = int($self->width * $k); } my $image = GD::Image->new($width, $height, $self->truecolor ); ## + modified $image->transparent( $self->transparent() ); ## Added $image->copyResampled($self->gd, 0, 0, # (destX, destY) 0, 0, # (srcX, srxY ) $width, $height, # (destX, destY) $self->width, $self->height ); return $image; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Resizing images with transparent background?
by aweizd (Initiate) on Oct 14, 2010 at 12:45 UTC
    Thanks for the code. I tried it and perl tells me it can't locate methods "trueColor" and "transparent" ?!? I have no idea how that can be since these methods are part of GD::Image and all the other GD::Image methods work. Any ideas?

      The modified lines should be:

      my $image = GD::Image->new($width, $height, $self->gd->truecolor ) +; $image->transparent( $self->gd->transparent() );

      Still essentially untested. I haven't found an image with a transparent background kicking around.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        It runs with the gd, but transparent() returns "255" for gif and "-1" for png. The result is black background for both. I think I'll just go with ImageMagick and resize to png since that gives me a real alpha channel and not just a single color transparency, as a result i get smooth edges.

        Thanks a lot for your help.

        It runs with the gd, but transparent() returns "255" for gif and "-1" for png. The result is black background for both. I think I'll just go with ImageMagick and resize to png since that gives me a real alpha channel and not just a single color transparency, as a result i get smooth edges.

        Thanks a lot for your help.