在ecshop或大商创等程序中,上传商品图片如果是Png格式的,生成的缩略图默认为Jpg格式的,如果我们想要使png格式的图片生成的缩略图仍为jpg格式图片,可通过以下方法进行修复:
1、打开网站根目录下的include/cls_image.php文件
2、搜索
function_exists('imagejpeg')
3、将:
function_exists('imagejpeg')
function_exists('imagegif')
function_exists('imagepng')
替换为:
$org_info['mime']=='image/jpeg'
$org_info['mime']=='image/gif'
$org_info['mime']=='image/png'
到这里我们只改了图片的后缀,还不能让png图片达到透明背景的效果,要想达到透明背景,我们还要进行以下操作:
将
$bgcolor = trim($bgcolor, "#"); sscanf($bgcolor, "%2x%2x%2x", $red, $green, $blue); $clr = imagecolorallocate($img_thumb, $red, $green, $blue); imagefilledrectangle($img_thumb, 0, 0, $thumb_width, $thumb_height, $clr);
替换成
$bgcolor = trim($bgcolor, "#"); sscanf($bgcolor, "%2x%2x%2x", $red, $green, $blue); if ($org_info['mime']=='image/png') { $color=imagecolorallocate($img_thumb,$red, $green, $blue); imagecolortransparent($img_thumb,$color); imagefill($img_thumb,0,0,$color); } $clr = imagecolorallocatealpha($img_thumb, $red, $green, $blue,127); imagefilledrectangle($img_thumb, 0, 0, $thumb_width, $thumb_height, $clr);
到这里就可以实现png图片透明背景了。感谢大家对小龙的大力支持,小龙也希望以后能写出更加优秀的技术文章帮到大家。