We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31946
    • 116 Posts
    Hello laugh

    I’m trying to write a plugin which generates auto thumbnails of images a user added
    with tinymce (only if the img tag contains a modified height or width property).

    However, i wanted to use Shaun’s phpthumbof snippet to generate the thumbnail.
    But as soon as I use this command, I get a blank page in the frontend, containing only the value returned by the snippet..(correct value!)

    $newimage = $modx->runSnippet("phpthumbof",array('input' => $input, 'options' => $options));
    


    This code is written inside the ’OnWebPagePrerender’ event.
    Anyone know why this happens? I tested to run another Snippet, I get also a blank page with the snippet-value.

    Thanks for answers smiley
      • 10713
      • 41 Posts
      This is exactly (exactly!) what I’m trying to do now, and I hit the same problem! This is the code of my plugin so far. Did you solve your problem??

      <?php
      $str = $modx->resource->get('content');
      
      preg_match_all('|\<img\s.*src=[",\']([A-Za-z0-9-_\s\/\.]+?)[",\'].*?\>|i', $str, $filenames);
      preg_match_all('|\<img\s.*height=[",\']([A-Za-z0-9-_\s\/\.]+?)[",\'].*?\>|i', $str, $heights);
      preg_match_all('|\<img\s.*width=[",\']([A-Za-z0-9-_\s\/\.]+?)[",\'].*?\>|i', $str, $widths);
      
      foreach($filenames[1] as $i => $filename) {
        $filetype = strtolower(substr($filename, strrpos($filename,".")+1));
        $height = $heights[1][$i];
        $width = $widths[1][$i];
      
        $image = array();
        $image['input'] = $filename;
        $image['options'] = "f=".$filetype."&h=".$height."&w=".$width; 
      
        $cacheurl = $modx->runSnippet('phpthumbof',$image);
        $str = str_replace($image['filename'], $cacheurl, $str);  
      }
      
      $modx->resource->set('content',$str);
        • 10713
        • 41 Posts
        As a quick fix, I copied the complete "phpthumbof"-snippet into my plugin an made a function out of it. It works, but this is not elegant!
          • 10713
          • 41 Posts
          It’s a caching problem. You should not try to manipulate the parsed MODX Object via "$modx->resource->set()", because "OnWebPagePreRender" this object is not of much use and generates trouble with caching.
            • 501
            • 10 Posts
            its work:
            $newimage = $modx->runSnippet("phpthumbof",array(
                                                                'input' => $item->image, 
                                                                'options' => 'w=50&h=50&zc=1'));


            where $item->image = path to file
              • 3749
              • 24,544 Posts
              Instead of using $modx->resource->set(), use this:


              $modx->resource->_output = $output;



              You can also use str_replace() or $modx->getChunk('TplChunk') to replace a placeholder for the image.


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting