Home  >  Article  >  PHP Framework  >  How to load images in thinkphp5

How to load images in thinkphp5

PHPz
PHPzOriginal
2023-04-11 10:33:04847browse

When developing with ThinkPHP5, loading images is a very common requirement. The following will introduce how to load images in ThinkPHP5.

  1. Static resource storage directory

First we need to determine the static resource storage directory. In ThinkPHP5, static resource files are usually stored in the public/static directory.

  1. Reference of pictures

In the template file, we can reference the picture through the following code:

<img src="__STATIC__/img/picture.jpg" alt="picture">

In the above code, __STATIC__ represents Is the absolute path to the public/static directory. Therefore, any file in the public/static directory can be referenced in this way. The images are placed in the public/static/img directory. Just replace img with the name of the folder, such as public/static/css, public/static/js, etc.

  1. Load local images

If you need to load local images, you only need to specify the path of the image in the src attribute of the tag. The code is as follows:

<img src="/static/img/picture.jpg" alt="picture">

It should be noted that the path here is relative to the root path of the domain name, not the path relative to the application root directory. For example, if the application is deployed under the domain name myapp.com, then "/" represents the root path of the domain name myapp.com.

  1. Load network pictures

If you need to load pictures on the network, you only need to specify the URL address of the picture. The code is as follows:

<img src="http://www.example.com/picture.jpg" alt="picture">

Here The path is the complete URL address where the image is located.

In general, loading images in ThinkPHP5 is very convenient. You only need to put the image in the correct location and then make the correct reference in the code. Hope this article is helpful to you.

The above is the detailed content of How to load images in thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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