php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81321 incorrect image on save
Submitted: 2021-08-01 21:30 UTC Modified: 2021-08-02 08:36 UTC
From: andrei at anleaf dot com Assigned: cmb (profile)
Status: Not a bug Package: GD related
PHP Version: 7.4.22 OS: CentOs 7.9
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: andrei at anleaf dot com
New email:
PHP Version: OS:

 

 [2021-08-01 21:30 UTC] andrei at anleaf dot com
Description:
------------
problem with opening and saving some png images using imagecreatefrompng() and then save it using imagepng($image,$file).
Image opens OK but when saved, the resulting image has broken alpha channel.
Happens with some png images.

Input image to test is here https://www.dropbox.com/s/c6ne72bobrt2iz9/logo_orig.png?dl=0

Resulting image with problem is here https://www.dropbox.com/s/xs4b4bv6208hiua/logo_new.png?dl=0


Test script:
---------------
$im = imagecreatefrompng("logo_orig.png");
imagepng($im,"logo_new.png");
imagedestroy($im);

Expected result:
----------------
saved image should look the same as an input image

Actual result:
--------------
saved image is completely distorted.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-02 08:00 UTC] andrei at anleaf dot com
One more png with the problem
https://www.dropbox.com/s/1kuirhjgvnfcohs/logo_orig-2.png?dl=0

Its conversion result (wrong) is here
https://www.dropbox.com/s/98iklpacpm5ppot/logo-orig-2_new.png?dl=0
 [2021-08-02 08:36 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Package: *Graphics related +Package: GD related -Assigned To: +Assigned To: cmb
 [2021-08-02 08:36 UTC] cmb@php.net
You need to preserve the alpha channel explicitly by calling
imagesavealpha()[1], e.g.

<?php
$im = imagecreatefrompng("orig.png");
imagesavealpha($im, true);
imagepng("new.png");
?>

[1] <https://www.php.net/imagesavealpha>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 13 02:01:28 2025 UTC