1

これに対する答えを求めてインターネットを精査しましたが、うまくいくものを見つけることができないようです。画像をたとえば 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);
?>

ただし、これは次のようになります。

収量

黒い境界ボックスを透明にするにはどうすればよいですか?

ありがとう!

4

3 に答える 3

2

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);
?>
于 2012-05-21T23:16:35.830 に答える
0

よくわかりませんが、その特定の PHP 関数の使用に慣れていません。とはいえ、ほとんどの開発者は、PHP での画像処理に ImageMagick または GD を直接使用することを好むようです。使用している機能は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

于 2012-05-21T23:09:26.087 に答える
0

これが機能するコードです(私の3年前のアーカイブから掘り起こしました)。透明性を維持しながら、2 つの png 画像を結合し、テキストをレンダリングするために使用しました。新しい画像を作成し、透過色を設定してから、画像をコピーします。この正確なファイルは私の実装で機能しました(当時はデータベースから色を抽出していたため、色は現在作成しているため、色が何であり、何を意味するのか記憶がありません)。雑だったらごめんなさい。結果の画像は$image$ruler_img、1$index_img番目のレイヤー、2 番目のレイヤーがテキスト、3 番目のレイヤーがテキストで構成されます。あなたのケースには1つのレイヤーしかありません。

        //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);
于 2012-05-21T23:10:35.613 に答える