You'll want to use RiakBucket::newBinary()
and RiakBucket::getBinary()
if you want to store unencoded binary data into Riak with the PHP client.
$image = file_get_contents("images/TagLabs-Logo-White-240x60.png");
$md5 = md5($image);
$riak->bucket("test")
->newObject("image_base64", base64_encode($image))
->store();
$riak->bucket("test")
->newBinary("image_raw", $image, 'image/png')
->store();
$b64Read = $riak->bucket("test")->get("image_base64");
echo "B64 md5 comparison: original=$md5, b64=".md5(base64_decode($b64Read->getData()))."\n";
$rawRead = $riak->bucket("test")->getBinary("image_raw");
echo "Raw md5 comparison: original=$md5, raw=".md5($rawRead->getData())."\n";
Produces output:
B64 md5 comparison: original=6749cfaf1516b01db9792e119d53177a, b64=6749cfaf1516b01db9792e119d53177a
Raw md5 comparison: original=6749cfaf1516b01db9792e119d53177a, raw=6749cfaf1516b01db9792e119d53177a
In my performance tests, both approaches have basically the same overhead from Riak's perspective. Spending cycles on base64 encoding / decoding (plus under the hood, the base64 data is then json encoded/decoded) puts the binary approach ahead overall.
Edit: Also note that there's a ~50mb upper limit for data stored in a Riak binary object (see this post) due to a limitation in the Erlang backend. Realistically if you're getting anywhere near that, you might want to rethink how you're storing those images, that's a lot of data to send on the pipe if you're accessing those frequently, something like NFS or another local filesystem cache is probably a better idea.