• Everything is setup and working smoothly so far with dynamic_images, but there’s one last thing I’d like to improve, before putting it on a live site.

    Fast Rewrite (FR) feels sensibly faster than Compatible Rewrite, but produces blurrier images. FR doesn’t seem to fire other hooks, like image_make_intermediate_size. Have modded a tiny plugin that improves sharpening dramatically for our kind of usage, but it doesn’t work in FR as it’s attached to the aforementioned hook. Would you consider integrating some sharpening abilities to wppp, or perhaps better, enable a (limited) set of filters so one could hook into them? I looked into this but couldn’t figure it out. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter alx359

    (@alx359)

    Relevant. Especially this.

    Thread Starter alx359

    (@alx359)

    Ok, after some poundering, and thanks to code already existed but wasn’t used, found my way around to enable support for specifically chosen plugins in shortInit mode.

    In class.wppp_serve_image.php ~line 174

    $this->load_wppp();
    
    // added to re-enable the built-in support for plugins in shortInit mode
    $this->prepare_resize();

    in serve-dynamic-images.php ~line 116 (right after EWWW code)

    // Add support for other plugins
    // as we can't use hooks in ShortInit, we do it via an option instead
    $wppp_dynimg_shortInit_plugins = get_option( 'wppp_dynimg_shortInit_plugins', array() );
    
    if ( !empty( $wppp_dynimg_shortInit_plugins ) && is_array( get_option( 'active_plugins' ) ) ) {
    
        // copied from above to keep this tweak self-contained
        if ( !$ewww ) { 
            wp_plugin_directory_constants();
            wp_load_translations_early();
            $GLOBALS['wp_plugin_paths'] = array();
        }
        
        foreach( $wppp_dynimg_shortInit_plugins as $plugin ) {
            $path = wp_normalize_path(WP_PLUGIN_DIR . '/' . $plugin);
            wp_register_plugin_realpath( $path );
            include_once ( $path );
        }
    }

    Now, to include a new plugin (for example this), I use this code in functions.php:

    add_action( 'init', 'wppp_dynimg_shortInit_plugins' );
    function wppp_dynimg_shortInit_plugins() {
        
        // plugin to include
        $plugin = 'sharpen-resized-images/ajx-sharpen-resized-images.php';
        
        $plugins_include = get_option('wppp_dynimg_shortInit_plugins', array());
    
        // check if the plugin is not already in the option array
        // to avoid redundant entries
        if( !in_array( $plugin, $plugins_include ) ) {
            $plugins_include[] = $plugin;
            update_option('wppp_dynimg_shortInit_plugins', $plugins_include);
        }
    
    }

    That’s it, it works!

    Hope Bjoern would (re)consider support for at least some simpler kinds of plugins in an upcoming version.

    Thread Starter alx359

    (@alx359)

    Have been updating this tweak to make it a bit more robust. Will share upon request, to not spam this topic anymore.

    Thread Starter alx359

    (@alx359)

    Another useful thing that have added lately is the support for a special page containing the code that will hook during SHORTINIT. That way one’s not obliged to use plugins as in the previous patch above. This page is located at <current_theme>/wppp/wppp_shortinit_functions.php

    The code for supporting this page is right after the SHORTINIT plugins code. So it all together looks like this now:

    // Add support for selected plugins
    // as we can't use hooks in ShortInit, we do it via an option instead
    $wppp_dynimg_shortInit_plugins = array_unique( get_option( 'wppp_dynimg_shortInit_plugins', array() ) );
    $plugins = get_option( 'active_plugins', array() ); 
    
    if ( !empty( $wppp_dynimg_shortInit_plugins ) && !empty( $plugins ) ) {
    
        // copied from above to keep this patch self-contained
        if ( !$ewww ) { 
            wp_plugin_directory_constants();
            wp_load_translations_early();
            $GLOBALS['wp_plugin_paths'] = array();
        }
        
        // Load the wanted plugins.
        // Mind SHORTINIT imposes many limitations, as for performance reasons
        // only a very limited functionality of WP core is loaded and available
        foreach( array_keys($wppp_dynimg_shortInit_plugins) as $key ) {
    
            $plugin = $wppp_dynimg_shortInit_plugins[$key];
    
            if ( in_array( $plugin, $plugins ) ) {
                $path = wp_normalize_path(WP_PLUGIN_DIR . '/' . $plugin);
               #error_log( var_export( $path, true ));
                if ( file_exists( $path ) ) {
                    wp_register_plugin_realpath( $path );
                    include_once( $path );
                }
            } else {
                // wanted but inactive plugins become forgotten 
                unset( $wppp_dynimg_shortInit_plugins[$key] );
                update_option('wppp_dynimg_shortInit_plugins', array_unique( $wppp_dynimg_shortInit_plugins ) );
            }
        }
        unset( $plugins );
    }
    // Add support for theme-based configuration.
    // A more convoluted way is used to get the current theme path
    // because the absence of many core functions in SHORTINIT
    $wppp_theme_settings = wp_normalize_path(WP_CONTENT_DIR.'/themes/'.get_option('stylesheet').'/wppp/wppp_shortinit_functions.php');
    if ( file_exists( $wppp_theme_settings ) ) {
        include_once( $wppp_theme_settings );
    }

    Here’s for example how I use wppp_shortinit_functions.php with a proposed new filter, to allow me modify the crop position of certain images where heads get cut off:

    /**
     * WPPP SHORTINIT Filters
     * Mind many/most of WP core functions won't work in SHORTINIT mode, 
     * so keep it simple and always test
     */
    
    // no direct page access
    defined('ABSPATH') || exit;
    
    add_filter( 'wppp_dynimg_crop_position', 'top_crop_position_images', 10, 2 );
    function top_crop_position_images( $position, $image_path ) {
    
        $image_files_top_pos = [
        'a4-east-woman-y21',
        ];
    
        $image_name = basename( $image_path );
        
        foreach( $image_files_top_pos as $image ) {
            if ( strpos($image_name, $image) !== FALSE ) {
                $position = ['center','top'];
                break;
            }
        }
        return $position;
    }

    HTH.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Fast Rewrite sharpening’ is closed to new replies.