首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将PNG上传到网站服务器

将PNG上传到网站服务器
EN

Stack Overflow用户
提问于 2015-06-23 15:09:54
回答 1查看 44关注 0票数 0

我见过很多其他的话题,我都试过了。有人能帮我解释一下为什么这个脚本不会上传PNG文件吗?正在显示空白PNG图像。

代码语言:javascript
复制
$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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-23 15:40:51

您不是在声明$image_width$image-height,而是在imagecopyresampled()中引用$image而不是$source_image

我也得到了一个普通的白色图像,但在这之后我得到了预期的结果:

代码语言:javascript
复制
$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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31006615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档