Direct output of images in PHP browser steps

Source: Internet
Author: User
Tags imagejpeg
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.