gpt4 book ai didi

php - 创建缩略图时背景变黑

转载 作者:行者123 更新时间:2023-12-03 19:34:10 27 4
gpt4 key购买 nike

我有以下脚本来创建缩略图,工作正常!但是,当我上传具有透明背景的 PNG 文件时,由于某种原因,背景变为黑色。

怎么修?我需要做什么?

这是我的代码

<?php
function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){

//folder path setup
$target_path = $target_folder;
$thumb_path = $thumb_folder;

//file name setup
$filename_err = explode(".",$_FILES[$field_name]['name']);
$filename_err_count = count($filename_err);
$file_ext = $filename_err[$filename_err_count-1];
if($file_name != ''){
$fileName = $file_name.'.'.$file_ext;
}else{
$fileName = $_FILES[$field_name]['name'];
}

//upload image path
$upload_image = $target_path.basename($fileName);

//upload image
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
{
//thumbnail creation
if($thumb == TRUE)
{
$thumbnail = $thumb_path.$fileName;
list($width,$height) = getimagesize($upload_image);
$thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
switch($file_ext){
case 'jpg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'jpeg':
$source = imagecreatefromjpeg($upload_image);
break;

case 'png':
$source = imagecreatefrompng($upload_image);
break;
case 'gif':
$source = imagecreatefromgif($upload_image);
break;
default:
$source = imagecreatefromjpeg($upload_image);
}

imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);

switch($file_ext){
case 'jpg' || 'jpeg':
imagejpeg($thumb_create,$thumbnail,100);
break;
case 'png':
imagepng($thumb_create,$thumbnail,100);
break;

case 'gif':
imagegif($thumb_create,$thumbnail,100);
break;
default:
imagejpeg($thumb_create,$thumbnail,100);
}

}

return $fileName;
}
else
{
return false;
}
}
if(!empty($_FILES['image']['name'])){

//call thumbnail creation function and store thumbnail name
$upload_img = cwUpload('image','uploads/','',TRUE,'uploads/thumbs/','200','160');

//full path of the thumbnail image
$thumb_src = 'uploads/thumbs/'.$upload_img;

//set success and error messages
$message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";

}else{

//if form is not submitted, below variable should be blank
$thumb_src = '';
$message = '';
}

?>

<form method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="submit" value="Upload"/>
</form>

<?php if($thumb_src != ''){ ?>
<img src="<?php echo $thumb_src; ?>" alt="">
<?php } ?>

在上面的代码中使用 JPG 和其他扩展名,但是当我上传透明图像时,背景变黑了

enter image description here

我做错了什么?

帮我

提前致谢。

最佳答案

试试这个 PNG 代码

<?php
function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){

//folder path setup
$target_path = $target_folder;
$thumb_path = $thumb_folder;

//file name setup
$filename_err = explode(".",$_FILES[$field_name]['name']);
$filename_err_count = count($filename_err);
$file_ext = $filename_err[$filename_err_count-1];
if($file_name != ''){
$fileName = $file_name.'.'.$file_ext;
}else{
$fileName = $_FILES[$field_name]['name'];
}

//upload image path
$upload_image = $target_path.basename($fileName);

//upload image
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
{
//thumbnail creation
if($thumb == TRUE)
{
$thumbnail = $thumb_path.$fileName;
list($width,$height) = getimagesize($upload_image);

$thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
imagealphablending($thumb_create, false);
imagesavealpha($thumb_create, true);
$trans_layer_overlay = imagecolorallocatealpha($thumb_create, 220, 220, 220, 127);
imagefill($thumb_create, 0, 0, $trans_layer_overlay);
switch($file_ext){
case 'png':
$source = imagecreatefrompng($upload_image);
break;
}

imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);

switch($file_ext){
case 'png':
imagepng($thumb_create,$thumbnail);
break;
}

}

return $fileName;
}
else
{
return false;
}
}
if(!empty($_FILES['image']['name'])){

//call thumbnail creation function and store thumbnail name
$upload_img = cwUpload('image','uploads/','',TRUE,'uploads/thumbs/','200','160');

//full path of the thumbnail image
$thumb_src = 'uploads/thumbs/'.$upload_img;

//set success and error messages
$message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";

}else{

//if form is not submitted, below variable should be blank
$thumb_src = '';
$message = '';
}

?>

<form method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="submit" value="Upload"/>
</form>

<?php if($thumb_src != ''){ ?>
<img src="<?php echo $thumb_src; ?>" alt="">
<?php } ?>

当您上传透明图像时它会起作用。

关于php - 创建缩略图时背景变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51587209/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com