1

私はpngファイルを持っていて、この画像の一部(長方形)を透明にしたいと思っています。

たとえば、次のようなものです。

疑似コード:

<?php
$path = 'c:\img.png';
set_image_area_transparent($path, $x, $y, $width, $height);
?>

ここで、x、y、$width、$height は画像内の四角形を定義し、透明に設定する必要があります。

PHPでいくつかのライブラリを使用することは可能ですか?

4

2 に答える 2

2

はい、可能です。画像内の領域を定義し、それを色で塗りつぶしてから、その色を透明に設定できます。GD ライブラリが利用可能である必要があります。コマンドのそれぞれのマニュアルには、例に次のコードがあります。

<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

// Make the background transparent
imagecolortransparent($im, $black);

// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);

// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>

あなたの場合、それぞれの関数で既存の画像を取得します。結果のリソースは上記の例の $im になります。次に、色を割り当て、透明に設定し、上記のように四角形を描画してから、画像を保存します。

<?php
// get the image form the filesystem
$im = imagecreatefromjpeg($imgname);
// let's assume there is no red in the image, so lets take that one
$red = imagecolorallocate($im, 255, 0, 0);

// Make the red color transparent
imagecolortransparent($im, $red);

// Draw a red rectangle in the image
imagefilledrectangle($im, 4, 4, 50, 25, $red);

// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
于 2013-03-12T06:23:18.663 に答える
1

まず、画像にアルファチャンネルを設定する必要があります: http://www.php.net/manual/en/function.imagealphablending.php http://www.php.net/manual/en/function.imagesavealpha.php

次に、透明領域のすべてのピクセルに透明色を設定する必要があります: http://www.php.net/manual/en/function.imagecolorset.php

于 2013-03-12T06:17:44.270 に答える