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

JPGs generated from PNGs by img_to_jpg function aren't deleted when original PNG is deleted #2535

Open
4foot30 opened this issue Feb 2, 2022 · 3 comments
Labels
2.x Future has-patch Image(Helper) Issues related to the Image and ImageHelper classes

Comments

@4foot30
Copy link

4foot30 commented Feb 2, 2022

Expected behaviour

JPGs created from PNGs with a direct call to ImageHelper::img_to_jpg() should be deleted from the uploads directory when the original source PNG is deleted from the WordPress Media library.

Actual behaviour

The generated JPGs remain in the uploads directory.

Steps to reproduce behaviour

I am generating the JPGs via my PHP, not in my Twig templates, and returning the filenames of the newly created JPGs to the Timber context for later output inside a Twig template inside a <picture> tag:

        $resized_image = ImageHelper::resize($image_original_file, $image_width, $image_height, ['center', 'center']);
        if ($to_jpg !== false) {
            $resized_image = ImageHelper::img_to_jpg($resized_image);
        }
        return $resized_image;

Everything works perfectly including the resizing and format conversion, but the converted JPGs are not deleted when the original PNG is. If I try the same code using an original JPG, with no need to call ImageHelper::img_to_jpg() then the resized JPGs are correctly deleted when the original source JPG is deleted from the Media library. The problem seems to only occur when a file format conversion has happened.

Basically I've got all the same symptoms as this issue: #894 - but I'm converting the image type using ImageHelper::img_to_jpg() in the PHP rather than the |tojpg filter in Twig. I can see that I'm using a new enough Timber version that it includes the fixes from #894 and #897 but I haven't had any luck debugging TimberImageHelper::delete_generated_files.

If it helps, I've used Timber and my resize/convert code in the past on other sites (I think WP 5.7 or so) and had confirmed that the converted JPGs were correctly deleted - but it's failing for me on the latest versions of Timber and WordPress on my local as well as on WPEngine hosting.

Timber is superb btw, so thank you! :)

What version of WordPress, PHP and Timber are you using?

WordPress 5.9, PHP 7.4.1, Timber 1.19

How did you install Timber? (for example, from GitHub, Composer/Packagist, WP.org?)

From Composer, via composer require wpackagist-plugin/timber-library

@4foot30 4foot30 changed the title img_to_jpg function doesn't delete JPGs generated from PNGs when original PNG is deleted JPGs generated from PNGs by img_to_jpg function aren't deleted when original PNG is deleted Feb 2, 2022
@jarednova
Copy link
Member

@4foot30 thanks so much for the great bug report. Are you interested or able to keep going and see if you can debug and bring things to a PR?

@jarednova jarednova added the help wanted Do you know computer? Then lend a hand and have your code live in infamy! label Feb 2, 2022
@gchtr
Copy link
Member

gchtr commented Mar 2, 2022

I am generating the JPGs via my PHP, not in my Twig templates

I shouldn’t matter whether you do it from Twig or from PHP, because Twig directly passes on the result directly to Timber\ImageHelper::img_to_jpg().

@jarednova I see that there were not tests added in #897, so maybe this is a regression bug. We should probably first add a test confirm this issue.

@Levdbas Levdbas added the Image(Helper) Issues related to the Image and ImageHelper classes label May 13, 2023
@Levdbas Levdbas added has-patch and removed help wanted Do you know computer? Then lend a hand and have your code live in infamy! labels May 25, 2023
@Levdbas
Copy link
Member

Levdbas commented May 25, 2023

Tested this issue and #2556 fixes it. Added has patch tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x Future has-patch Image(Helper) Issues related to the Image and ImageHelper classes
Projects
None yet
Development

No branches or pull requests

4 participants