2012-01-12 9 views
1

私はサイズ変更でpng - > pngを変換しようとすると、私は具体的な色として提示された透明度を得ました:#ff00ff;ここでPHP png:透明度を可視色に変換しました。それを避ける方法は?

は結果である:ここでは

http://i.stack.imgur.com/1xTd5.png
コードです:

<?php 

$image = __DIR__ . '/image/data/10891.png'; // local file 
$image = 'http://content.wuala.com/contents/mike.walker/sharing/10891.png?dl=1'; // source file in public 
$scale = 1; 

// Get meta for image 
$info = getimagesize($image); 
$info = array(
    'width' => $info[ 0 ], 
    'height' => $info[ 1 ], 
    'bits' => $info[ 'bits' ], 
    'mime' => $info[ 'mime' ] 
); 

// Get res for image 
$image = imagecreatefrompng($image); 

$width = $info['width'] * $scale; 
$height = $info['height'] * $scale; 

$new_width = (int)($info[ 'width' ] * $scale); 
$new_height = (int)($info[ 'height' ] * $scale); 
$xpos  = (int)(($width - $new_width)/2); 
$ypos  = (int)(($height - $new_height)/2); 

$image_old = $image; 
$image  = imagecreatetruecolor($width, $height); 

//imagetruecolortopalette($image, FALSE, 255); 
imagealphablending($image, false); 
imagesavealpha($image, true); 
$background = imagecolorallocatealpha($image, 255, 255, 255, 128); 
imagecolortransparent($image, $background); 

imagefilledrectangle($image, 0, 0, $width, $height, $background); 
imagecopyresampled($image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $info[ 'width' ], $info[ 'height' ]); 

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

このprobledを解決する方法は? 助けを借りてうれしいです。ありがとう。

P.S.私の英語には申し訳ありません。

答えて

0

試した後、それが解決策です:imagecopymerge() 誰かを説明してください、なぜですか?

<?php 

$image = 'http://content.wuala.com/contents/mike.walker/sharing/10891.png?dl=1'; 

// ---------------------------------------- Prepare 

$scale = 1; 

// Get meta for image 
$info = getimagesize($image); 
$info = array(
    'width' => $info[ 0 ], 
    'height' => $info[ 1 ], 
    'bits' => $info[ 'bits' ], 
    'mime' => $info[ 'mime' ] 
); 

// Get res for image 
$image = imagecreatefrompng($image); 

$width = $info[ 'width' ] * $scale; 
$height = $info[ 'height' ] * $scale; 

$new_width = (int)($info[ 'width' ] * $scale); 
$new_height = (int)($info[ 'height' ] * $scale); 
$xpos  = (int)(($width - $new_width)/2); 
$ypos  = (int)(($height - $new_height)/2); 

$image_old = $image; 
$image  = imagecreatetruecolor($width, $height); 
$image2  = imagecreatetruecolor($width, $height); 

//imagetruecolortopalette($image, FALSE, 255); 
imagealphablending($image, true); 
imagesavealpha($image, true); 
$background = imagecolorallocatealpha($image, 255, 255, 255, 0); 
//$background = imagecolortransparent($image, imagecolorexact($image, 254, 0, 254)); 
//$background = imagecolorallocate($image, 254, 0, 3); 
imagecolortransparent($image2, $background); 

imagefilledrectangle($image, 0, 0, $width, $height, $background); 
//imagefilledrectangle($image2, 0, 0, $width, $height, $background); 
imagecopymerge($image, $image_old, 0, 0, 0, 0, $new_width, $new_height, 100); 
imagecopyresampled($image2, $image, $xpos, $ypos, 0, 0, $new_width, $new_height, $info[ 'width' ], $info[ 'height' ]); 
//imagecopyresampled($image2, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $info[ 'width' ], $info[ 'height' ]); 

// ------------------------------------------------ Convert 

$scale = .9; 

$width = $info[ 'width' ] * $scale; 
$height = $info[ 'height' ] * $scale; 

$new_width = (int)($info[ 'width' ] * $scale); 
$new_height = (int)($info[ 'height' ] * $scale); 
$xpos  = (int)(($width - $new_width)/2); 
$ypos  = (int)(($height - $new_height)/2); 

$image_old = $image2; 
$image  = imagecreatetruecolor($width, $height); 

//imagetruecolortopalette($image, FALSE, 255); 
imagealphablending($image, true); 
imagesavealpha($image, true); 
$background = imagecolorallocatealpha($image, 255, 0, 255, 0); 
//$background = imagecolortransparent($image, imagecolorexact($image, 254, 0, 254)); 
//$background = imagecolorallocate($image, 254, 0, 3); 
imagecolortransparent($image2, $background); 

//imagefilledrectangle($image2, 0, 0, $width, $height, $background); 
//imagefilledrectangle($image2, 0, 0, $width, $height, $background); 
//imagecopymerge($image, $image_old, 0, 0, 0, 0, $new_width, $new_height, 100); 
//imagecopyresampled($image2, $image, $xpos, $ypos, 0, 0, $new_width, $new_height, $info[ 'width' ], $info[ 'height' ]); 
imagecopyresampled($image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $info[ 'width' ], $info[ 'height' ]); 

header('Content-type: image/png'); 
imagepng($image); 
//imagepng($image2, __DIR__ . '/resized.png'); 
+0

私がよく覚えているのであれば、imagecopymergeはうまくいきません。*画像のマージにノイズがあります。 – nulll

関連する問題