Home > php教程 > php手册 > php读取图片内容并输出到浏览器的实现代码

php读取图片内容并输出到浏览器的实现代码

WBOY
Release: 2016-06-06 20:28:07
Original
1756 people have browsed it

如果php以图片,zip,exe等文件输出到浏览器,而前面还输出了其他字符,那就会是你看到的乱码

代码很简单,网上都能找到,美国空间,但在我机子上就是显示不出来,显示出的一直是这个php文件路径,

费了点时间才搞定,原来是我的

网上查了下,有这样一说:
如果php以图片,zip,exe等文件输出到浏览器,而前面还输出了其他字符,虚拟主机,那就会是你看到的乱码。
应该是输出图片前有输出空格或其他字符造成的,可以检查一下输出图片前有没有其他字符,虚拟主机,
如果是utf-8编码记得保存为无BOM的文件。
相关代码如下:

复制代码 代码如下:


class imgdata{
        public $imgsrc;
        public $imgdata;
        public $imgform;
        public function getdir($source){
                $this->imgsrc  = $source;
        }
        public function img2data(){
                $this->_imgfrom($this->imgsrc);
                return $this->imgdata=fread(fopen($this->imgsrc,'rb'),filesize($this->imgsrc));       
        }
        public function data2img(){
                header("content-type:$this->imgform");
                echo $this->imgdata;
                //echo $this->imgform;
                //imagecreatefromstring($this->imgdata);
        }
        public function _imgfrom($imgsrc){
                $info=getimagesize($imgsrc);
                //var_dump($info);
                return $this->imgform = $info['mime'];
        }
}
$n = new imgdata;
$n -> getdir("1.jpg");
$n -> img2data();
$n -> data2img();


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template