ThinkPHP is a popular PHP framework, in which outputting pictures is also a very common function. Today we will discuss how to output images in ThinkPHP.
- Output pictures through HTML tags
In ThinkPHP, we can use HTML to output pictures. We can specify the image path by using the src attribute of the img tag in the template file and output the content of the image to the browser.
<img src="/static/imghwm/default1.png" data-src="http://www.example.com/image.jpg" class="lazy" alt="How thinkphp outputs pictures" >
Note that this method is only suitable for outputting external pictures and cannot be used to output internal pictures.
- Output pictures through PHP code
If you want to output internal pictures, or want to more flexibly control the way of outputting pictures, we can use PHP code to output pictures . Below is a simple code example.
public function showImage() { $path = './uploads/image.jpg'; header('Content-type: image/jpeg'); readfile($path); }
In this example, we first specify the path of the image to be output, and then set the Content-Type header to "image/jpeg", which will tell the browser that this is a JPEG format image. . Finally, we use the readfile function to output the image content to the browser.
- Processing dynamic output images
If we need to dynamically generate images, such as allowing users to upload their own avatars to the website and adjust them to the corresponding size For output, we need to use more advanced techniques.
ThinkPHP provides a class called "Think\Image" to handle image processing and output. We can use this class to dynamically generate images and output them to the browser. The following is a basic example:
public function showImage() { $image = new \Think\Image(); $image->open('./uploads/image.jpg'); $image->thumb(150, 150); $image->save(); header('Content-type: image/jpeg'); readfile($path); }
In this example, we first open an image using the open method. We then use the thumb method to resize the image to 150x150 pixels and save the processed image. Finally, we use the readfile function to output the image content to the browser. It should be noted that we still need to specify Content-Type as "image/jpeg" in the header.
Summary
The above are the three methods of outputting images in ThinkPHP. You can choose different methods based on specific usage scenarios. Whether it is simply outputting external images, or dynamically generating and processing images, ThinkPHP has corresponding solutions.
The above is the detailed content of How thinkphp outputs pictures. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
