I am using PHP to handle an .png file upload which gets cropped and then uploaded. For whatever reason my first attempt was swapping the transparent with black. After reading multiple stack overflow questions and trying solutions I am now at the stage where the file gets uploaded but is unreadable and 0 bytes. What is going wrong? (i am following exact answers from other questions...)
The .jpeg is working just fine, the .png not so
Code I am using:
$dest_image = ImageCreateTrueColor($target_width, $target_height);
switch ($search->found_extension()) {
case 'PNG':
case 'png':
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
$source_image = imagecreatefrompng($image);
$transparent = imagecolorallocatealpha($dest_image, 255, 255, 255, 127);
imagefilledrectangle($dest_image, 0, 0, $target_width, $target_height, $transparent);
imagecopyresampled($dest_image, $source_image, 0, 0, $x, $y, $target_width, $target_height, $w, $h);
header('Content-type: image/png');
imagepng($dest_image, "../../" . $folder . "/" . cleanstring($userdata->id) . $hasher . ".png", $quality);
break;
case 'jpg':
case 'jpeg':
case 'JPG':
case 'JPEG':
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
$source_image = imagecreatefromjpeg($image);
imagecopyresampled($dest_image, $source_image, 0, 0, $x, $y, $target_width, $target_height, $w, $h);
header('Content-type: image/jpeg');
imagejpeg($dest_image, "../../" . $filename, $quality);
break;
}
Tried solutions: