Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Compress image manipulation removes png image transparency #4178

Closed
ivanz040592 opened this issue Jan 29, 2021 · 2 comments · Fixed by #5971
Closed

Bug: Compress image manipulation removes png image transparency #4178

ivanz040592 opened this issue Jan 29, 2021 · 2 comments · Fixed by #5971
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@ivanz040592
Copy link

ivanz040592 commented Jan 29, 2021

I am using compress image from codeigniter 4 library with below code :

\Config\Services::image()->withFile($this->request->getFile('storeimage'))->reorient()->save('images/store/test.png', 70)

But it removes transparency png image. Can i compress png image without losses the transparency in ci 4?

I put below code in GDHanlder.php (save function) but it makes compress image not working, image size result still same and not compressed

imageAlphaBlending($this->resource, true);
imageSaveAlpha($this->resource, true);
case IMAGETYPE_PNG:
    imageAlphaBlending($this->resource, true);
    imageSaveAlpha($this->resource, true);

    if (! function_exists('imagepng'))
    {
        throw ImageException::forInvalidImageCreate(lang('images.pngNotSupported'));
    }

    if (! @imagepng($this->resource, $target))
    {
        throw ImageException::forSaveFailed();
    }
    break;
@ivanz040592 ivanz040592 added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 29, 2021
@kenjis
Copy link
Member

kenjis commented May 6, 2022

@ivanz040592
Sorry for the delay in responding.

Try this:

--- a/system/Images/Handlers/GDHandler.php
+++ b/system/Images/Handlers/GDHandler.php
@@ -251,6 +251,9 @@ class GDHandler extends BaseHandler
                     throw ImageException::forInvalidImageCreate(lang('Images.pngNotSupported'));
                 }
 
+                imagealphablending($this->resource, false);
+                imagesavealpha($this->resource, true);
+
                 if (! @imagepng($this->resource, $target)) {
                     throw ImageException::forSaveFailed();
                 }

@kenjis
Copy link
Member

kenjis commented May 7, 2022

This bug was fixed in the develop branch.
And it would be included in v4.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants