裁剪和上传 PNG 文件创建不可读文件

Cropping and uploading PNG file creating unreadable file

我正在使用 PHP 处理 .png 文件上传,该文件被裁剪然后上传。不管出于什么原因,我的第一次尝试是将透明色换成黑色。在阅读了多个堆栈溢出问题并尝试了解决方案之后,我现在处于文件上传但不可读且为 0 字节的阶段。出了什么问题? (我正在关注其他问题的确切答案...)

.jpeg 可以正常工作,.png 则不然

我使用的代码:

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

尝试过的解决方案:

How do I resize pngs with transparency in PHP?

Resize images with transparency in php

查看ajax请求响应,发现错误是由于第三个参数qualityimagepngimagejpeg的不同造成的。

imagejpeg 是从 0 - 100(最佳质量),imagepng 是 0 - 9(高度压缩)。