迅睿CMS框架是一款PHP8高性能·簡(jiǎn)單易用的CMS開源開發(fā)框架,基于MIT開源許可協(xié)議發(fā)布,免費(fèi)且不限制商業(yè)使用,是免費(fèi)開源的產(chǎn)品,以萬端互聯(lián)為設(shè)計(jì)理念,支持的微信公眾號(hào)、小程序、APP客戶端、移動(dòng)端網(wǎng)站、PC網(wǎng)站等多終端式管理系統(tǒng)。
聯(lián)系官方銷售客服
1835022288
028-61286886
自定義應(yīng)用里用header("Content-type: image/jpeg"); 輸出圖片,header不起作用,輸出為亂碼。
瀏覽器里還是content-type:text/html; charset=UTF-8,請(qǐng)教一下這個(gè)怎么樣才能輸出顯示圖片?
要看看你的代碼
回復(fù)@官方研發(fā)實(shí)習(xí)技術(shù)
<?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請(qǐng)求(支持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; } }
你是要把圖片輸出到瀏覽器嗎?這種代碼是完成不了輸出瀏覽器的
但是這個(gè)圖片,用img標(biāo)簽是可以顯示的,就是直接訪問是亂碼,設(shè)置的header沒起作用
<?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 創(chuàng)建一個(gè)新圖象。如: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; //輸出質(zhì)量,JPEG格式(0-100),PNG格式(0-9) $getImgInfo = "image{$imgExt}"; $getImgInfo($imgInfo, null, $quality); //2.將圖像輸出到瀏覽器或文件。如: imagepng ( resource $image ) imagedestroy($imgInfo); } //HTTP請(qǐng)求(支持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; } }
改進(jìn)一下,試一試這個(gè)
回復(fù)@官方研發(fā)實(shí)習(xí)技術(shù) 好吧,那我用單文件吧,單文件是可以輸出顯示的。
return $img;
改成
echo $img;exit;
回復(fù)@官方研發(fā)實(shí)習(xí)技術(shù) 謝謝,你這個(gè)代碼,我研究研究
要看看你的代碼
回復(fù)@官方研發(fā)實(shí)習(xí)技術(shù)
回復(fù)@官方研發(fā)實(shí)習(xí)技術(shù)

你是要把圖片輸出到瀏覽器嗎?這種代碼是完成不了輸出瀏覽器的
但是這個(gè)圖片,用img標(biāo)簽是可以顯示的,就是直接訪問是亂碼,設(shè)置的header沒起作用
改進(jìn)一下,試一試這個(gè)
回復(fù)@官方研發(fā)實(shí)習(xí)技術(shù) 好吧,那我用單文件吧,單文件是可以輸出顯示的。
改成
回復(fù)@官方研發(fā)實(shí)習(xí)技術(shù) 謝謝,你這個(gè)代碼,我研究研究