SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems resizing transparent PNG image.

    Hello,

    I am creating an application that will help me resize images quickly.

    Some of the images I wanted to resize in in PNG format, with alpha channel, but when I resize using imagecopyresampled, the output image is not ahh, transparent anymore.

    Any ideas please?

    This is the way this application work: This application opens the source image, and calculate the destination size, create a new true color image, and then imagecopyresampled to the destination image resource, then write to file.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Devon, UK
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure why none of it's transparent, but it really doesn't surprise me. You don't have much control over image resizing and you need to do it in a decent graphics package if you want to keep the quality.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)

    imagealphablending()

    http://www.php.net/manual/en/functio...hablending.php
    imagesavealpha()
    http://www.php.net/manual/en/functio...esavealpha.php

    If you want to resize a png-24 image and preserve the alpha channel you need to set imagealphablending($im_dest, false) on the destination image just after creating it with imagecreatetruecolor() and do a imagesavealpha($im_dest, true) on it before saving it:
    PHP Code:
    <?php

    $im 
    ImageCreateFromPNG('redfade.png');

    $im_dest imagecreatetruecolor (500300);
    imagealphablending($im_destfalse);

    imagecopyresampled($im_dest$im0000300300500300);

    imagesavealpha($im_desttrue);
    imagepng($im_re'small_redfade.png');

    ?>
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, thank you very much!

    Looks like imagesavealpha is the key. I'll try it.

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by the DtTvB View Post
    Oh, thank you very much!

    Looks like imagesavealpha is the key. I'll try it.
    For all of you looking for a way to transform images on the fly, you might want to check the open-source project Asido: there are a lot of useful features (http://www.asido.info/about/features/), including various types of image resize - like fit resize (resize if bigger) and frame resize (force-fit), watermarking, image copy, cropping, etc. There is no problem working w/ transparent images at all - check the examples on the Asido website. Also, it can use various platforms: not only GD but ImageMagick too (either via shell or via any of the php extensions like php_magickwand.dll or php_imagick.dll). Here's the list of drivers: http://www.asido.info/about/drivers/


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
  •