Have this function that encodes content of images file to base64.
function data_uri($file_to_get_contents, $mime) {
$contents = file_get_contents( '../images/'. $file_to_get_contents );
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
Actual location (url) of images file is this
$val_img = '../images/2014-12-03/13-1-b5780ffc85f5f29d5ce43d1f4e38003f.gif';
I need to access echo image using such url (without .gif
, .jpg
etc.).
$val_img = '../images/2014-12-03/13-1-b5780ffc85f5f29d5ce43d1f4e38003f';
Decided to use glob
. And use code below.
$val_img = glob($val_img. '×.*');
Tried also
$val_img = glob( '×'. $val_img. '*' );
With this see empty array
echo '<pre>', print_r($val_img, true), '</pre> $val_img <br/>';
And with this
echo '<img src='. data_uri($val_img , "../images"). ' alt="Image" >';
See error like Warning: file_get_contents(../images/Array) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory
Error informs that in this $contents = file_get_contents( '../images/'. $file_to_get_contents );
line is error.
But seems something incorrect with this $val_img = glob($val_img. '×.*');
What would be correct code?
Here is my code that works
function data_uri($file_to_get_contents, $mime) {
$contents = file_get_contents( '../images/'. $file_to_get_contents );
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
$val_img = '../images/2014-12-03/13-1-b5780ffc85f5f29d5ce43d1f4e38003f';
$val_img = glob( $val_img. '*' );
echo '<img src='. data_uri($val_img[0] , "../images"). ' alt="Image" >';