1

I found the solution to my problem. See below the original post and completely at the bottom my solution. I made a stupid mistake :)


First I crop an image and then save it to a png file. Right after this, I also show the image.
However, the saved png does not have transparency and the shown one has. What is going on?

$this->resource = imagecreatefrompng($this->url);

imagealphablending($this->resource, false);
imagesavealpha($this->resource, true);

$newResource = imagecreatetruecolor($destWidth, $destHeight);
imagealphablending($newResource, false);
imagesavealpha($newResource, true);

$resample = imagecopyresampled($newResource,$this->resource,0,0,$srcX1,$srcY1,$destWidth,$destHeight,$srcX2-$srcX1, $srcY2-$srcY1);
imagedestroy($this->resource);
$this->resource = $newResource;

// SAVING
imagepng($this->resource, $destination, 100);

// SHOWING
header('Content-type: image/png');
imagepng($this->resource);


The reason I also save the image is for caching. If the script is executed on a png, it saves a cached png.
Next time the image is requested, the png file will be shown, but it has lost its transparency.

Even stranger:

  • When I save that cached png image as (within Firefox), it saves it suddenly as a jpg, even though the extension was png.
  • Downloading the cached png using chrome and opening it in Photoshop gives the error: "file-format module cannot parse the file".

Once I try to show that saved PNG with the GD library, it gives me an error.


SOLUTION

I found out what the problem was. It was a stupid mistake. The script I provided above were cut out of a class and placed as sequential code, while in real this is not what exactly happened. The save image function:

function saveImage($destination,$quality = 90)
{
    $this->loadResource();
    switch($extension){
        default:
        case 'JPG': 
        case 'jpg': 
            imagejpeg($this->resource, $destination, $quality);
            break;
        case 'gif': 
            imagegif($this->resource, $destination);
            break;
        case 'png': 
            imagepng($this->resource, $destination);
            break;
        case 'gd2': 
            imagegd2($this->resource, $destination);
            break;
    }
}

However... $extension does not exist. I fixed it by adding:

$extension = $this->getExtension($destination);
  • You'd be better off simply doing `readfile($destination)` instead of making your server do the PNG compression routines twice. – Marc B Nov 13 '11 at 03:34
  • Thanks for your answer. That is not the problem here. The saved PNG does not have a transparency layer and can't be opened in photoshop for instance. I added a link in my post to show what I mean. I hope this makes it clearer... How do I reopen my question? It is not a duplicate.. – Harry Stroker Nov 13 '11 at 09:10
  • Yes you're right this is isn't a duplicate so...I can re-open this provided that you move your solution into a self answer. Also mention at the start of this question that you reviewed the supposed dupe and explain as you did why it doesn't apply. Then flag for a mod to re-open. – Kev Nov 13 '11 at 14:04
  • Sure thing. I will do that. Thank you. I already flagged for re-opening yesterday though :) – Harry Stroker Nov 14 '11 at 08:16

0 Answers0