当前位置: 代码迷 >> 综合 >> php获取本地图片直接输出浏览器,file_get_contents
  详细解决方案

php获取本地图片直接输出浏览器,file_get_contents

热度:98   发布时间:2024-02-23 05:55:22.0
php获取本地图片直接输出浏览器,file_get_contents:function aa()
{$url = './img/99.png';//file_get_contents($url,true); 可以读取远程图片,也可以读取本地图片$img = file_get_contents($url, true);//使用图片头输出浏览器header("Content-Type: image/jpeg;text/html; charset=utf-8");echo $img;exit;
}
aa();第二种:$url = './img/99.png';
header('Content-Type: application/pdf'); // PDF文件
$fp = fopen($url, "rb"); //二进制方式打开文件
fpassthru($fp); // 输出至浏览器
exit;
如果是图片,就修改header头header('Content-Type: text/html; charset=utf-8'); //网页编码
15 header('Content-Type: text/plain'); //纯文本格式
16 header('Content-Type: image/jpeg'); //JPG、JPEG 
17 header('Content-Type: application/zip'); // ZIP文件
18 header('Content-Type: application/pdf'); // PDF文件
19 header('Content-Type: audio/mpeg'); // 音频文件 
20 header('Content-type: text/css'); //css文件
21 header('Content-type: text/javascript'); //js文件
22 header('Content-type: application/json'); //json
23 header('Content-type: application/pdf'); //pdf
24 header('Content-type: text/xml'); //xml
25 header('Content-Type: application/x-shockw**e-flash'); //Flash动画裁剪图片:
function resize_image($file, $w, $h, $crop=FALSE) {list($width, $height) = getimagesize($file);$r = $width / $height;if ($crop) {if ($width > $height) {$width = ceil($width-($width*abs($r-$w/$h)));} else {$height = ceil($height-($height*abs($r-$w/$h)));}$newwidth = $w;$newheight = $h;} else {if ($w/$h > $r) {$newwidth = $h*$r;$newheight = $h;} else {$newheight = $w/$r;$newwidth = $w;}}$src = imagecreatefrompng($file);$dst = imagecreatetruecolor($newwidth, $newheight);imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);return $dst;
}$image = resize_image('go.png', 756, 756);
//imagepng($image, './icon-128x128.png');
imagepng($image, './icon-256x256.png');var_dump($image);提示:
switch ($file_type) {case 'jpg':$src = imagecreatefromjpeg($file);$dst = imagecreatetruecolor($newwidth, $newheight);break;case 'png':$src = imagecreatefrompng($file);$dst = imagecreatetruecolor($newwidth, $newheight);break;case 'gif':$src = imagecreatefromgif($file);$dst = imagecreatetruecolor($newwidth, $newheight);break;case 'jpeg':$src = imagecreatefromjpeg($file);$dst = imagecreatetruecolor($newwidth, $newheight);}