2011-01-26 15 views
3

2つのイメージを1つのバックグラウンドとして、もう1つをターゲットイメージとしてマージすると、私はPNGを使用しています。ターゲット画像を回転させてからマージすると、回転した画像のエッジがジグザグになる以外はすべてうまくなります。これは滑らかでないことを意味します。どのようにPHPのGDを使用してエッジを滑らかにするには???PHP回転とマージの画質問題

Normal Image

After Rotation

私が使用していますコード:

<?php 

// Create image instances 

$dest = imagecreatefrompng('bg.png'); 
$src = imagecreatefrompng('text.png'); 
$width = imagesx($src); 
$height = imagesy($src); 
imageantialias($src, true); 
$color = imagecolorallocatealpha($src, 0, 0, 0, 127); 
$rotated = imagerotate($src, 40, $color); 
imagesavealpha($rotated, true); 

// $trans_colour = imagecolorallocatealpha($rotated, 0, 0, 0, 127); 
// imagefill($rotated, 0, 0, $trans_colour); 

imagepng($rotated, 'shahid.png'); 
$new_img = imagecreatefrompng('shahid.png?'); 
$width = imagesx($new_img); 
$height = imagesy($new_img); 

// imagecopymerge($dest, $new_img, 50, 50, 0, 0, $width+60, $height+60, 100); 

imagecopymerge_alpha($dest, $new_img, 0, 20, 0, 0, $width, $height, 100); 

// Output and free from memory 

header('Content-Type: image/png'); 
imagepng($dest); 
imagedestroy($dest); 
imagedestroy($src); 

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) 
{ 
    if (!isset($pct)) { 
     return false; 
    } 

    $pct/= 100; 

    // Get image width and height 

    $w = imagesx($src_im); 
    $h = imagesy($src_im); 

    // Turn alpha blending off 

    imagealphablending($src_im, false); 

    // Find the most opaque pixel in the image (the one with the smallest alpha value) 

    $minalpha = 127; 
    for ($x = 0; $x < $w; $x++) 
    for ($y = 0; $y < $h; $y++) { 
     $alpha = (imagecolorat($src_im, $x, $y) >> 24) & 0xFF; 
     if ($alpha < $minalpha) { 
      $minalpha = $alpha; 
     } 
    } 

    // loop through image pixels and modify alpha for each 

    for ($x = 0; $x < $w; $x++) { 
     for ($y = 0; $y < $h; $y++) { 

      // get current alpha value (represents the TANSPARENCY!) 

      $colorxy = imagecolorat($src_im, $x, $y); 
      $alpha = ($colorxy >> 24) & 0xFF; 

      // calculate new alpha 

      if ($minalpha !== 127) { 
       $alpha = 127 + 127 * $pct * ($alpha - 127)/(127 - $minalpha); 
      } 
      else { 
       $alpha+= 127 * $pct; 
      } 

      // get the color index with new alpha 

      $alphacolorxy = imagecolorallocatealpha($src_im, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha); 

      // set pixel with the new color + opacity 

      if (!imagesetpixel($src_im, $x, $y, $alphacolorxy)) { 
       return false; 
      } 
     } 
    } 

    // The image copy 

    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); 
} 

?> 
+0

によって置き換えられるべきであるあなたは見つけることが[anti-aliasing](http://en.wikipedia.org/wiki/Anti-aliasing)を検索すると便利です - あなたが見ている[jaggies](http://en.wikipedia.org/wiki/Jaggies) [エイリアシング](http://en.wikipedia.org/wiki/Aliasing)によるものです。 –

答えて

2

私は古いプロジェクトでこれを持っていたし、私のために働きました。私のPNG画像が本当にアルファ透明度または「GIF」、透明性を持っていた場合とにかく、私は覚えていない...

$image = imagecreatefrompng($href); 
$newImage = imagecreatefrompng($href2); 
imagealphablending($image, true); 
imagealphablending($newImage, true); 
$newImage = imagerotate($newImage, $r, -1); 
$blue = imagecolorallocate($newImage, 0, 0,255); 
imagecolortransparent($newImage, $blue); 
imagecopy($image,$newImage,$x,$y,0,0,imagesx($newImage) , imagesy($newImage)); 

マニュアルから、その場合には、imagealphablendingimagesavealpha