Disclaimer: Before you reply you should know that I already know that storing images - in most circumstances - is a really dumb idea. I have also read the site FAQ with regards to this matter and I have also searched the forums for help on this without luck. The problem that has arisen is not of my making but I have been brought in - as per bloody usual - to fix someone else's mess. The problem needs a solution based on the current setup as redoing it would be far to time consuming and out of the reach of this.The problem lies in an SP that was rewritten to return the image dimensions and most of the time it works great. However recently an image was entered that caused the SP to return incorrect dimensions which isn't good for the app.From my understanding of the SP - asking the orginal author isn't an option
- the problem lies in how it handles JPG files, but I can't figure out what to change. I have written similar code but in VB and it doesn't translate well to T-SQL
i.e. it just don't work captian! Anyway here is the <sarcasm>wonderful piece of code</sarcasm> that is breaking:ALTER PROCEDURE ImageSize @ImgID as intAS/* *******************************************************************************Procedure usp_ImageSize returns the height and width of a specified image in theImages table.Inputs: @ImgID - Integer containing the ID of the image for which to return height and width.Outputs: A recorset is returned containing two columns - the first is the height in pixels of the image, and the second is the width in pixels of the image. If an invalid image ID is passed to the procedure, or the specified image has an invalid extension or MIME type, a null value is returned.Example call: exec ImageSize 24******************************************************************************* */set nocount ondeclare @MIMEType intset @MIMEType = (select MIMEType from Images where [ID]=@ImgID)if @MIMEType > 0 begin declare @ext char(3) declare @width int declare @height int declare @buf varbinary(10) declare @hb smallint declare @lb smallint set @ext = upper((select Extension from MIMETypes where [ID]=@MIMEType)) if @ext = 'GIF' begin set @buf = (SELECT SUBSTRING(ImageData, 1, 10) FROM images where [ID]=@ImgID) set @hb = substring(@buf, 8, 1) set @lb = substring(@buf, 7, 1) set @width = (256 * @hb) + @lb set @hb = substring(@buf, 10, 1) set @lb = substring(@buf, 9, 1) set @height = (256 * @hb) + @lb end else begin if @ext = 'JPG' begin declare @fp int declare @subcode int set @subcode = 0 set @fp = 3 set @buf = (SELECT SUBSTRING(ImageData, 1, 10) FROM images where [ID]=@ImgID) set @hb = substring(@buf, 2, 1) set @lb = substring(@buf, 1, 1) if (@hb = 216) and (@lb = 255) begin while @subcode != 49407 begin set @buf = (SELECT SUBSTRING(ImageData, @fp, 10) FROM images where [ID]=@ImgID) set @hb = substring(@buf, 2, 1) set @lb = substring(@buf, 1, 1) set @hb = @hb & 240 set @subcode = (256 * @hb) + @lb if @subcode = 49407 begin set @hb = substring(@buf, 6, 1) set @lb = substring(@buf, 7, 1) set @height = (256 * @hb) + @lb set @hb = substring(@buf, 8, 1) set @lb = substring(@buf, 9, 1) set @width = (256 * @hb) + @lb end else begin set @hb = substring(@buf, 3, 1) set @lb = substring(@buf, 4, 1) set @fp = @fp + 2 + (256 * @hb) + @lb end end end else begin set @ext = 'nil' end end else begin set @ext = 'nil' end end end set nocount off if @ext = 'nil' begin select null end else begin select @height, @width endend else begin set nocount off select nullend
Any help debugging this would be greatly appreciated.Regards,Graham