Learn about Centmin Mod LEMP Stack today
Register Now

Wordpress Optimise Images modification and plugin

Discussion in 'Centmin Mod User Tutorials & Guides' started by ShawnH, Mar 11, 2019.

  1. ShawnH

    ShawnH New Member

    5
    5
    3
    Dec 27, 2016
    Ratings:
    +8
    Local Time:
    2:15 AM
    1.1.16
    10
    I have really been impressed with Centmin Mod and some of the optimizations done, especially the image optimization. Most of the people who I have been working with have no understanding of image size and load speed so the image optimization will be a life saver. The problem I have is that it isn't built into WordPress. I took care of that via a plugin. I also don't like explaining to users how to run command line programs and how to optimize images for the first time so I also fixed that.

    I modified George's optimise-images shell script to search the entire tree for image files, optimize them, and convert to WebP without a depth limit. The script also handles the passing of a single file to convert and optimize. This was a key change to allow a WordPress plugin to call the script with individual filenames. Now, when a user uploads a new media file, the image is optimized and a .webp file created. The additional file sizes that WordPress creates automatically are handled at the same time. Once a user optimizes their WP install they should be kept up to date as new files are added, no more cron jobs. When the image is deleted via WordPress the WebP files are deleted as well.

    I mostly brute forced this by stripping the original script down to install and optimise functions. I haven't fully tested but I am building out a site so it will get tested as I go. I didn't have a clean install to try the install command so you might have to install the original optimise-images first.

    In my rush to get this done I have skipped a few things,
    • It needs to call exec to run the modified shell script
    • The shell script is located in /usr/sbin and needs the correct permissions
    • There is no admin panel optimization parameters outside the shell script
    • No way to set
    • There is no way to click and run the optimizer for the first time on the whole WP structure.
    • The script should really be re-written in PHP and use function calls instead of exec
    • The plugin code is not robust, no error checking, no database information
    I have uploaded to Github (shor0814/image-optimiser-plugin) if anyone wants to use it or fix things. I mostly wrote this for myself but why not share.

    The quick and dirty instructions are in the Readme file.

    Thanks again to George for all of his work, none of this would be possible without it.
     
  2. eva2000

    eva2000 Administrator Staff Member

    44,743
    10,200
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +15,809
    Local Time:
    7:15 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    wow that's for sharing your modifications (y)
    awesome - though the depth limit was so I can control how much is processed as I know some folks have image directories as large as 4+ TB worth of images - that would certainly timeout PHP process and hog all the php-fpm children leaving to the wonderful 502 gateway timeout errors. With that many images in many subdirectories, you can run the original optimise-images.sh script in parallel batches on each subdirectory tailored to your server resources - cpu cores/memory etc.

    exec or shell_exec, if it's shell_exec for centmin mod users that means editing php-fpm.conf config file at /usr/local/etc/php-fpm.conf and commenting out this line
    Code (Text):
    php_admin_value[disable_functions] = shell_exec


    You're welcome and I love it when folks share/show how they are using or modifying my work :) (y)
     
  3. ShawnH

    ShawnH New Member

    5
    5
    3
    Dec 27, 2016
    Ratings:
    +8
    Local Time:
    2:15 AM
    1.1.16
    10
    Thanks for the feedback. Will make some comments to clarify for others if they are interested in using.

    Point well taken. I could put in an option to limit depth based on the command line if it gives anyone troubles. Or they can still use your original. This one was never meant to replace yours, more of a compliment to it for less informed end users.

    This is probably why it will forever live as a shell script. I did background the script in a "fire it off and ignore it" way because it was messing with image uploads and Wordpress thought the upload failed because the conversion took too much time. Backgrounding took care of that. If I ever add an admin tool to index everything I will have to monitor the process and update the dashboard.

    exec. I was fighting shell_exec for a bit, I never got any errors in the error log but it was late and I don't remember if that happened before or after I realized I was missing execute permissions on the shell script.

    Code is never "done" is it.
     
  4. elargento

    elargento Member

    348
    17
    18
    Jan 4, 2016
    Ratings:
    +43
    Local Time:
    6:15 AM
    10
    This explanation isn't clear enough for me:
    May you please clarify the difference? I still don't understand "removing the 2 level deep directory search"
     
  5. eva2000

    eva2000 Administrator Staff Member

    44,743
    10,200
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +15,809
    Local Time:
    7:15 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    my version limits image optimisations to 1 level of directories max at a time, so sub directories within the directory you point to don't get optimised and my version can't optimise a single file by pointing to it
     
  6. elargento

    elargento Member

    348
    17
    18
    Jan 4, 2016
    Ratings:
    +43
    Local Time:
    6:15 AM
    10
    Yes but what I don't understand is if @ShawnH script supports more levels of directories taking into account WP upload folder can contain many levels (years/months)

    @eva2000 sorry for being such a pain in the a#. I'm doing my best to learn server stuff but some words are still Chinese for me so I must ask everything to make sure I can understand the context of the situation
     
    Last edited: Jul 7, 2019