2011-04-16 17 views
18

我正在使用允许用户上传图片的脚本。脚本调整大小并将图像转换为JPEG。如何在用透明背景调整/转换PNG图像为JPEG格式时用白色替换黑色背景。

我遇到的问题是上传带有透明度的PNG时,得到的JPEG图像在黑色透明处出现。

如何编辑下面的脚本来替换黑色与白色?它已经为GIF做了这个,但不是PNG的。

// RESIZE IMAGE AND PUT IN USER DIRECTORY 
    switch($this->file_ext) 
{ 
    case "gif": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefromgif($this->file_tempname); 
     $kek=imagecolorallocate($file, 255, 255, 255); 
     imagefill($file,0,0,$kek); 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 

    case "bmp": 
     $file = imagecreatetruecolor($width, $height); 
     $new = $this->imagecreatefrombmp($this->file_tempname); 
     for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 

    case "jpeg": 
    case "jpg": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefromjpeg($this->file_tempname); 
     for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 

    case "png": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefrompng($this->file_tempname); 
     for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 
    } 

    chmod($photo_dest, 0777); 

    return true; 
} 

我试图编辑的情况下“PNG”:部分以匹配的情况下“GIF”的:代码,但所得到的JPEG完全是白色。

UPDATE:

我固定它自己。

谢谢大家,为了贡献!

我代替:

case "png": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefrompng($this->file_tempname); 
     for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 

有:

case "png": 
     $file = imagecreatetruecolor($width, $height); 
     $new = imagecreatefrompng($this->file_tempname); 
     $kek=imagecolorallocate($file, 255, 255, 255); 
     imagefill($file,0,0,$kek); 
     imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
     imagejpeg($file, $photo_dest, 100); 
     ImageDestroy($new); 
     ImageDestroy($file); 
     break; 
+0

这imagecolorallocate做什么?它为png接收4个参数,不应该为alpha通道接收5个参数吗? – Andre 2011-04-16 19:27:45

+0

我没有写代码,我是小白。我不知道你的问题的答案。抱歉,我无法帮助你帮助我。 – Jeff 2011-04-16 19:43:15

+0

实际上这些都在手册中。 _imagecolorallocate - 返回表示由给定的RGB分量组成的颜色的颜色标识符._对于alpha透明度,您需要使用'imagecolorallocatealpha',它确实会添加第5个参数:http://php.net/manual/en/ function.imagecolorallocatealpha.php – kasimir 2012-12-06 09:57:07

回答

0

好吧,这个功能是从PHP。因为我从来没有在PHP上使用过图片,所以我不知道它。

因此,图像由三种颜色组成:RGB(红色,绿色,蓝色)。在PNG的情况下,它也由另一个过滤器,称为α,也就是透明度

SO,只为PNG,您应该切换到imagecolorallocateimagecolorallocatealpha($file,$i,$i,$i,$i);

这应该使您的图片透明的。这是解决你的问题还是你真的需要他们在白色背景?

编辑: 我还注意到,你在所有情况下都使用imagejpg功能。他们切换到相应的功能,即,imagepng,imagebmp,imagegif

+0

我需要为所有功能保留imagejpeg,因为所有新图像都必须是jpg。我做了你提出的其他修改,但是结果jpg在透明的地方仍然是黑色的。我不需要保持透明度,我只需要将黑色更改为白色。谢谢! – Jeff 2011-04-16 20:33:00

0

尝试这样的(未测试):

case "png": 
    $file = imagecreatetruecolor($width, $height); 
    $new = imagecreatefrompng($this->file_tempname); 
    imagefilledrectangle ($file, 0, 0, $width, $height, imagecolorallocate($file, 0,0,0)) 

(提前绘制在白色背景上的$文件图像)

此外, for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }部分看起来很奇怪。

+0

谢谢,但它没有奏效。 – Jeff 2011-04-16 20:49:20

0
switch($image_extension){ 
    case 'gif': 
    case 'GIF': 
    $image_orig_resource = imagecreatefromgif($image_orig_path); 
    break; 
    case 'png': 
    case 'PNG': 
    $image_orig_resource = imagecreatefrompng($image_orig_path); 
    break; 
    case 'jpg': 
    case 'jpeg': 
    case 'JPG': 
    case 'JPEG': 
    $image_orig_resource = imagecreatefromjpeg($image_orig_path); 
    break; 
    default: 
    throw new Exception('Extension not supported'); 
} 

$image_resource = imagecreatetruecolor($width, $height); 
imagefill($image_resource, 0, 0, imagecolorallocate($image_resource, 255, 255, 255)); // white background; 

imagecopyresampled($image_resource, $image_orig_resource, 0, 0, 0, 0, $width, $height, $image_orig_width, $image_orig_height); 

imagejpeg($image_resource, $image_path, 100); // quality: [0-100] 
0

图片真实色彩后增添线:

$file = imagecreatetruecolor($width, $height); 
    $background = imagecolorallocate($file, 0, 0, 0); 
    imagecolortransparent($file, $background); 
    imagealphablending($file, false); 
    imagesavealpha($file, true); 

这将mailtaining阿尔法所有格式帮助。平如果你没有得到答案。

相关问题