base64_decode

See all posts Reply

base64_decode  new!
by Steve, 1 year ago
I have image data stored in a MySQL table encoded as base64.

How can I use this class to use that data, resize and store on my server?

So far I've gotten this far:

$data = $myBase64DataFromDb;

if (preg_match('/^data:image\/(\w+);base64,/', $data, $type)) {
  $data = substr($data, strpos($data, ',') + 1);
  $type = strtolower($type[1]); // jpg, png, gif
  $data = base64_decode($data);
  $path = '/img/put/here/';

  $filename = $data.'.'.$type;

  $handle = new upload($filename);
  $handle->file_max_size = '5000000';

  $handle->image_resize = true;
  $handle->image_x = 600;
  $handle->image_ratio_y = true; 
  $handle->process($path);
  if ($handle->processed) {
      echo $filename;
      $handle->clean();
  } else {
      echo 'error : ' . $handle->error;
  }
}
Reply
Re: base64_decode  new!
by colin, 1 year ago
Your code above cannot work anyway: you are trying to instantiate a class with a file that doesn't exist. Indeed, the filename you have is the contents of the file with an image extension, it cannot possibly work.

You should instantiate the class with raw base64 data:
$handle = new upload($data);
Reply
Re: base64_decode  new!
by Steve, 1 year ago
Great, thanks for your help. I have changed the code as per your suggestion and the file was stored, however it is a data.txt file with base64 encoded image. How do I convert it back to jpeg image and store it?Reply
Re: base64_decode  new!
by Steve, 1 year ago
Great, thanks for your help. I have changed the code as per your suggestion and the file was stored, however it is a data.txt file with base64 encoded image. How do I convert it back to jpeg image and store it?Reply