douan3182 2014-12-08 13:24
浏览 172

imagepng()和imagegif()中的图像透明度

I want to resize 2 types of images (png, gif) with transparent background...

  1. But in png it works well in big size of the original image. eg. Original file dimension is 150x150 enter image description here

    change into 200x200 or exceeding the original size it works well. but in 100x100 it displays enter image description here

  2. Then the last GIF it doesn't work.

    Here my code for png

    $new_img = imagecreatetruecolor($max_w, $max_h);

    imagealphablending($new_img, false);
    imagesavealpha($new_img, true);

    $transparentindex = imagecolorallocatealpha($new_img, 255, 255, 255, 127);

    imagefill($new_img, 0, 0, $transparentindex);

    imagepng($new_img, $dir);

    Here my code for gif

    imagegif($new_img, $dir);
  • 写回答

2条回答 默认 最新

  • douge3830 2014-12-08 14:31
    关注

    I know no PHP, but this seems to do the trick:

    #!/usr/local/bin/php -f
    <?php
        $neww=100;
        $newh=100;
    
        // Load original image and get its dimensions
        $img = imagecreatefrompng("badge.png");
        $w=imagesx($img);
        $h=imagesy($img);
    
        // Create output image, and fill with lots of transparency
        $out = imagecreatetruecolor($neww,$newh);
        imagealphablending($out,false);
        $transparentindex = imagecolorallocatealpha($out,0,0,0,127);
        imagefill($out,0,0,$transparentindex);
        imagesavealpha($out, true);
    
        // Copy original image, reized on top
        imagecopyresized($out,$img,0,0,0,0,$neww,$newh,$w,$h);
        imagepng($out,"out.png");
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line