簡體   English   中英

裁剪和上傳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;
        }

嘗試的解決方案:

如何在PHP中透明地調整png的大小?

在php中使用透明度調整圖像大小

查看ajax請求響應,我發現錯誤是由imagepngimagejpeg之間的差異引起的,當涉及到第三個參數quality

imagejpeg是0 - 100(最佳質量), imagepng是0 - 9(高度壓縮)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM