Skip to main content
September 9, 2010
Question

Can I control Image sizes from a database with coldfusion?

  • September 9, 2010
  • 2 replies
  • 418 views

Hi I have several thousand images that are loaded by search queries on my site. I had heard that CF 9 had a new tag for specifying the sizes of dynamic images, but couldnt find it. Is this possible? I want all my images to be of fairly similar size. Also should mention that some are jpeg and some are gif. Not sure if that matters anyway. Thanks for any help and suggestions.

    This topic has been closed for replies.

    2 replies

    Rodrigo Alarcon
    Known Participant
    September 9, 2010

    For what i understand you have a lot of images with different sizes and you need to scale them to the same width of height, that should be simple enougth with some math and using cfimage.

    <!--- Let's suppose you did a query b4 this comment and named it myImageQuery --->

    <cfoutput query="myImageQuery">
         <cfimage source="#myImageQuery.imagePath#" action="info" structName="imageInfo">
         <cfset widthConst="100"> <!--- This is a 100 pixel width for every image --->
         <cfset ScaleFactor = widthConst / imageInfo.width>
         <cfset newHeight = ScaleFactor * imageInfo.height>
         <img src="#myImageQuery.imagePath#" width="#widthConst#" height="#newHeight#" alt="#myImageQuery.imagePath#"/><br/>
    </cfoutput>

    This code will scale every image and set every width to 100 pixel and scale the height to keep the aspect ratio. Fair to warn you that this could be REALLY slow since you are displaying an image in full resolution scaled down, not a thumbnail of the image. What i do to fix that? i create a thumbnail scaled to the width of height that i need when the image is uploaded and show the user that version of the image that can be clicked to view a larger version of it.

    Hope this helps...!

    ilssac
    Inspiring
    September 9, 2010

    What do you want to actually do?

    Control the size of the image as it is displayed in the browser?


    OR

    Control the size of the image as it is stored in a file (or your case -- a database blob?).

    For the former that would just be proper height and width HTML or CSS parameters applied to the <img...> tags used to display the image in the web page.  But be warry of how you could abuse bandwidth if you are sending large image files that will be shrunk by the browser for display.

    For the latter that would be the <cfimage...> tag or functions, but I would not want to do this "on the fly".  That type of data restructuring is something I would hope is done by a back end "administative" type function.  Not something that is done "just-in-time" when an end user client requests the image file.

    HTH

    Ian