PNG Transparency with PHP

前端 未结 5 1475
半阙折子戏
半阙折子戏 2020-11-28 05:28

Hey having some trouble trying to maintain transparency on a png when i create a thumbnail from it, anyone any experience with this? any help would be great, here\'s what i

相关标签:
5条回答
  • 2020-11-28 06:03

    See dycey's answer to "How do I resize...". Essentially, you need to fill the entire background with transparency before you do any other operations.

    0 讨论(0)
  • 2020-11-28 06:07

    Forget the color transparency index, it never works in all rendering products. Instead use an alpha layer mask:

    $image = imagecreatetruecolor($size, $size);
    
    imagealphablending($image, false);
    imagesavealpha($image, true);
    
    $trans_layer_overlay = imagecolorallocatealpha($image, 220, 220, 220, 127);
    imagefill($image, 0, 0, $trans_layer_overlay);
    
    0 讨论(0)
  • 2020-11-28 06:15

    Those functions access the underlying gdlib library, which is a fine toy, but not something that makes for nice results. If you have the option, use imagemagick instead. The downside is that there are currently no good php-bindings, so you need to access it over the shell, which you're usually not allowed on shared hosts.

    0 讨论(0)
  • 2020-11-28 06:19

    I have had success doing it like this in the past:

    $thumb = imagecreatetruecolor($newwidth, $newheight);
    imagealphablending($thumb, false);
    imagesavealpha($thumb, true);  
    
    $source = imagecreatefrompng($fileName);
    imagealphablending($source, true);
    
    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
    imagepng($thumb,$newFilename);
    

    I found the output image quality much better using imagecopyresampled() than imagecopyresized()

    0 讨论(0)
  • 2020-11-28 06:19

    imagecopyresized does not support transparency properly.

    imagecopymerge does, but it doesn't resize.

    The solution? You'd probably end up resizing the thing manually.

    0 讨论(0)
提交回复
热议问题