2

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.

dulan
  • 1,584
  • 6
  • 22
  • 50

3 Answers3

1

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

Brad
  • 159,648
  • 54
  • 349
  • 530
  • No I didn't mean to rename the variable or the file, the image file uploaded to the server becomes a file named 'blob', and this blob file cannot be opened with any image viewing software, because it is no longer an image but a whole bunch of binary data, so I want to retrieve this blob file back to an image file. var_dump does print out the content of image string, although it's all unrecognized characters. Sorry that I didn't make this more clear:( – dulan Sep 18 '14 at 01:27
  • @dulan An image is a whole bunch of binary data. The file name on disk doesn't matter at all. What matters is content type. If you are trying to send the image to the client, just add `header('Content-Type: image/jpeg');`. – Brad Sep 18 '14 at 01:53
  • I see your point, I am trying to store that image for later use, maybe attach it to a post or a product(My website is an ECommerce CMS) – dulan Sep 18 '14 at 02:10
  • @dulan Right, so what is it exactly that you're trying to do? It isn't clear at all. Your question originally asks about renaming a file, but then you say that's not what the question is. Then you say that you need a way to show that image to users correctly, so I mention how to add that header, but now you're talking about doing something with the file again. Can you be more clear and specific as to what it is you're attempting to do? – Brad Sep 18 '14 at 02:12
  • Well.. I didn't ask that many things, just how to retrieve blob to image file, but never mind, I found out that the problem is that it is uploaded as chunks and not getting handled appropriately in php as I explained in my own answer. Anyhow, thanks for all of your time. – dulan Sep 18 '14 at 02:23
1

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"]);
dulan
  • 1,584
  • 6
  • 22
  • 50
  • Never mind this new question, I think it is a duplicate and is already answered here:http://stackoverflow.com/questions/9011138/handling-pluploads-chunked-uploads-on-the-server-side – dulan Sep 18 '14 at 02:18
-1

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
unixmiah
  • 3,081
  • 1
  • 12
  • 26