This time to bring you a PHP browser directly output picture steps in detail, the php browser directly output the picture of the attention of the matter, the following is the actual case, together to see.
The simplest way to output a picture in a browser is to use the HTML IMG tag to pass directly to the image path or link. But sometimes we need to do some processing of the picture, such as changing the quality or size to show again, we can use the PHP built-in image processing function imagecreatefromjpeg
(or imagecreatefrompng
) by a file or URL to create a new image and imagejpeg
/or imagepng
, by header()
sending Content-type:image/jpeg allows the PHP script to output JPEG images directly.
/* * PHP page output image directly */function showimg ($img) { $info = getimagesize ($img); $IMGEXT = Image_type_to_extension ($info [2], false); Gets the file suffix $fun = "imagecreatefrom{$imgExt}"; $imgInfo = $fun ($img); 1. Create a new image from a file or URL. such as: Imagecreatefrompng (string $filename) //$mime = $info [' mime ']; $mime = Image_type_to_mime_type (Exif_imagetype ($img)); Gets the MIME type of the picture header (' Content-type: '. $mime); $quality = +; if ($imgExt = = ' png ') $quality = 9; Output quality, JPEG format (0-100), PNG format (0-9) $getImgInfo = "image{$imgExt}"; $getImgInfo ($imgInfo, NULL, $quality); 2. Export the image to a browser or file. such as: imagepng (Resource $image) Imagedestroy ($imgInfo);}
You can read the local picture file, or you can read the remote picture link.
Note: imagejpeg
(or imagepng
) support is only available when PHP is compiled with GD-1.8 or later versions. quality is optional, JPEG ranges from 0 (worst quality, file size) to 100 (best quality, File max), PNG range from 0 to 9.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP Implementation request refund steps in detail
thinkphp Implementing Payment (JSAPI Payment) steps
PHP callback function and anonymous function use case resolution