有此功能将图像文件的内容编码为base64。图像文件的php base64_encode和glob无法开始工作
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);
}
实际位置(URL)是本
$val_img = '../images/2014-12-03/13-1-b5780ffc85f5f29d5ce43d1f4e38003f.gif';
我需要访问使用这样的URL回波图像(不.gif
,.jpg
等)。
$val_img = '../images/2014-12-03/13-1-b5780ffc85f5f29d5ce43d1f4e38003f';
决定使用glob
。并使用下面的代码。
$val_img = glob($val_img. '×.*');
也试过
$val_img = glob('×'. $val_img. '*');
有了这个看空数组
echo '<pre>', print_r($val_img, true), '</pre> $val_img <br/>';
而与此
echo '<img src='. data_uri($val_img , "../images"). ' alt="Image" >';
看到错误像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
错误通知在此$contents = file_get_contents('../images/'. $file_to_get_contents);
行是错误的。
,但似乎不正确的数据与此$val_img = glob($val_img. '×.*');
什么是正确的代码?
这里是我的代码工作
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" >';
什么是“x。*”?只需要'“$ val_img。*”'应该得到正确的'glob()'结果。 – 2014-12-03 06:55:19
另外,你为什么首先使用'glob()'?因为你不知道扩展名?如果有两张图片的扩展名不同,会怎么样? – 2014-12-03 06:57:59
是的,因为我不知道扩展名。图像名称将是唯一的。我称没有扩展名的图像名称。如果这样的名字存在,我想显示图像。这就是为什么我使用glob – user2118559 2014-12-03 07:01:09