14 Replies Latest reply on Oct 8, 2015 1:35 PM by WolfShade

    ImageResize: unable to cast an object of [B to image

    WolfShade Level 4

      Hello, all,

       

      On a personal project at home, I'm trying to use ImageResize to resize an image pulled from a BLOB in a MySQL database.  I'm getting an error message "unable to cast an object of [B to image."  Have not a clue what that means. 

       

      Here's my code..

       

      SELECT imageBLOB, imageWidth, imageHeight
      FROM r_images
      WHERE imageID = <cfqueryparam value="#arguments.thisID#" cfsqltype="cf_sql_varchar" />
      
      

       

      and..

       

      variables.thisPicQry = new components.PICTURES().getAlbumPic('#trim(url.id)#');
      ImageResize(variables.thisPicQry.imageBLOB,"50%","");
      variables.thisImg = variables.thisPicQry.imageBLOB;
      
      

       

      I've never used ImageResize() before.  The data from the database is already a binary; so what am I doing incorrectly?

       

      V/r,

       

      ^_^

        • 1. Re: ImageResize: unable to cast an object of [B to image
          WolfShade Level 4

          Do I need to convert the BLOB to something else in order to use ImageResize()?  If I don't use ImageResize(), the picture displays, but it's going way past the DIV boundary (it's being used as a background image.)

           

          V/r,

           

          ^_^

          • 2. Re: ImageResize: unable to cast an object of [B to image
            haxtbh Level 4

            Create an image instance first and assign the blob to it, then manipulate the image.

             

            So something like this :

             

            theImage = ImageNew(variables.thisPicQry.imageBlob);
            ImageResize(theImage, "50%");
            

             

            Are you just displaying this on a web page though? If so and the image are not huge in size, dont use Coldfusion to resize it, just use CSS.

            • 3. Re: ImageResize: unable to cast an object of [B to image
              WolfShade Level 4

              The images range anywhere from 160k to ~400k, so if resizing it can bring it down, perfect - less network traffic, at the very least.  This is for a "kind of" thumbnail - it's the picture that represents the album it is in, so I don't want to shrink the image before storing in the database.

               

              I did try that, earlier, but it didn't work.  I'll see about trying, again.

               

              Thanks for your reply!

               

              V/r,

               

              ^_^

              • 4. Re: ImageResize: unable to cast an object of [B to image
                BKBK Adobe Community Professional & MVP

                Shouldn't imageResize() have at least 3 arguments? In any case, I cannot see what's going on with:

                 

                variables.thisPicQry = new components.PICTURES().getAlbumPic('#trim(url.id)#'); 

                and

                variables.thisPicQry.imageBLOB

                 

                If the name of the query is variables.thisPicQry then you could just do something like

                 

                <cfset thisImg = imageNew(variables.thisPicQry.imageBLOB)>

                <cfset imageResize(thisImg, "50%", "50%")>

                • 5. Re: ImageResize: unable to cast an object of [B to image
                  haxtbh Level 4

                  Yeah it should have 3 arguments. I missed the last one off by accident when I typed my reply. Copy and paste fail!

                   

                  If that is still not working, then why doesnt it work. What happens and what are you expecting?

                  • 6. Re: ImageResize: unable to cast an object of [B to image
                    WolfShade Level 4

                    Unfortunately, things at work have exploded (new public site launched; it was showcased at a huge convention in DC; end of FY; end of contract, then recompete; frenzy of getting new CACs for everyone in my dept) and I haven't had a chance to breathe, much less get back to this project at home. 

                     

                    I'll try that as soon as I can and report back.

                     

                    Thanks, BKBK and haxtbh, for your help.

                     

                    V/r,

                     

                    ^_^

                    • 7. Re: ImageResize: unable to cast an object of [B to image
                      WolfShade Level 4

                      BKBK wrote:

                      <cfset thisImg = imageNew(variables.thisPicQry.imageBLOB)>

                      <cfset imageResize(thisImg, "50%", "50%")>

                      If I use _either_ "ImageNew()" or "ImageResize()", I get a blank.  Nothing.  No error message, but no picture.  Is there a setting in CFAdmin that needs to be set for this to work?  The DSN _does_ have BLOB/CLOB enabled, and I jacked the max size to x10 the default.

                       

                      If I remove both "ImageNew()" and "ImageResize()", I get the image to appear, but it's over twice the size that I need.

                       

                      V/r,

                       

                      ^_^

                      • 8. Re: ImageResize: unable to cast an object of [B to image
                        BKBK Adobe Community Professional & MVP

                        (Grasping at straws) - does this fare any better in debugging: 

                         

                        <cfset thisImg = imageNew(#variables.thisPicQry.imageBLOB#)>

                        <cfset imageResize(thisImg, "50%", "50%")>

                        <cfdump var="#thisImg#">

                        • 9. Re: ImageResize: unable to cast an object of [B to image
                          WolfShade Level 4

                          I don't think it will.  I'm going back to my OP to make sure I didn't miss anything.  Hang on..

                           

                          Okay.. I'm not sure if this will make any difference, or not (if it does, I've completely lost all faith in CFCs) (that might be a bit strong, I will be upset, though):

                           

                          I have a page called "pictures.cfm" that upon first page load grabs (via CFC) all of the albums in the database; part of this query gets an image ID that is the album "cover" photo.

                           

                          As the CFOUTPUT loops the query, there is a DIV that is supposed to display the picture from the database.

                           

                          <div id="pic#variables.picsQry.currentrow#" class="albumpic" style="background-image: url('display.cfml?t=i&amp;section=pics&amp;id={uuid};"></div>
                          
                          

                           

                          display.cfml uses the code from my OP to get the BLOB, and use CFCONTENT and CFHEADER to display the image as the background of the div.

                           

                          Like I said, if I don't use imageNew() or imageResize(), it works, albeit larger than I need it.  When I add either of those, it breaks.  I am beginning to suspect that it's because imageNew and imageResize can only be used with cfimage.

                           

                          V/r,

                           

                          ^_^

                          • 10. Re: ImageResize: unable to cast an object of [B to image
                            BKBK Adobe Community Professional & MVP

                            I have some questions. How do you implement the cfcontent and cfheader tags?

                            <div id="pic#variables.picsQry.currentrow#" class="albumpic" style="background-image: url('display.cfml?t=i&amp;section=pics&amp;id={uuid};"></div>

                            There is a missing bracket and the URL seems suspect. Would it be an idea to use display.cfm to write the resized image to disk (say, as somePic.jpg), then do

                             

                            <div id="pic#variables.picsQry.currentrow#" class="albumpic" style="background-image: url('somePic.jpg')"></div>

                            • 11. Re: ImageResize: unable to cast an object of [B to image
                              WolfShade Level 4

                              BKBK wrote:

                               

                              I have some questions. How do you implement the cfcontent and cfheader tags?

                              <cfheader name="Content-Disposition" value="inline; filename=file_#trim(url.id)#.jpg" />
                              <cfcontent type="image/jpg" variable="#variables.thisImg.BLOB#" reset="yes" /> <!--- variables.thisImg.BLOB is a variable set that is the binary data --->
                              
                              

                              BKBK wrote:

                               

                              There is a missing bracket and the URL seems suspect. Would it be an idea to use display.cfm to write the resized image to disk (say, as somePic.jpg), then do

                               

                              <div id="pic#variables.picsQry.currentrow#" class="albumpic" style="background-image: url('somePic.jpg')"></div>

                              The missing bracket is because I manually typed the code (it's on another machine, and I got lazy) and missed the ending apostrophe and parenthesis; it's there in the actual code.

                               

                              Why does the URL seem suspect?  I've used it successfully in other projects, as well as other parts of this project.

                               

                              I do not want to write to disk, because I'll have to clean it up, later, and want to keep this as efficient as possible.  Disk I/O cost may not seem much for one ~150k to ~300k image, but when you're on shared hosting, it can really slow things down.

                               

                              V/r,

                               

                              ^_^

                              • 12. Re: ImageResize: unable to cast an object of [B to image
                                WolfShade Level 4

                                I wound up using CSS to set the background image size.  I'm not a fan of doing it that way, but I have not yet found a solution that will set that on the server-side before sending it to the browser.

                                 

                                If you can think of a way to use imageResize() and the CFHEADER/CFCONTENT method I'm using to display images, please let me know.  Thanks, haxtbh and  BKBK, for your input.

                                 

                                V/r,

                                 

                                ^_^

                                • 13. Re: ImageResize: unable to cast an object of [B to image
                                  BKBK Adobe Community Professional & MVP

                                  Your title showed that something was amiss. The error message, "unable to cast an object of [B to image", implies that ColdFusion is unable to handle images in this way.

                                   

                                  Coldfusion may not recognize the blob from the database as an image. Incidentally, what line of code did the error message point to?

                                  • 14. Re: ImageResize: unable to cast an object of [B to image
                                    WolfShade Level 4

                                    When I first started on this, that is the error message it _was_ giving.  It hasn't displayed that message, lately.  If I remember correctly, the line number was pointing at the ImageResize() line.

                                     

                                    V/r,

                                     

                                    ^_^