How to output images directly in the browser with PHP

php中世界最好的语言
Release: 2023-03-22 06:56:01
Original
10201 people have browsed it

This time I will show you how PHP can directly output pictures in the browser. What are the precautions for PHP to directly output pictures in the browser? Here is the actual combat Let’s take a look at the case.

The example in this article describes the method of directly outputting images in the browser using PHP. Share it with everyone for your reference, the details are as follows:

The easiest way to output the image 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

You can read this map file, and you can also read 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:

PHP directly implements the generation of poster advertisements

detailed explanation of phpqrcode class to generate QR code

mysqld_multi deployment stand-alone detailed explanation

The above is the detailed content of How to output images directly in the browser with PHP. 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