2017-07-05 6 views
0

2つの大きな画像を撮影しようとしていますが(後で合計6枚の画像を合成します)、Photoshopから撮影したx、y幅、高さにサイズを変更して結合します1つの460 x 230サイズの画像に変換します。私は50を変更する場合は、Image GDサイズ変更の問題

imagecopymerge($dest2, $src2, 0, 0, 0, 0, 460, 230, 50);

これは私が

<?php 

$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png'); 
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png'); 

imagealphablending($dest, false); 
imagesavealpha($dest, true); 

imagealphablending($src, false); 
imagesavealpha($src, true); 


//imagescale($dest, 396, 161.92); 
$some = imagecreate(460, 230); 

$dest2 = resize($dest, 396, 162); 
$src2 = resize($src, 79.19, 79.19); 

//imagecopyresized($dest, $dest, 0, 0, 0, 0, 396, 161.92, 1098, 449); 
imagecopyresized($src, $src, 10, 10, 0, 0, 79.19, 79.19, 256, 256); 
//$img2 = imagecopymerge($dest, $src, 0, 0, 0, 0, 256, 256, 100); //have to play with these numbers for it to work for you, etc. 
imagecopymerge($dest2, $src2, 0, 0, 0, 0, 460, 230, 50); 

header('Content-Type: image/png'); 
imagepng($dest, 'merged2.png'); 
imagepng($dest2); 
//file_put_contents('merged.png', $contents); 
imagedestroy($dest); 
imagedestroy($src); 
imagedestroy($some); 
imagedestroy($dest2); 
imagedestroy($src2); 
imagedestroy($img2); 
//imagedestroy($then); 

function resize($img, $width, $height, $stretch = false) 
    { 
     $temp = imagecreatetruecolor($width, $height); 
     imagealphablending($temp, true); 
     imagesavealpha($temp, true); 

     $bg = imagecolorallocatealpha($temp, 0, 0, 0, 0); // Background color 
     imagefill($temp, 0, 0, $bg); 

     if ($stretch) 
     { 
      imagecopyresampled($temp, img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img)); 
     } 
     else 
     { 
      if (imagesx($img) <= $width && imagesy($img) <= $height) 
      { 
       $fwidth = imagesx($img); 
       $fheight = imagesy($img); 
      } 
      else 
      { 
       $wscale = $width/imagesx($img); 
       $hscale = $height/imagesy($img); 
       $scale = min($wscale, $hscale); 
       $fwidth = $scale * imagesx($img); 
       $fheight = $scale * imagesy($img); 
      } 
      imagecopyresampled($temp, 
       $img, 
       ($width - $fwidth)/2, ($height - $fheight)/2, 
       0, 0, 
       $fwidth, $fheight, 
       imagesx($img), imagesy($img) 
      ); 
     } 
     return $temp; 
    } 
ので、このラインの

問題がレンダリングされた画像は非常に薄れているされていることを を使用しているコードですPCT値を100にすると黒い背景の画像(別の画像をマスクする画像)が表示されますが、0に変更すると黒い背景の画像のみが表示されます(他の画像をマスクする) 値が0または100の場合、画像はフルカラーになります。透明性と色の鮮やかさを維持しながら、これらの2つの画像をどのようにマージしますか?

+0

私はこれらの値を使ってみました: 'imagealphablend ing($ dest、true); imagesavealpha($ dest、true); imagealphablending($ src、true); imagesavealpha($ src、true); ' –

答えて

0

imagecopymergeの代わりにimagecopyを使用してください。 - あなたが新しいイメージをレンダリング

$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png'); 
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png'); 

$dest2 = resize($dest, 396, 162); 
imagedestroy($dest); 
$src2 = resize($src, 79, 79); // should be int not float. 
imagedestroy($src); 

// the last 2 params must match the width/height of the $src2 image. 
imagecopy($dest2, $src2, 0, 0, 0, 0, 79, 79); 
imagedestroy($src2); 

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

imagedestroy($dest2); 

あなたは彼らがレンダリングされていないので、$destまたは$srcにアルファの設定を変更する必要はありません。コピーするときにも、常に正しく元画像の大きさを指定する必要がありますあなたのresize関数で作成され、返されたリソースです。あなたは少しの機能を変更する必要があります。このため、次

function resize($img, $width, $height, $stretch = false) 
{ 
    $temp = imagecreatetruecolor($width, $height); 
    imagealphablending($temp, false); // changed to false. 
    imagesavealpha($temp, true); 
    ... 

編集:

あなたは、単にimagescale機能を使用しての代わりに、独自のresize機能を使用したほうが良いかもしれません。

$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png'); 
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png'); 

$dest2 = imagescale($dest, 396); 
imagealphablending($dest2, false); 
imagesavealpha($dest2, true); 
$src2 = imagescale($src, 79); 

imagecopy($dest2, $src2, 0, 0, 0, 0, 79, 79); 

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

imagedestroy($dest); 
imagedestroy($src); 
imagedestroy($dest2); 
imagedestroy($src2); 
+0

@ChukwumaOkereこれで問題は解決しましたか? – timclutton

関連する問題