Detailed explanation of the steps to directly output images in the PHP browser

php中世界最好的语言
Release: 2023-03-26 14:04:01
Original
2468 people have browsed it

This time I will give you a detailed explanation of the steps to directly output images in the PHP browser. What are the precautions for directly outputting images in the PHP browser? Here is a practical case, let’s take a look.

The easiest way to output images in the browser is of course to use the img tag of HTML and directly pass in the image path or link. But sometimes we need to do some processing on the image, such as changing the image quality or size before displaying it. We can use PHP's built-in image processing function imagecreatefromjpeg (or imagecreatefrompng) from the file or URL Create a new image and imagejpeg (or imagepng), send via header() Content-type: image/jpeg PHP scripts can be made to output JPEG images directly.

/*
 * php 页面直接输出图片
 */
function showImg($img){
  $info = getimagesize($img);
  $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);
}
Copy after login

Can read local image files and remote image links.

NOTE: imagejpeg (or imagepng) support is only available when PHP is compiled with GD-1.8 or higher. quality is optional, jpeg ranges from 0 (worst quality, smaller file size) to 100 (best quality, largest file size), and png ranges from 0 to 9.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to implement WeChat refund application in PHP

ThinkPHP detailed explanation of the steps to implement WeChat payment (jsapi payment)

PHP callback function and anonymous function use case analysis

The above is the detailed content of Detailed explanation of the steps to directly output images in the PHP browser. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!