【发布时间】:2017-10-16 09:39:48
【问题描述】:
我有以下脚本用于将 JPEG 和 PNG 处理为名为 base.png 的现有 PNG。在函数“transparent_background”中,我用透明度替换了白色背景。这个功能是有问题的。独立的函数直接在浏览器中处理输出。请看注释掉“//imagepng($img);”。但是如果我将 $img 从函数中返回,我认为它仍然是 jpeg,这就是它不透明的原因。第二个功能仅用于调整大小。
<?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 中的图像不透明的原因。有什么想法我可以解决这个问题吗?我尝试过使用 ob_start 和 ob_get_contents 但这也没有用。
【问题讨论】: