Php:Binary Image Data, Checking the Image Type

PHP : binary image data, checking the image type

The bits start with:

$JPEG = "\xFF\xD8\xFF"
$GIF = "GIF"
$PNG = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
$BMP = "BM"
$PSD = "8BPS"
$SWF = "FWS"

The other ones I wouldn't know right now, but the big 3 (jpeg,gif,png) usually cover 99%. So, compare the first bytes to those string, and you have your answer.

Check binary image data

What you're looking for is called file magic number.

The magic number is a type of file signature - since sometimes it takes more than the magic number to identify the file.

A (very) short list of such numbers can be found here. A larger list can be found here.

File identification websites often times also mention the file magic number.

In linux, the file command can be used to identify files.
In PHP you can use the FileInfo set of functions to identify files.


By the way, you did not specify the kind of files you want to identify. Sometimes, identification might be the wrong solution. For example, people used to want to identify files before passing them to GD or saving them on the server as images.
In this case, identification is not really your job. Instead, use the following code:

$data = file_get_contents('data.dat'); // File might eventcontain a JPG...it is
// still loaded without problems!
$image = imagecreatefromstring($data); // ... since this function just needs the
// file's data, nothing more.

How to convert binary image data to image file and save it in folder in php

Try the imagecreatefromstring method, which is documented here.

PHP - Convert binary to image and store it on server

Here is a simple code which I use to store images on my webserver, It doesn't convert images to binary, but it does the job of storing images onto the server.

You should create a uploads folder and give full permissions such as 777 to it.

<?php
echo $_FILES['image']['name'] . '<br/>';

//ini_set('upload_max_filesize', '10M');
//ini_set('post_max_size', '10M');
//ini_set('max_input_time', 300);
//ini_set('max_execution_time', 300);

$target_path = "uploads/";

$target_path = $target_path . basename($_FILES['image']['name']);

try {
//throw exception if can't move the file
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
throw new Exception('Could not move file');
}

echo "The file " . basename($_FILES['image']['name']) .
" has been uploaded";
} catch (Exception $e) {
die('File did not upload: ' . $e->getMessage());
}
?>

HTML which links to the php file (for your reference)

<html>
<head>
<title>Upload</title>
</head>
<body>
<form enctype="multipart/form-data" action="fileUpload.php" method="POST">
Select a file<input name="image" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

php: recreate and display an image from binary data

You can do this using a data URI in the image src attribute.

The format is: data:[<MIME-type>][;charset="<encoding>"][;base64],<data>

This example is straight from the Wikipedia page on data URIs:

<?php
function data_uri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
?>

<img src="<?php echo data_uri('elephant.png','image/png'); ?>" alt="An elephant" />

How to save image in folder ,when you convert binary image to string

use file_put_contents

$data = base64_decode($data);

file_put_contents('/tmp/image.png', $data);

or

file_put_contents('img.png', base64_decode($base64string));

Get image type from binary data image & convert it to actual image in PHP

About your 2 questions:

how to convert it back to normal image.

If the image is simply encode with base64, then of course decode it with base64_decode got the original image data.

$original_image = base64_decode($encoded_base64_string);

I guess no since what I need is its original image type

Don't be confused by it's name, getimagesize also tells you the image type and the mime type.

Have GD get image from binary string

imagecreatefromstring() should do the trick. I think the function example in the manual is almost exactly what you need:

$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}

Where $data is your binary data string from the database.



Related Topics



Leave a reply



Submit