We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32670
    • 11 Posts
    Is there a way to compress or resize files on upload?
    I'm using High Definition images with a resolution at around 1280x800. But the loading time of this images takes way too long in the lightbox after clicking the thumbnail. For now I'm using Photoshop batch compressing, the only problem is; not everyone has Photoshop installed on their PC..

    Kind Regards,
    Wouter
      MODX Revolution 2.2.4
    • You can generate them on the fly with phpthumb/phpthumbof, but in order to pre-process them you would need to setup a cron job and related scripts.
        Patrick | Server Wrangler
        About Me: Website | TweetsMODX Hosting
        • 32670
        • 11 Posts
        Allright. Let's take a look at that! Thanks!
          MODX Revolution 2.2.4
          • 16430
          • 217 Posts
          I have the same problem but I could not make it work with CronManager...
          Server returns code 200 if I put empty snippet (return true;) but return code 500 with this:
          <?php
          $images = new Imagick(glob('assets/p-images/*.JPG'));
          $imageprops = $image->getImageGeometry();
          if ($imageprops['width'] <= 800 && $imageprops['height'] <= 600) {
                  // don't upscale
              } else 
              foreach($images as $image) {
                // Providing 0 forces thumbnailImage to maintain aspect ratio
                $image->resizeImage(800,600, imagick::FILTER_LANCZOS, 0.9, true);
              }
          
          
          return;
            • 16430
            • 217 Posts
            Working resize snippet, which resize all images in specified directory and also include watermark image...
            <?php
            // resize all images in p-images
            $dir  = $modx->getOption('base_path') . 'assets/p-images/';      //original images
            $wdir  = $modx->getOption('base_path') . 'assets/w-images/';     //watermarked images
            $files = glob($dir . "*.jpg"); //get all jpg from dir
            
            $watermark = new Imagick($modx->getOption('base_path') . "assets/images/watermark.png");
            foreach($files as $images) {
               
              $image = new Imagick ($images);
              $imageprops = $image->getImageGeometry();
              if ($imageprops['width'] <= 800 && $imageprops['height'] <= 600) { 
                //is smaller, do nothing 
              }
              else {
                // Providing 0 forces thumbnailImage to maintain aspect ratio
                $image->resizeImage(800,600, imagick::FILTER_BOX, 0.8, true);
                 // write orig. images
            	  $image->writeImages($images, true);
              }
              $images = str_replace("p-images", "w-images", $images);
              $image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);
              $image->writeImages($images, true);
              
            }
            return;


            Have to be run manually or as a cron job...