简体   繁体   中英

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? Blank PNG image being displayed.

$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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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