I'm resizing and saving PNGs with PHP. The first example doesn't work, the PNG ends up being completely black, but the second example works. The first example doesn't have a transparent background but the second one does. Why doesn't the first example work? Is it a problem with the URL?
<?php
$id = "example";
//$originalFile = "https://th.bing.com/th/id/OIP.v_YT6iKMW6sOOVdCxVYQkwHaE8?pid=ImgDet&rs=1.png"; // Doesn't work
$originalFile = "https://cdn.globalxetfs.com/content/files/210618-China_Materials_02.png"; // Works
list($originalWidth, $originalHeight) = getimagesize($originalFile);
$originalImage = @imagecreatefrompng($originalFile);
$newHeight = 255;
$newWidth = 255;
// Create empty canvas
$resizedImage = "";
$resizedImage = imagecreatetruecolor($newWidth, $newHeight); // width, height
// Preserve transparency
imagesavealpha($resizedImage, true);
$color = imagecolorallocatealpha($resizedImage, 0, 0, 0, 127);
imagefill($resizedImage, 0, 0, $color);
// Resize image
imagecopyresampled(
$resizedImage, $originalImage, 0, 0, 0, 0,
$newWidth, $newHeight, $originalWidth, $originalHeight
);
header('Content-type: image/png');
$imageName = $id;
$imageName = $imageName . ".png";
if(imagepng($resizedImage, "images/" . $imageName)){
echo "Image uploaded";
}
?>
My Answer:
The issue with the first example where the PNG ends up being completely black could be due to the transparency of the original image.
When you create the resized image using `imagecreatetruecolor`, you need to preserve the transparency of the original image. In the first example, the original image might not have transparency, so when you try to preserve transparency and fill the resized image with a transparent color, it might result in a black image.
To fix this issue, you can check if the original image has an alpha channel (transparency) before trying to preserve transparency. You can use the `imagecolortransparent` function to set a specific color as transparent if the original image doesn't have an alpha channel.
Here is an updated version of your code with this check:
php
$id = "example";
//$originalFile = "https://th.bing.com/th/id/OIP.v_YT6iKMW6sOOVdCxVYQkwHaE8?pid=ImgDet&rs=1.png"; // Doesn't work
$originalFile = "https://cdn.globalxetfs.com/content/files/210618-China_Materials_02.png"; // Works
list($originalWidth, $originalHeight) = getimagesize($originalFile);
$originalImage = @imagecreatefrompng($originalFile);
$newHeight = 255;
$newWidth = 255;
// Create empty canvas
$resizedImage = imagecreatetruecolor($newWidth, $newHeight); // width, height
// Check if original image has alpha channel
if(imageistruecolor($originalImage) || imagecolortransparent($originalImage) >= 0) {
imagesavealpha($resizedImage, true);
$color = imagecolorallocatealpha($resizedImage, 0, 0, 0, 127);
imagefill($resizedImage, 0, 0, $color);
}
// Resize image
imagecopyresampled(
$resizedImage, $originalImage, 0, 0, 0, 0,
$newWidth, $newHeight, $originalWidth, $originalHeight
);
header('Content-type: image/png');
$imageName = $id;
$imageName = $imageName . ".png";
if(imagepng($resizedImage, "images/" . $imageName)){
echo "Image uploaded";
}
?>
By adding the check for alpha channel and setting transparency only if the original image has it, you should be able to resize and save PNG images correctly, even if they don't have a transparent background.
Rate this post
3 of 5 based on 1224 votesComments