【问题标题】:PHP PNG image creation outputting wrong colorsPHP PNG图像创建输出错误的颜色
【发布时间】:2013-11-05 14:05:54
【问题描述】:

我正在尝试在底层 png 文件上放置一个 png 覆盖。 该脚本工作得很好,但不是给我应有的颜色覆盖,而是以灰色输出。 但只有叠加图像是灰色的(叠加的bg-color,基本上是底层图像的边框:RGB 20,114,158)。它是来自 PS 的 24 位。 透明部分(白色)效果很好。

<?php
$im = imagecreatefrompng($sourceFile);
$overlay = imagecreatefrompng($overlayFile);
$white = imagecolorallocate($overlay, 255, 255, 255);
imagecolortransparent($overlay, $white);
imagecopymerge($im, $overlay, 0, 0, 0, 0, 173, 173,100); 
header('Content-Type: image/png'); 
imagepng($im);
?>

非常感谢任何帮助!

干杯

克里斯

【问题讨论】:

    标签: php gd imagecreatefrompng


    【解决方案1】:

    你应该只使用imagecopy()(你使用100作为imagecopymerge()的最后一个参数($pct)。

    pct
    两幅图像将根据 pct 进行合并,范围为 0 到 100。 当 pct = 0 时,不采取任何行动,当 100 时,此函数的行为与 imagecopy() 用于调色板图像,除了忽略 alpha 分量,而它 实现真彩色图像的 alpha 透明度。

    别忘了在两张图片上启用imagealphablending()

    【讨论】:

      【解决方案2】:

      你可以试试这个(不是我的代码):

      <?php
      /**
       * PNG ALPHA CHANNEL SUPPORT for imagecopymerge();
       * This is a function like imagecopymerge but it handle alpha channel well!!!
       **/
      
      // A fix to get a function like imagecopymerge WITH ALPHA SUPPORT
      // Main script by aiden dot mail at freemail dot hu
      // Transformed to imagecopymerge_alpha() by rodrigo dot polo at gmail dot com
      function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
          if(!isset($pct)){
              return false;
          }
          $pct /= 100;
          // Get image width and height
          $w = imagesx( $src_im );
          $h = imagesy( $src_im );
          // Turn alpha blending off
          imagealphablending( $src_im, false );
          // Find the most opaque pixel in the image (the one with the smallest alpha value)
          $minalpha = 127;
          for( $x = 0; $x < $w; $x++ )
          for( $y = 0; $y < $h; $y++ ){
              $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF;
              if( $alpha < $minalpha ){
                  $minalpha = $alpha;
              }
          }
          //loop through image pixels and modify alpha for each
          for( $x = 0; $x < $w; $x++ ){
              for( $y = 0; $y < $h; $y++ ){
                  //get current alpha value (represents the TANSPARENCY!)
                  $colorxy = imagecolorat( $src_im, $x, $y );
                  $alpha = ( $colorxy >> 24 ) & 0xFF;
                  //calculate new alpha
                  if( $minalpha !== 127 ){
                      $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha );
                  } else {
                      $alpha += 127 * $pct;
                  }
                  //get the color index with new alpha
                  $alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
                  //set pixel with the new color + opacity
                  if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){
                      return false;
                  }
              }
          }
          // The image copy
          imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
      }
      
      // USAGE EXAMPLE:
      $img_a = imagecreatefrompng('image1.png');
      $img_b = imagecreatefrompng('wm2.png');
      
      // SAME COMMANDS:
      imagecopymerge_alpha($img_a, $img_b, 10, 10, 0, 0, imagesx($img_b), imagesy($img_b),50);
      
      // OUTPUT IMAGE:
      header("Content-Type: image/png");
      imagesavealpha($img_a, true);
      imagepng($img_a, NULL);
      ?>
      

      源:http://www.php.net/manual/en/function.imagecopymerge.php#88456

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-15
        • 2013-01-30
        • 2018-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多