dulan
dulan

Reputation: 1604

how to retrieve blob file to image in php?

I am using plupload to upload file in my php based website, with large file uploading the file becomes a file named 'blob' without any suffix. I know this is a binary file that contains the raw data, question is how to retrieve the data and save it back as an image file, say .png/.jpg or etc? I tried:

$imageString = file_get_contents($blogPath);
$image = imagecreatefromstring($imageString);

But it gives me some 'Data is not in recognized format...' error, any thoughts? Thanks in advance.

Upvotes: 2

Views: 3050

Answers (3)

dulan
dulan

Reputation: 1604

I am storing the uploaded image files for late use, like attaching them to posts or products(my site is e-commerce CMS). I figured that my image file didn't get fully uploaded to the server, the image before upload is 6mb, but the blob file is just 192kb, so my best guess is that what get uploaded is just a chunk instead of the whole package, and yet that brought up another question: how should I take all the pieces and assemble them as one complete image file? As mentioned earlier, I am using plupload for js plugin and php as backend, the backend php code to handle uploading goes like this:

move_uploaded_file($_FILES["file"]["tmp_name"], $uploadFolder . $_FILES["file"]["name"]);

Upvotes: 1

unixmiah
unixmiah

Reputation: 3145

Instead of doing that you should do this to display image to the browser

 <img src="data:image/jpeg;base64,'.base64_encode( $row['blob_image'] ).'"/>

I'm not sure what imagecreatefromsting does or how it encodes the image.

I looked at the documentation for that function; you're missing:

$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';

$data = base64_decode($data); <--- this operation

Upvotes: -1

Brad
Brad

Reputation: 163232

Your call to imagecreatefromstring() should work just fine if your file_get_contents() is working. Use var_dump($imageString) to verify. Did you mean to name your variable $blobPath instead of $blogPath?

You don't need to load this image though. Just rename the file.

rename($blobPath, 'new/path/here.jpg');

http://php.net/manual/en/function.rename.php

Upvotes: 1

Related Questions