We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4385
    • 372 Posts
    Is there a way to force a snippet to change the content-type? I want to set the header to an image. I know you can do it with a resource. But I wanted to make a self contained snippet.

    I am making a snippet called DIG (Dynamic Image Generator) from this script. http://dummyimage.com/

    [[!DIG? &size=`520x300` &background=`618200` &foreground=`fff` &text=`This is a MODx image.`]]

    I can get it to create the file on the file system, but that leaves orphaned images to build up over time.
      DropboxUploader -- Upload files to a Dropbox account.
      DIG -- Dynamic Image Generator
      gus -- Google URL Shortener
      makeQR -- Uses google chart api to make QR codes.
      MODxTweeter -- Update your twitter status on publish.
      • 3749
      • 24,544 Posts
      Could you just use an empty template and have the snippet generate HTML to produce the image (and maybe the header)?

      $output = '<img src = "generator.php" etc.>';
      return $output;

        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
        • 4385
        • 372 Posts
        Quote from: BobRay at Oct 21, 2010, 11:58 PM

        Could you just use an empty template and have the snippet generate HTML to produce the image (and maybe the header)?

        $output = '<img src = "generator.php" etc.>';
        return $output;



        Sure, but I was just trying to make it a bit cleaner for someone else to use.
          DropboxUploader -- Upload files to a Dropbox account.
          DIG -- Dynamic Image Generator
          gus -- Google URL Shortener
          makeQR -- Uses google chart api to make QR codes.
          MODxTweeter -- Update your twitter status on publish.
          • 22629
          • 194 Posts
          If your snippet is responsible for writing the image directly to the browser, you could have it execute the following at the end:

          header('Content-type: image/jpeg');
          exit();


          (obviously replacing image/jpeg with your image’s associated content type.

          This will prevent MODx from over-ruling the content-type header.

          I did the same thing not long ago (using GD to render a thumbnail of an image on-demand.)
            Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

            modx Revolution 2.2.6
            Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

            Content-Managed Websites Built on MODX
            • 4385
            • 372 Posts
            Thanks Andy, but that still kinda kills it as a snippet. But thank you, it gave me an idea. I moved the image generation code to an external file and just have the snippet build the image url.

            It works great. This is snippet is intended to be used as a placeholder for development and mockups, I would not use it on a live site, since it makes the image on the fly every time. I added iab standard banner sizes too.

            [[DIG? &size=`leaderboard`]]
            <h1>About Us</h1>
            [[DIG? &size=`180x240` &background=`618200` &foreground=`fff` &text=`This will be your picture.` &class=`floatRight`]]
            




              DropboxUploader -- Upload files to a Dropbox account.
              DIG -- Dynamic Image Generator
              gus -- Google URL Shortener
              makeQR -- Uses google chart api to make QR codes.
              MODxTweeter -- Update your twitter status on publish.
              • 4385
              • 372 Posts
              BTW I used the most excellent Packman to make the transport package. I am sure the snippet could be improved and made the Revo way.
                DropboxUploader -- Upload files to a Dropbox account.
                DIG -- Dynamic Image Generator
                gus -- Google URL Shortener
                makeQR -- Uses google chart api to make QR codes.
                MODxTweeter -- Update your twitter status on publish.
                • 22629
                • 194 Posts
                Ah I see what you’re doing. Mine works slightly differently - you place the snippet on a dedicated resource page (uncached as the snippet caches thumbnails itself) which has an empty template:

                [[!ImageThumbnail]]


                I can then call this page from anywhere in modx:

                <img src="[[~[[++thumbnail_resource_id]]? &scheme=`full` &imageId=`10` &width=`100` &height=`100`]]" alt="Thumbnail" />


                imageId is the ID of a static resource in my modx content tree. Width and height are obviously how big to make the image. thumbnail_resource_id is a system setting containing the ID of the dedicated resource page.

                This creates an <img> tag like:

                <img src="http://www.mysite.com/thumbnail?imageid=10&width=100&height=100" alt="Thumbnail" />


                The ImageThumbnail snippet reads the GET parameters and writes the image directly to the browser, which displays it for the <img> tag.
                  Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

                  modx Revolution 2.2.6
                  Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

                  Content-Managed Websites Built on MODX
                  • 4385
                  • 372 Posts
                  @Andy -- Good idea. I might need to do something like that in the future.

                  I made the DIG snippet just for fun. I think it would be useful for mocking up a design for a client with placeholder images (like the feature Dreamweaver has).
                    DropboxUploader -- Upload files to a Dropbox account.
                    DIG -- Dynamic Image Generator
                    gus -- Google URL Shortener
                    makeQR -- Uses google chart api to make QR codes.
                    MODxTweeter -- Update your twitter status on publish.