2017-05-17 9 views
0

JPEGとPNGをbase.pngという既存のPNGにコピーするためのスクリプトがあります。関数 "transparent_background"では、私は白い背景を透明に置き換えます。この機能が問題です。スタンドアロン機能は、ブラウザで直接出力を操作しています。 「// imagepng($ img);」のコメントを参照してください。しかし、もし私が関数から$ imgを返すと、それはまだjpegだと思います。なぜそれは透明ではないのですか? 2番目の関数はサイズ変更用です。透明な背景を持つJPEGとPNGを1つの画像にマージするPHP

<?php 

function transparent_background($img) 
{ 
    $img = imagecreatefromjpeg($img); //or whatever loading function you need 
    $colors= array("255","255","255"); 
    $remove = imagecolorallocate($img, $colors[0], $colors[1], $colors[2]); 
    imagecolortransparent($img, $remove); 
    //imagepng($img); 
    return $img; 
    imagedestroy($img); 
} 
function resize($img, $w){ 
    $img = imagecreatefromjpeg($img); 
    $ratio = imagesx($img)/imagesy($img); 
    if($ratio > 1) { 
     $width = $w; 
     $height = $w/$ratio; 
    } 
    else { 
     $width = $w*$ratio; 
     $height = $w; 
    } 
    $dst = imagecreatetruecolor($width,$height); 
    imagecopyresampled($dst,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img)); 
    return $dst; 
    imagedestroy($dst); 
    imagedestroy($img); 
} 

$h="https://images-eu.ssl-images-amazon.com/images/I/415zYwg2-TL.jpg"; 
$base = imagecreatefrompng("base.png"); 
$logo = imagecreatefrompng("fs_logo_line.png"); 

$pos1=resize($h,"730"); 
$pos1=transparent_background($h); 
imagecopy($base,$pos1,0, 5, 0, 0, imagesx($pos1),imagesy($pos1)); 
imagecopy($base,$logo,0, 1136, 0,0,imagesx($logo),imagesy($logo)); 

imagepng($base); 

?> 

私はこの問題は、私は戻ってtransparent_background機能からJPEGを得ること、であると$ POS1の画像が透明でない理由thatsのだと思います。どのように私はそれを解決することができる任意のアイデア?私はob_start & ob_get_contentsで試しましたが、これもうまくいきませんでした。

答えて

0

PHP GD2ライブラリを使用して、2つの画像を結合することができます。

例:あなたは、画像の上にPNGフレームtransparancyを維持したい場合は

<?php 
# If you don't know the type of image you are using as your originals. 
$image = imagecreatefromstring(file_get_contents($your_original_image)); 
$frame = imagecreatefromstring(file_get_contents($your_frame_image)); 

# If you know your originals are of type PNG. 
$image = imagecreatefrompng($your_original_image); 
$frame = imagecreatefrompng($your_frame_image); 

imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100); 

# Save the image to a file 
imagepng($image, '/path/to/save/image.png'); 

# Output straight to the browser. 
imagepng($image); 
?> 

imagecopymerge()imagealphablending($frame,true);を追加します。

+0

ありがとう、@Vishwas Soni。私はそれを試みます。しかし、私は問題が関数transparent_backgroundの中にあると思います。私はjpegを透明なものを行う関数にプッシュし、imagepngでファイルをブラウザ/ファイルに出力しません。だから、問題は私が関数からjpegを返すことだと思う。 jpegをブラウザやファイルに出力せずにpngに変換する方法があります。変数の中で。 – swapfile

+0

@swapfileこの回答は役に立ちます。 http://stackoverflow.com/questions/21105802/convert-image-format-png-to-jpeg-without-saving-to-disk-php –

+0

リンクをありがとうございます。しかし、ここに私はしたくないブラウザへの出力もあります。私は戻り変数内のpngを持っています。 return imagepng($イメージ);動作しません。 – swapfile