We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21301
    • 93 Posts
    More on-the-fly processing! Here’s a plugin to demonstrate the use of regular expressions.

    It will filter out images on your site and add a watermark to them. It actually works but performance wise you’re better off not to do this on the fly. I just thought this could be an example on how to filter out certain tags. This does not mean the regexp is perfect, though I think it works well.

    This plugin is easily modified to do all sorts of other stuff like adding acronym tags. With some small changes you could also protect images from unauthorized users.

    /*
     *  Created: 8/2005
     *  For: MODx cms (modxcms.com)
     *  Name: Watermark
     *  Description: On the fly watermark images on your site
     *  Events: OnWebPagePrerender
     *  Version: 1.0
     *
     *  Put this text into a new plugin and set the System Event
     *  'OnWebPagePrerender'.
     *
     *  Depending on the regular expression it will take all src-
     *  fields within the <img> tag and change it.
     *  You need to install the file watermarked.php into
     *  '/assets/plugins/watermark/' for this to work.
     *
     *  You also need a '/assets/images/watermark.png' and
     *  configure the watermarked.php file to your needs.
     */
    
    $string = $modx->documentOutput;
    $pattern = "/(<img[^>]*src\s*=\s*[\'\"])([^\'^\"]*\.(jpg|jpeg))([\'\"][^>]*>)/i";
    $replacement = "$1/assets/plugins/watermark/watermarked.php?src=$2$4";
    
    $modx->documentOutput = preg_replace($pattern, $replacement, $string);
    


    /assets/plugins/watermark/watermarked.php
    <?php  
    
    header('content-type: image/jpeg');  
    
    // Should be a safe image repository if you
    // need to make sense.
    $dirprefix = "http://sitename.com/"; 
    
    $watermark = $dirprefix."/assets/images/watermark.png";
    $image = $dirprefix.$_REQUEST['src'];
    
    watermark($image, $watermark, 2,1);
    
    // The minimumfactors tell how big the image must compared to the watermark
    // before the watermark is applied. 2,2 means the image must be at least be 4
    // times as big as the watermark.
    function watermark($srcfilename, $watermark, $minumfactorx, $minumfactory) {
    $imageInfo = getimagesize($srcfilename); 
    $width = $imageInfo[0]; 
    $height = $imageInfo[1]; 
    $logoinfo = getimagesize($watermark);
    $logowidth = $logoinfo[0];
    $logoheight = $logoinfo[1];
    $horizmargin =  $width-$logowidth;
    $vertmargin =  $height-$logoheight;
    $photoImage = ImageCreateFromJPEG($srcfilename);
    
    if ($width>=($logowidth*$minumfactorx) && $height>=($logoheight*$minumfactory)) {
      // Only add watermark if watermark fits
      ImageAlphaBlending($photoImage, true);
      $logoImage = ImageCreateFromPNG($watermark);
      $logoW = ImageSX($logoImage);
      $logoH = ImageSY($logoImage);
      ImageCopy($photoImage, $logoImage, $horizmargin, $vertmargin, 0, 0, $logoW, $logoH);
    }
    
    ImageJPEG($photoImage); 
    ImageDestroy($photoImage);
    ImageDestroy($logoImage);
    }
    
    ?>
      • 33337
      • 3,975 Posts
      Hi Theo !

      Thanks for the cooool snippet smiley

      Best regards,

      zi
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 32963
        • 1,732 Posts
        Hi Theo,

        Vey nice smiley

        Doing the image processing on the fly can indeed impact performance. You might want o implement some image caching mechanism?

          xWisdom
          www.xwisdomhtml.com
          The fear of the Lord is the beginning of wisdom:
          MODx Co-Founder - Create and do more with less.
          • 21301
          • 93 Posts
          Quote from: xwisdom at Aug 29, 2005, 04:34 PM

          Doing the image processing on the fly can indeed impact performance. You might want o implement some image caching mechanism?
          That would of course be a perfect solution to the performance issues. It’s easy enough to create an image cache and store the watermarked images there. Also easy to check if the image already exists. Then checking if the image changed is a little harder, but with a MD5 check or so this shouldn’t be too difficult. Calculating the MD5 then also takes some processing. I wonder what would be best. Maybe looking at the time the page was modified is an option? Only do the processing if the page is less than 5 minutes old. If you have the preview option on in the manager, the images are written to cache as soon as you save the page. That makes me think of doing the cachefill with a second plugin, directly after the page has been saved. Or integrate with the modx caching mechanism. Could you advise on which event this would work best?

          Another question, why is the return statement in the plugin examples so important as stated below?
          default :
          return; // stop here - this is very important.
           break;
          
            • 32963
            • 1,732 Posts
            Well I think the OnWebPagePrerender event might be the best option here as everything is prerendered at this stage.

            As it relates to the default return statement. It’s only there to prevent further execution of the script but it’s not really necessary.

              xWisdom
              www.xwisdomhtml.com
              The fear of the Lord is the beginning of wisdom:
              MODx Co-Founder - Create and do more with less.
              • 21301
              • 93 Posts
              Adding cache headers to watermarked.php helps... It’s a start.

              The MD5 check could be replaced by a filelength check. That would be an easy method to check for an imagechange. I’ll look into that.

              Header ("Last-Modified: " . gmdate("D, d M Y H:i:s",mktime (0,0,0,1,1,2000)) . " GMT");  // Date in the past
              Header ("Expires: Mon, 26 Jul 2040 05:00:00 GMT");    // In other words... never expire the image
              Header ("Cache-Control: max-age=10000000, s-maxage=1000000, proxy-revalidate, must-revalidate");//Cache-Control header is ESSENTIAL for forcing Netscape to load all images!... and telling the ISP caches to do the same in this case cache for 1 million seconds.
                • 7455
                • 2,204 Posts
                I was looking for a snippet/plugin that replaces a string truwout the whole site but only in the content field.

                e.g.: I have a site www.brugco.com and yhis perdon like to see his name Brug-Co as this <b>BRUG-CO</b> truout his whole site instead of selecting all the brug-co’s and Brug-Co’s it would be nicer to search for that string and replace it.

                that could be done in a simular way you did on the images I think (search and replace)
                  follow me on twitter: @dimmy01
                  • 21301
                  • 93 Posts
                  Quote from: Dimmy at Aug 29, 2005, 10:27 PM

                  that could be done in a simular way you did on the images I think (search and replace)
                  That does work indeed. Using this plugin would change everything that’s sent to the browser. I’m not sure which event to use for the content field only. ’OnLoadWebDocument’ is not ok. I guess we need some docs on the events here. http://www.modxcms.com/system-events.html is still empty. Patience I think.

                  It does take up (some but little) performance. Why not dump the database table site_content (backup manager), do the search and replace on the sql and put the table back in? That would not change chunks/snippets but doesn’t take up any performance. If you backup the site/database before doing this (since it’s little risky depending on your db skills) you should be fine.
                    • 32963
                    • 1,732 Posts
                    Quote from: theo at Aug 29, 2005, 11:11 PM

                    That does work indeed. Using this plugin would change everything that’s sent to the browser. I’m not sure which event to use for the content field only. ’OnLoadWebDocument’ is not ok. I guess we need some docs on the events here. http://www.modxcms.com/system-events.html is still empty. Patience I think.

                    I would recommend using the ’OnWebPagePrerender’ event since the document at this point is fully parsed.


                    It does take up (some but little) performance.

                    True. But if you’re using cached files the impact would be negligible. Here’s how this can be done:

                    The $modx->documentContent is the document source that’s saved to cache while the $modx->documentOutput is the source that’s sent to the browser. If you do a search (using stripos or someother function) on $modx->documentContent and found an instance of brug-co then use relace it have those changes saved to cache. Next do a similar search on $modx->documentOutput for an instance of brug-co and if the string was found then replace it.

                    This way you would only be doing a replace if string was found

                      xWisdom
                      www.xwisdomhtml.com
                      The fear of the Lord is the beginning of wisdom:
                      MODx Co-Founder - Create and do more with less.
                      • 7455
                      • 2,204 Posts
                      Quote from: theo at Aug 29, 2005, 11:11 PM

                      Quote from: Dimmy at Aug 29, 2005, 10:27 PM

                      that could be done in a simular way you did on the images I think (search and replace)
                      That does work indeed. Using this plugin would change everything that’s sent to the browser. I’m not sure which event to use for the content field only. ’OnLoadWebDocument’ is not ok. I guess we need some docs on the events here. http://www.modxcms.com/system-events.html is still empty. Patience I think.

                      It does take up (some but little) performance. Why not dump the database table site_content (backup manager), do the search and replace on the sql and put the table back in? That would not change chunks/snippets but doesn’t take up any performance. If you backup the site/database before doing this (since it’s little risky depending on your db skills) you should be fine.

                      but what if the cliend made a new page would i need to do that again replacing it in the database?
                        follow me on twitter: @dimmy01