php - 广东!将 png 图像转换为 jpeg 并使 alpha 默认为白色而不是黑色

标签 php image-processing image-manipulation gd

我试过类似的东西,但它只是使图像的背景变白,不一定是图像的 alpha。我只想将所有内容都上传为 jpg,所以如果我能以某种方式“展平”带有一些透明的 png 图像以默认它只是白色,那么我可以将它用作 jpg 而不是。感谢任何帮助。谢谢。

$old = imagecreatefrompng($upload);
$background = imagecolorallocate($old,255,255,255);
imagefill($old, 0, 0, $background);
imagealphablending($old, false);
imagesavealpha($old, true);

最佳答案

<?php
$input_file = "test.png";
$output_file = "test.jpg";

$input = imagecreatefrompng($input_file);
$width = imagesx($input);
$height = imagesy($input);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output,  255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);

关于php - 广东!将 png 图像转换为 jpeg 并使 alpha 默认为白色而不是黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2569970/

相关文章:

PHP preg_replace 将 $ 转换为字母 S

javascript - PHP/Javascript 变量传递——意想不到的结果

php - CloudConvert API v2 任务字段为必填项

python - SJ4000 + OpenCV + Raspberry Pi 图像处理

c++ - 使蒙版区域透明?

php - soap.wsdl_cache_enabled

c++ - C++字符串中的ROS cv_bridge ImagePtr转换

python - OpenCV 匹配模板

image-manipulation - 如何替换 cloudinary 中的图像并保持相同的 URL?

javascript - 使用 Javascript 进行图像处理?