Note that there are some explanatory texts on larger screens.

plurals
  1. POMy PNG has transparency, but after saving with PHP GD, transparency is lost
    primarykey
    data
    text
    <p>I found the solution to my problem. See below the original post and completely at the bottom my solution. I made a stupid mistake :)</p> <hr> <p>First I crop an image and then save it to a png file. Right after this, I also show the image.<br> However, the saved png does not have transparency and the shown one has. What is going on?</p> <pre><code>$this-&gt;resource = imagecreatefrompng($this-&gt;url); imagealphablending($this-&gt;resource, false); imagesavealpha($this-&gt;resource, true); $newResource = imagecreatetruecolor($destWidth, $destHeight); imagealphablending($newResource, false); imagesavealpha($newResource, true); $resample = imagecopyresampled($newResource,$this-&gt;resource,0,0,$srcX1,$srcY1,$destWidth,$destHeight,$srcX2-$srcX1, $srcY2-$srcY1); imagedestroy($this-&gt;resource); $this-&gt;resource = $newResource; // SAVING imagepng($this-&gt;resource, $destination, 100); // SHOWING header('Content-type: image/png'); imagepng($this-&gt;resource); </code></pre> <p><br> The reason I also save the image is for caching. If the script is executed on a png, it saves a cached png.<br> Next time the image is requested, the png file will be shown, but it has lost its transparency.</p> <p>Even stranger:</p> <ul> <li>When I save that cached png image as (within Firefox), it saves it suddenly as a jpg, even though the extension was png.</li> <li>Downloading the cached png using chrome and opening it in Photoshop gives the error: <code>"file-format module cannot parse the file"</code>.</li> </ul> <p>Once I try to show that saved PNG with the GD library, it gives me an error.</p> <hr> <p><strong>SOLUTION</strong></p> <p>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:</p> <pre><code>function saveImage($destination,$quality = 90) { $this-&gt;loadResource(); switch($extension){ default: case 'JPG': case 'jpg': imagejpeg($this-&gt;resource, $destination, $quality); break; case 'gif': imagegif($this-&gt;resource, $destination); break; case 'png': imagepng($this-&gt;resource, $destination); break; case 'gd2': imagegd2($this-&gt;resource, $destination); break; } } </code></pre> <p>However... $extension does not exist. I fixed it by adding:</p> <pre><code>$extension = $this-&gt;getExtension($destination); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload