开发者

Store dimensions (along with image) in database columns?

I must store my images in varbinary(max) fields in a SQLServer 2008 database (file system is not an option in my environment).

I'd like to be able to set image and height properties on HTML image tags when I serve these up (in my MVC2 app) and do different things in a gallery depending on portrait vs landscape orientation.

So, from your experience, would it be worth the hassle (and de-normalization?) to store this information in separate columns of the database, or开发者_运维知识库 is just cheap enough, performance-wise, to grab height & width & calculate orientation on the fly?


How easy is it to get them?

If you have instantiate an object every time for every image that adds up to lot of unnecessary overhead. I assume the images will be static too?

I'd tend to store the values down if they were used often.


You can maybe cache the dimensions first time the image is requested. Nice thing about varbinary(max) is that they are relatively easy to look inside from SQL. Had to write this recently to extract JPEG sizes from varbinary

CREATE PROCEDURE [SP_GET_JPEG_DIMENSIONS] 
(   
    @image varbinary(max), 
    @width int OUTPUT, 
    @height int OUTPUT,
    @Xdensity int OUTPUT,
    @Ydensity int OUTPUT    
) 
AS
    DECLARE @blocksize INT
    DECLARE @datasize INT
    DECLARE @i int
    SET @i=1
    SET @datasize = DATALENGTH(@image);
    IF(
        --Check for correct lead in
        SUBSTRING(@image,@i,1)=0xFF 
        AND SUBSTRING(@image,@i+1,1)=0xD8 
        AND SUBSTRING(@image,@i+2,1)=0xFF 
        AND SUBSTRING(@image,@i+3,1)=0xE0
        )
    BEGIN
        SET @i = @i+4;
        IF 
        (
        --Check for JFIF identifier
        CHAR(SUBSTRING(@image,@i+2,1))='J' 
        AND CHAR(SUBSTRING(@image,@i+3,1))='F' 
        AND CHAR(SUBSTRING(@image,@i+4,1))='I' 
        AND CHAR(SUBSTRING(@image,@i+5,1))='F'
        AND SUBSTRING(@image,@i+6,1)=0x00
        )
        BEGIN
            --get density
            SET @Xdensity = SUBSTRING(@image,@i+10,1)*256+SUBSTRING(@image,@i+11,1)
            SET @Ydensity = SUBSTRING(@image,@i+12,1)*256+SUBSTRING(@image,@i+13,1)

            SET @blocksize = SUBSTRING(@image,@i,1)*256+SUBSTRING(@image,@i+1,1)
            WHILE @i < @datasize
            BEGIN
                SET @i = @i+ @blocksize

                IF SUBSTRING(@image,@i,1) <> 0xFF RETURN -1 --not a good block              

                IF SUBSTRING(@image,@i+1,1)  = 0xC0 --image size block
                BEGIN
                    SET @height = SUBSTRING(@image,@i+5,1)*256+SUBSTRING(@image,@i+6,1)
                    SET @width = SUBSTRING(@image,@i+7,1)*256+SUBSTRING(@image,@i+8,1)

                END
                ELSE -- some other sort of block - move onto the next one
                BEGIN
                    SET @i = @i+2
                    SET @blocksize = SUBSTRING(@image,@i,1)*256+SUBSTRING(@image,@i+1,1)
                END         
            END
        END
        ELSE 
        BEGIN
            SET @width = -1
        END
    END
    ELSE 
    BEGIN
        SET @height = -1
        SET @width = -1
    END     
RETURN
GO
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜