我看过很多其他主题,并且都尝试过。有人可以帮忙解释一下为什么这个脚本不能上传 PNG 文件吗?正在显示空白 PNG 图像。
$image = $_FILES['file']['tmp_name'];
$image_name = $_FILES['file']['name'];
$ext = pathinfo($image_name, PATHINFO_EXTENSION);
$location = "Profiles/{$user}/Picture/{$image_name}";
$new_image = imagecreatetruecolor(100, 100);
$source_image = imagecreatefrompng($image);
imagealphablending($source_image, false);
imagesavealpha($source_image, true);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, 100, 100, $image_width, $image_height);
imagepng($new_image, '../' . $location, 9);
最佳答案
您没有声明 $image_width
或 $image-height
并且您引用的是 $image
而不是 $source_image
在 imagecopyresampled()
中。
我也得到了纯白色图像,但之后我得到了预期的结果:
$image = $_FILES['file']['tmp_name'];
$image_name = $_FILES['file']['name'];
$ext = pathinfo($image_name, PATHINFO_EXTENSION);
$location = "Profiles/{$user}/Picture/{$image_name}";
$new_image = imagecreatetruecolor(100, 100);
$source_image = imagecreatefrompng($image);
// Get the width & height of the uploaded image.
$image_width = imagesx($source_image);
$image_height = imagesy($source_image);
imagealphablending($source_image, false);
imagesavealpha($source_image, true);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $source_image, 0, 0, 0, 0, 100, 100, $image_width, $image_height);
imagepng($new_image, '../' . $location, 9);
关于php - 上传PNG到网站服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006615/