2017-07-14 3 views
2

私は助けが必要です。私はPHPライブラリGDで画像を作成してい透明な背景がGDで動作しないのはなぜですか?

$image = imagecreatefrompng('http://polariton.ad-l.ink/8rRSf4CpN/thumb.png'); 
$w = imagesx($image); 
$h = imagesy($image); 

$border = imagecreatefrompng('https://www.w3schools.com/css/border.png'); 

// New border width 

$x1 = $w; 
$x2 = 28; 
$x3 = (int)($x1/$x2); 
$x4 = $x1 - $x3 * $x2; 
$x5 = $x4/$x3; 
$x2 = $x2 + $x5; 

$bw = $x2; 

// New border height 

$y1 = $h; 
$y2 = 28; 
$y3 = (int)($y1/$y2); 
$y4 = $y1 - $y3 * $y2; 
$y5 = $y4/$y3; 
$y2 = $y2 + $y5; 

$bh = $y2; 

// New image width and height 

$newWidth = (int)$w * (1 - ((($bw * 100)/(int)$w)/100 * 2)); 
$newHeight = (int)$h * (1 - ((($bh * 100)/(int)$h)/100 * 2)); 

// Transparent border 

$indent = imagecreatetruecolor($w, $h); 
$color = imagecolorallocatealpha($indent, 0, 0, 0, 127); 
imagefill($indent, 0, 0, $color); 

imagecopyresampled($indent, $image, $bw, $bw, 0, 0, $newWidth, $newHeight, $w, $h); 

// Vertical border 

$verticalX = 0; 
$verticalY = $bh; 

while ($verticalY < $h - $bh) { 
    // Left 
    imagecopy($indent, $border, $verticalX, $verticalY, 0, 27, $bh, $bh); 

    // Right 
    imagecopy($indent, $border, $w - $bw, $verticalY, 0, 27, $bh, $bh); 

    $verticalY += $bh; 
} 

// Horizontal border 

$horizontalX = $bw; 
$horizontalY = 0; 

while ($horizontalX < $w - $bw) { 
    // Top 
    imagecopy($indent, $border, $horizontalX, $horizontalY, 0, 27, $bw, $bw); 

    // Bottom 
    imagecopy($indent, $border, $horizontalX, $h - $bh, 0, 27, $bw, $bw); 

    $horizontalX += $bw; 
} 

// Left top border 
imagecopy($indent, $border, 0, 0, 0, 0, $bw, $bh); 

// Right top border 
imagecopy($indent, $border, $w - $bw, 0, 0, 0, $bw, $bh); 

// Right bottom border 
imagecopy($indent, $border, $w - $bw, $h - $bh, 0, 0, $bw, $bh); 

// Left bottom border 
imagecopy($indent, $border, 0, $h - $bh, 0, 0, $bw, $bh); 


// Save result 

header('Content-Type: image/png'); 
imagepng($indent); 

しかし、透明性does't作業: enter image description here

IDKを、なぜ、私はマニュアルに書かれたよう全力を尽くします。

何が問題なのですか?誰かがそれを解決するのに役立つだろうか? ありがとうございました。

+0

JPEGファイルは透過性をサポートしていません。 – timclutton

+0

PNGのイメージを変更し、imagecreatefromjpegをimagecreatefrompngに、imagejpeg($ indent、null、100)をimagepng($ indent)に変更しました。 – wagwandude

+0

しかし、まだ動作しませんでした。 – wagwandude

答えて

2

JPEGファイルは透過性をサポートしていません。

PNGなどの別の形式を試すことができます。

あなたはあなたの新しいイメージリソースを作成した直後imagesavealphaを呼び出すことにより、アルファチャンネルの透明性を維持するために、出力を可能にするために、あなたのコードに小さな変更を加える必要があります:

... 
$indent = imagecreatetruecolor($w, $h); 
imagesavealpha($indent, true); 
... 

そしてあなたの最後を変更2行:

header('Content-Type: image/png'); 
imagepng($indent); 
+0

ありがとう! – wagwandude