php - 上传PNG图像是黑色背景
问题描述
我正在从 excel 文件中的链接中获取 png 图像,并使用文件夹路径将其上传到数据库中。问题是它以黑色背景出现,尽管原始图像是透明的。
这是代码
$imageSource = file_get_contents($obj->ProductImageLink);
$uploadedFile = file_put_contents($imageAbsolutePath, $imageSource);
if (filesize($imageAbsolutePath) != 0) {
try{
$image = new ImageResize($imageAbsolutePath);
$image->resizeToHeight(600);
$image->save($imageAbsolutePath, $obj->ImageType);
$image->resizeToHeight(250);
$image->save($imageAbsolutePathThumb, $obj->ImageType);
unset($image);
}
我正在使用图像调整器,对于 PNG 我有以下
case IMAGETYPE_PNG:
if (!$this->quality_truecolor && !imageistruecolor($this->source_image)) {
$dest_image = imagecreate($this->getDestWidth(), $this->getDestHeight());
$background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1);
imagecolortransparent($dest_image, $background);
imagefill($dest_image, 0, 0, $background);
} else {
$dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
}
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
break;
}
我试图改变$background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1); to imagecolorallocatealpha($dest_image, 255, 255, 255);
或不同的组合,但没有任何效果。
解决方案
推荐阅读
- c++ - OpenSSL 的 EVP_DigestSignFinal() 创建无效签名
- python - Python:以特定顺序提取给定数组的元素
- postgresql - postgres pg_dump 数据库备份
- swift - kotlin 中的 WebSockets
- python - 从 python 中访问 tensorflow/core/framework/cpu_allocator_impl.cc
- c# - 在 ActionFilterAttribute 中查找过滤器
- python - 在字典列表中查找键值,然后替换其他值
- python - 我想根据行内的条件从烧瓶 db.execute 查询中删除一行
- python - 尝试使用谷歌工作表的电子表格().batchUpdate()时“收到无效的 JSON 有效负载”
- java - Base64 错误:javax.xml.bind.DatatypeConverter.parseBase64Binary 处的 java.lang.NullPointerException(DatatypeConverter.java:296)