简体   繁体   English

将PNG上传到网站服务器

[英]Uploading PNG to Website Server

I have seen loads of other topics and I've tried them all. 我看到了很多其他主题,并且都尝试了全部。 Can someone please help with why this script won't upload PNG files? 有人可以帮忙为什么这个脚本不能上传PNG文件吗? Blank PNG image being displayed. 正在显示空白的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);

You're not declaring $image_width or $image-height and you are referencing $image instead of $source_image in imagecopyresampled() . 您没有声明$image_width$image-height ,而是在imagecopyresampled()中引用$image而不是$source_image

I too was getting a plain white image, but after this I get the expected result: 我也得到了纯白色的图像,但是在此之后,我得到了预期的结果:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM