1

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:

How do I resize pngs with transparency in PHP?

Resize images with transparency in php

Community
  • 1
  • 1
NealVDV
  • 2,302
  • 3
  • 26
  • 51
  • 1. Why are you outputting image headers, 2. Why is there such a big difference in the paths you are using for the png and the jpg images and 3. What is the return value of `imagepng()`? – jeroen Dec 06 '16 at 10:31
  • @jeroen I thought that the image header were a must when saving a file, difference in paths is just because of the extensions ".png / .jpg" (will rewrite once it works). How do I get a return value of the `imagepng()`? This code is called by ajax call. – NealVDV Dec 06 '16 at 11:37
  • 1
    As you ajax call will expect text / html / json back, you should not set an image header, you only do that when you want to output an image directly from the script to the browser. As your code is pretty repetitive, I would use the same code for jpg's and png's so that you can easily narrow down where the problem is. – jeroen Dec 06 '16 at 12:00
  • @jeroen see my own answer for the solution, thanks for helping – NealVDV Dec 10 '16 at 12:14

1 Answers1

0

Looking at the ajax request response I saw that the error was caused by the difference between imagepng and imagejpeg when it comes to the third parameter quality.

imagejpeg is from 0 - 100 (best quality) and imagepng is 0 - 9 (highly compressed).

NealVDV
  • 2,302
  • 3
  • 26
  • 51