php - 上传PNG到网站服务器

标签 php mysql image png

我看过很多其他主题,并且都尝试过。有人可以帮忙解释一下为什么这个脚本不能上传 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_imageimagecopyresampled() 中。

我也得到了纯白色图像,但之后我得到了预期的结果:

$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/

相关文章:

php - 如何优化我当前的Mysql查询

mysql - 未获得按日期时间进行 Rank() 分区的正确排名

mysql - 如何使用 docker 容器将后端和前端的 WordPress 分开?

android - 如何在 Android 中减小 JPEG 图像的大小

ruby-on-rails - 使用回形针保存从 api 获得的 base64 图像

php - 按月份分组并按时间顺序而不是按字母顺序在 mysql 中显示

php - 从 MySQL 数据库循环 CURL 脚本中的 FCM token

php - 当关系数据库中的 A 列相同时,如何对 B 列求和

php - 当mysql查询结果没有值时在PHP中显示消息

image - 从图像中删除背景灰色像素