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

时间:2016-12-06 10:07:06

标签: php image png

我正在使用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

1 个答案:

答案 0 :(得分:0)

查看ajax请求响应我发现错误是由imagepngimagejpeg之间的差异导致的第三个参数quality

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