2012-03-31 15 views
3

イメージ(.png、.jpg、または.gif)に別のイメージをウォーターマークする関数を作成しようとしています。これらのフォーマットの1つでもある可能性があります。私は両方の画像が透明性を保つことを望む。私はgifアニメーションを気にしません。残念ながら、ウォーターマークは、それが想定されていない場所で透明になり、完全に透明ではありません。私はこのスクリプトをテストするために私に利用できるいくつかのランダムな画像を使用し、そして私の結果は次のようになります。透かしのイメージは次のようになりますが.png、.gif、.jpgを.png、.gif、.jpgにウォーターマーカーリングする

enter image description here

enter image description here

enter image description here

ここに私のコードです:

function watermark_get_left() 
    { 
    return NCFG_TEMPLATE_DIR . 'images/watermark_left.png'; 
    } 

    function watermark_get_right() 
    { 
    return NCFG_TEMPLATE_DIR . 'images/watermark_right.png'; 
    } 

    function create_img($path, $type) 
    { 
    switch($type) 
    { 
     case NCFG_IMAGE_JPG: 
     $img = imagecreatefromjpeg($path); 
     return $img; 
     case NCFG_IMAGE_GIF: 
     $img = imagecreatefromgif($path); 
     return $img; 
     case NCFG_IMAGE_PNG: 
     $img = imagecreatefrompng($path); 
     imagealphablending($img, false); 
     imagesavealpha($img, true); 
     return $img; 
     default: 
     return FALSE; 
    } 
    } 

    function img_transparency($new_img, $img, $type) 
    { 
    if($type == NCFG_IMAGE_GIF) 
    { 
     $transparent_index = imagecolortransparent($img); 

     if($transparent_index != -1) 
     { 
     $transparent_color = imagecolorsforindex($img, $transparent_index); 
     $transparent_new = imagecolorallocate($new_img, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); 
     $transparent_new_index = imagecolortransparent($new_img, $transparent_new); 
     imagefill($new_img, 0, 0, $transparent_new_index); 
     } 
    }else if($type == NCFG_IMAGE_PNG){ 
     imagecolortransparent($new_img, imagecolorallocatealpha($new_img, 0, 0, 0, 127)); 
     imagealphablending($new_img, false); 
     imagesavealpha($new_img, true); 
    } 
    } 

    function image_watermark($path, $new_path, &$w_height) 
    { 
    $type = get_image_type($path); 
    list($width, $height) = getimagesize($path); 

    if(!($img = create_img($path, $type))) 
    { 
     return FALSE; 
    } 

    $l_path = watermark_get_left(); 
    $r_path = watermark_get_right(); 

    if($l_exists = file_exists($l_path)) 
    { 
     $l_type = get_image_type($l_path); 
     list($l_width, $l_height) = getimagesize($l_path); 

     if(!($l_img = create_img($l_path, $l_type))) 
     { 
     return FALSE; 
     } 
    } 

    if($r_exists = file_exists($r_path)) 
    { 
     $r_type = get_image_type($r_path); 
     list($r_width, $r_height) = getimagesize($r_path); 

     if(!($r_img = create_img($r_path, $r_type))) 
     { 
     return FALSE; 
     } 
    } 

    $w_height = max(($r_exists ? $r_height : 0), ($l_exists ? $l_height : 0)); 

    echo("w_height: $w_height<br/>"); 

    $new_width = $width; 
    $new_height = $height + $w_height; 

    $new_img = imagecreatetruecolor($new_width, $new_height); 

    $bg_color = imagecolorallocate($new_img, 255,255,255); 
    imagefilledrectangle($new_img, 0, $height, $new_width - 1, $new_height - 1, $bg_color); 

    img_transparency($new_img, $img, $type); 
    imagecopy($new_img, $img, 0, 0, 0, 0, $width, $height); 

    if($l_exists) 
    { 
     imagecopy($new_img, $l_img, 0, $height, 0, 0, $l_width, $l_height); 
    } 

    if($r_exists) 
    { 
     imagecopy($new_img, $r_img, $new_width - $r_width, $height, 0, 0, $r_width, $r_height); 
    } 

    switch($type) 
    { 
     case NCFG_IMAGE_JPG: 
     imagejpeg($new_img, $new_path); 
     break; 
     case NCFG_IMAGE_GIF: 
     imagegif($new_img, $new_path); 
     break; 
     case NCFG_IMAGE_PNG: 
     imagepng($new_img, $new_path, 9); 
     break; 
    } 

    imagedestroy($new_img); 
    imagedestroy($img); 

    return TRUE; 
    } 

私はimagecopymergeも試してみました。無駄です。私はこれについて何か助けていただければ幸いです。

編集:私の予想どおりの画像が表示されました。ウォーターマークの付いたイメージは、白い背景では正しいように見えますが、他の背景にはありません。

+4

私は両方の画像に透明性を保持したいと考えています。透明性は気にしない。 – mowwwalker

+2

私が知る限り、透かしは半透明であると考えられます。透かしがないので、jpgを透かしとして使うことはできません。そして、これは本当に混乱しています。「私は両方の画像が透明性を保つことを願っています。私は透明性について気にしません。あなたはそれを望むかしないか? –

+2

申し訳ありませんが、私は「私はGIFアニメーションを気にしません」と書いたかったのです...私はあまりにも長くそれをしてきました。 – Neob91

答えて

1

これにはImageMagick CLIを使用できます。ここでは1つのライナーです:

composite -dissolve 50% -geometry +20+10 -gravity SouthWest watermark.png input.png output.png 

この+20, +10ピクセルのオフセットとSouthWest隅に50%の不透明度をinput.pngの上にwatermark.pngを配置します。あなたは、あなたの好みに合わせて微調整し、shell_execとPHPでそれを使用することができます。

shell_exec('composite ...'); 

参考:http://www.imagemagick.org/script/command-line-options.php

サンプル画像:

編集: あなたの場合元の画像の高さを(たとえば20ピクセルで)拡大したい場合は、d o this first:

convert original.png -bordercolor transparent -border 0x20 -background transparent -gravity NorthWest -extent +0+20 input.png 
+0

残念ながら、私はImageMagickを私のサーバにインストールしていません。私もそれをインストールするためのアクセス権がありません。 – Neob91

関連する問題