迅睿CMS框架是一款PHP8高性能·简单易用的CMS开源开发框架,基于MIT开源许可协议发布,免费且不限制商业使用,是免费开源的产品,以万端互联为设计理念,支持的微信公众号、小程序、APP客户端、移动端网站、PC网站等多终端式管理系统。
联系官方销售客服
1835022288
028-61286886
自定义应用里用header("Content-type: image/jpeg"); 输出图片,header不起作用,输出为乱码。
浏览器里还是content-type:text/html; charset=UTF-8,请教一下这个怎么样才能输出显示图片?
<?php namespace Phpcmf\Controllers; class Show extends \Phpcmf\Common { public function img() { $url = \Phpcmf\Service::L('input')->get('url'); $img_info = getimagesize($url); $img = $this->https_request($url); header('Content-type: image/jpeg'); return $img; } //HTTP请求(支持HTTP/HTTPS,支持GET/POST) protected function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $output = curl_exec($curl); curl_close($curl); return $output; } }
<?php namespace Phpcmf\Controllers; class Show extends \Phpcmf\Common { public function img() { $url = \Phpcmf\Service::L('input')->get('url'); $info = getimagesize($url); $img = $this->https_request($url); header('Content-type: image/jpeg'); $imgExt = image_type_to_extension($info[2], false); //获取文件后缀 $fun = "imagecreatefrom{$imgExt}"; $imgInfo = $fun($img); //1.由文件或 URL 创建一个新图象。如:imagecreatefrompng ( string $filename ) //$mime = $info['mime']; $mime = image_type_to_mime_type(exif_imagetype($img)); //获取图片的 MIME 类型 header('Content-Type:'.$mime); $quality = 100; if($imgExt == 'png') $quality = 9; //输出质量,JPEG格式(0-100),PNG格式(0-9) $getImgInfo = "image{$imgExt}"; $getImgInfo($imgInfo, null, $quality); //2.将图像输出到浏览器或文件。如: imagepng ( resource $image ) imagedestroy($imgInfo); } //HTTP请求(支持HTTP/HTTPS,支持GET/POST) protected function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $output = curl_exec($curl); curl_close($curl); return $output; } }
return $img;
echo $img;exit;