English 中文(简体)
PHP png: 透明度转化为明显的肤色。 如何避免?
原标题:PHP png: transparency converted to visible color. How to avoid it?

当我试图改变Png->png with resizing I got transparency presented as specific则:#ff00ff;

结果是:

https://i.stack.imgur.com/1xTd5.png
Here is the code:

<?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);

How to solve this probled? Glad to get any help. Thanks.

P.S. Sorry for my English.

问题回答

After trying and trying, it is the solution: imagecopymerge() Explain me someone, why?

<?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 );




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签