【问题标题】:Black background in the bounding box created when an image is rotated with PHP使用 PHP 旋转图像时创建的边界框中的黑色背景
【发布时间】:2012-05-21 22:58:45
【问题描述】:

我已经在互联网上搜索了这个问题的答案,但我似乎找不到有用的东西。当我将图像旋转 5 度时,该图像会在为容纳旋转而创建的边界图像内旋转。创建的图像是全黑的。

我正在尝试使边界框图像完全透明。 我看过的其他一些网站和问题说这应该可行:

<?PHP
$png = imagecreatefrompng('polaroids/polaroid0002.png');

// Do required operations
$png = imagerotate($png, 354.793, 0, 0);

// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);

// Output image to browser
header('Content-Type: image/png');

imagepng($png);
imagedestroy($png);
?>

但是,这会产生:

如何使黑色边框透明?

谢谢!

【问题讨论】:

    标签: php transparency image-rotation bounding-box


    【解决方案1】:

    只需为背景颜色设置透明颜色imagecolorallocatealpha($png, 0, 0, 0, 127)即可。

    <?php
    $png = imagecreatefrompng('polaroids/polaroid0002.png');
    
    // Do required operations
    $png = imagerotate($png, 354.793, imagecolorallocatealpha($png, 0, 0, 0, 127), 0);
    
    // Turn off alpha blending and set alpha flag
    imagealphablending($png, false);
    imagesavealpha($png, true);
    
    // Output image to browser
    header('Content-Type: image/png');
    
    imagepng($png);
    imagedestroy($png);
    ?>
    

    【讨论】:

      【解决方案2】:

      不确定,我不熟悉使用那个特定的 PHP 函数。也就是说,大多数开发人员似乎更喜欢直接使用 ImageMagick 或 GD 来使用 PHP 进行图像处理。您使用的功能似乎是 GD 功能。我会开始在这里寻找。您可能需要稍微修改图像资源以设置透明度或不同的颜色。

      http://php.net/manual/en/book.image.php

      http://php.net/manual/en/book.imagick.php

      我建议看看这个:http://php.net/manual/en/function.imagecolortransparent.php

      【讨论】:

      • 此脚本使用 GD。我还没有看到任何使用 ImageMagick 的教程。
      • 是的,对不起,我一秒钟前就草草修改了答案。
      • 尝试在标题部分之前添加:$black = imagecolorallocate($png, 0, 0, 0); imagecolortransparent($png, $black);,但没有任何改变。
      【解决方案3】:

      这是一个有效的代码(从我 3 年前的档案中挖掘出来)。我用它来组合 2 个 png 图像并渲染一些文本,同时保持透明度。它创建一个新图像,设置透明度颜色,然后将图像复制到其中。这个确切的文件适用于我的实现(但是我现在已经制作了颜色,因为当时我是从数据库中提取它们,所以我不记得它们是什么以及它们的含义)。很抱歉,如果它很乱。生成的图像为$image,由$ruler_img作为第一层,$index_img作为第二层,文本作为第三层组成。你的情况只有一层。

              //prepare first layer
              $ruler_img=$ruler_img=imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].$ruler['out_ruler_img']);
              //create main image, basic layer, background
              $image = imageCreateTrueColor($ruler_width, $ruler_height);
              imageSaveAlpha($image, true);
              $transparentColor = imagecolorallocatealpha($image, 0,0,0, 127); //some color used as transparency key, this uses black
              $color = imagecolorallocatealpha($image, 0,0,0,127);
              //fill with transparent color
              imagefill($image, 0, 0, $transparentColor);
      
              imagealphablending($ruler_img,true);
              //copy the first layer
              imagecopy($image,$ruler_img,0,0,0,0,$ruler_width,$ruler_height);
      
              //prepare the second layer 
              $index_img=imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].$ruler['out_index_img']);
              $size=getimagesize($_SERVER['DOCUMENT_ROOT'].$ruler['out_index_img']);
              $index_width=$size[0];
              $index_height=$size[1];
      
              imagealphablending($index_img,true);
      
              $ratio=1.0;
              if($index_height>$ruler_height)
                  $ratio=$ruler_height*1.0/$index_height;
              $new_width=$index_width*$ratio;
              $new_height=$index_height*$ratio;
      
              //now I copy the resampled second layer 
              imagecopyresampled(
                  $image,
                  $index_img,
                  $position*($ruler_width-$new_width),
                  ($ruler_height-$new_height)/2,
                  0,
                  0,
                  $new_width,
                  $new_height,
                  $index_width,
                  $index_height);
              //render text   
              $font = "fonts/comic.ttf";
              $fontSize=10;
              $box=imagettfbbox($fontSize, 0, $font,$text);
              $textWidth=$box[2]-$box[6];
              $textHeight=$box[3]-$box[7];
      
              imagettftext($image, $fontSize, 0, $ruler_width-$textWidth-10, $ruler_height+12, $color, $font, $text);
              header("Content-type: image/png");
              //that's it!
              imagepng($image);
      

      【讨论】:

      • 非常有趣的实现。试试这个。
      • 我决定采用上面发布的更简单的解决方案。不过还是谢谢。