php reads the image content and outputs it to the browser

  • 2020-08-22 21:56:07
  • OfStack

The code is very simple, you can find it on the Internet, but it just doesn't show up on my machine. The 1 shown is the path of this php file.

It took me a while to figure it out. It's mine < ? php this label has many Spaces in front, delete ok, details, very careless, really speechless.

Check the Internet, there is such a 1 said:
If php prints images,zip,exe, and so on to the browser with other characters in front of it, you'll see a mess.
It should be caused by the output space or other characters before the output of the picture. You can check whether there are other characters before the output of the picture.
If it's es15EN-8, remember to save it as a file without BOM.
The relevant codes are as follows:


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 articles: