Jump to content

Regex to get file type from BLOB


onlyican

Recommended Posts

Hi

 

Background:

We have a large MySQL table which stores data and images as BLOBS (do not ask)

 

My Task:

To extract the images from the SQL and save them as image files

 

Problem:

to Calculate the image type from the blob itself. (if its jpg, png ect)

 

I have heard that I can use a regex to work out if the image is jpg / png ect, but not sure what it is, could anyone advice??

 

Cheers

 

Link to comment
Share on other sites

Could you not just use the header ?

Or does this not work on blobs ?

 

if(header("Content-type: image/jpg")){
print $row['image'];  
}else if (header("Content-type: image/gif"); ){
print $row['image'];  
}else if(header("Content-type: image/png") ){
print $row['image'];  
}

Link to comment
Share on other sites

Cheers ignance

 

I was going down that avenue. Forgot about the "rename()" function

 

Was going to create a tmp file, use exif_imagetype then save image, delete tmp

 

but your method seems, shall we say, less messy

Link to comment
Share on other sites

Alternatively.. Each image type has it's own unique 'signature' within the first few bytes of the image. That means you can just run through an array of pre-defined signatures and check which matches, then write to a file:

 

function getImageTypeFromBlob($imageData)
{
    $signatures = array(
        'jpg' => "\xFF\xD8\xFF",
        'gif' => "GIF",
        'png' => "\x89PNG",
        'bmp' => "BM",
        'swf' => "CWS"
    );

    $first4Bytes = substr($imageData, 0, 4);

    foreach ($signatures as $imageType => $signature) {
        if (strpos($first4Bytes, $signature) === 0) {
            return $imageType;
        }
    }

    return false;
}

Link to comment
Share on other sites

I would recommend using ignace's method by the way, given you're already writing to a file and it's only what exif_imagetype() does internally. Just posted the alternative method so you're a bit clearer on how it works.

Link to comment
Share on other sites

Cheers Adam.

 

I have to do this for thousands of image entries in the db

 

We have approx 175000 entries per DAY. this is over at least 3 years of data.

 

So I might use your option Adam simply to save one process on the rename (normally wouldn't bother but at this scale, i think I might)

Link to comment
Share on other sites

Hmm I would definitely recommend benchmarking the two methods. Both have positives and negatives that stretched over 175000 repetitions could be quite a substantial difference. I would still lean towards the exif_imagetype() method though to be honest, just because it's a native function and more robust. I don't think the additional file read and rename would cause much of a difference. Benchmark them and find out though..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.