splittingred / phpThumbOf

A secure phpthumb output filter for MODx Revolution
http://rtfm.modx.com/display/addon/phpthumbof/
23 stars 17 forks source link

Implemented a system event just before initializing the image render #40

Closed bertoost closed 12 years ago

bertoost commented 12 years ago

To can hook on it for a "style-based" component

PLEASE READ:

The idea is to create a component for making style-based image-scaling possible. This is intended for SEO purposes and high configurable trough a custom manager page. The new call will be;

[[*imageTVname:phpthumbof=`style=blog`]]

The image will be saved in;

assets/components/phpthumbof/cache/blog/

Without any hashes (the plugin will set "phpthumbof.postfix_property_hash" to false).

You can also setup a custom URL path for every single style. Like "i/blog/". The image URL returned will be "***/i/blog/image.jpg". With one line of htaccess code you can redirect this back to the cache map of phpthumbof.

A single style will contain the phpthumb properties like "w=100" and "h=75". Even can make it easy to add watermarks and grayscales etc. (basically every property is possible), all setable trough the manager interface.

The plugin provided with this custom component will hook in on the event added in this pull request and will checkup the custom database table(s) to find the provided stylename and pass the properties trough to phpthumbof object.

Hope this is clear enough. I even can let you see how things work, because I have tested it already! And it works very very great! Please contact me if you have any questions

bertoost commented 12 years ago

Example of a hard-coded plugin for above description..

/**
 * 
 * Test to generate image thumbnails based on stylenames in parameters of phpthumbof
 * Example: [[*imageTV:phpthumbof=`style=mystyle`]]
 */
if($modx->context->get('key') == 'mgr') { return ''; }

$eventName = $modx->event->name;
switch($eventName) {

    case 'OnPhpThumbOfInit':
        $ptThumbnail =& $scriptProperties['ptThumbnail'];
        $options = $ptThumbnail->options;

        if(isset($options['style']) && !empty($options['style'])) {

            // TODO: check if style exists in custom db, for now just assume it does

            $properties = array('w=300', 'h=125', 'zc=1', 'q=90');

            $customPath = '';
            $customPath = 'i/'.$options['style'];

            // set options into phpThumb object
            $ptThumbnail->setOptions($properties);
            unset($phpthumbnail->options['style']);

            // overwrite the cachePath
            $cachePath = $ptThumbnail->config['cachePath'];
            $cachePath .= $options['style'].'/';
            $ptThumbnail->config['cachePath'] = $cachePath;
            if(!file_exists($cachePath)) {
                $modx->cacheManager->writeTree($cachePath);
            }

            if(!empty($customPath)) {

                // overwrite the cacheUrl
                $baseUrl = $modx->getOption('base_url');
                $ptThumbnail->config['cachePathUrl'] = $baseUrl.$customPath.'/';
            }
            else {

                $ptThumbnail->config['cachePathUrl'] .= $options['style'].'/';
            }

            // overrule some settings
            $ptThumbnail->config['phpthumbof.postfix_property_hash'] = false;
        }
    break;
}

Note that this is for test only, to see if I can achieve my goal.

bertoost commented 12 years ago

A screenshot of the image-test result of above descriptions

http://i.oostdesign.nl/s/phpthumof-image-style-test-result.png

(in here is "/_testing/modx-2.2.2-pl/" a test install on my develop machine)

Used this little line in my htaccess to route the "i/blog/" image

RewriteRule ^i/(.*)$ assets\/components\/phpthumbof\/cache\/$1 [L,QSA]

bertoost commented 12 years ago

Found an other way (eq. Gallery component does), no need for phpthumbof to add this change!