When I was building my own personal website recently, I encountered a problem: how to store and display images on the website. Generally speaking, we can directly upload images to the server of the website, but considering the scalability and performance of the website, I hope to store the images on a dedicated image server and call the display images through php operations.
Below, I will introduce in detail how to build a picture server and call it in php to display pictures.
First, we need to choose a picture server software. Here, I chose the free open source software Piwigo. It is an image management system based on PHP and MySQL that supports uploading, classification, tags, search, sharing and other functions. At the same time, it also has the characteristics of global support, scalability, and personalized design.
Next, we follow the following steps to build the image server:
1. Prepare the server environment
Before we start, we need a Linux-based server environment and install Apache, PHP and MySQL. You can use a distribution you are familiar with, such as Ubuntu, CentOS, etc.
2. Download Piwigo
You can download the latest version of Piwigo from the Piwigo official website (https://piwigo.org/download/) and unzip it to the specified directory. For example, we extract it to the /var/www/html/piwigo directory.
3. Create a database
Create a database named piwigo in MySQL. You can create the database using the MySQL command line tool or using phpMyAdmin (if you have it installed).
4. Configure Piwigo
We need to set up Piwigo's configuration file so that it can connect to the MySQL database. We need to edit the /var/www/html/piwigo/local/config/config.inc.php file and fill in the following information:
$conf['dbCharset'] = 'utf8mb4'; $conf['dbHost'] = 'localhost'; // 数据库所在的地址 $conf['dbName'] = 'piwigo'; // 数据库名 $conf['dbUser'] = 'root'; // 数据库用户名 $conf['dbPassword'] = 'password'; // 数据库密码 $conf['prefixeTable'] = 'piwigo_'; // 数据表的前缀
5. Install Piwigo
Open your website and visit / piwigo/install.php, install according to the interface instructions. When initializing the database, you need to enter the user name and password of the database.
6. Configure image storage path
By default, Piwigo stores all uploaded images in /var/www/html/piwigo/galleries. If you want to customize the storage path, you can modify it. The following parameters in the configuration file /var/www/html/piwigo/local/config/config.inc.php:
$conf['dir_photos'] = PWG_LOCAL_DIR . 'photos/'; // 图片存储路径 $conf['dir_resized'] = PWG_LOCAL_DIR . 'upload/'; // 调整后的图片存储路径
7. Upload images
Now, we can use the web version of Piwigo to upload images . In Piwigo's management interface, click the "Upload" button on the left and select the image you want to upload.
8. Calling pictures
Finally, let’s look at how to call display pictures in php. Piwigo provides a set of APIs that allow us to query image and album information. We can find the relevant functions in /var/www/html/piwigo/include/ws_functions.inc.php.
For example, to get all the picture information in a certain album, you can use the following code:
// 引入 Piwigo 的 API require_once('/var/www/html/piwigo/include/ws_functions.inc.php'); // 获取相册 ID,这里假设为 1 $cat_id = 1; // 调用 API 获取相册中的所有图片信息 $photos = ws_getPhotos(array( 'cat_id' => $cat_id, 'nb_results' => 999, // 最多返回 999 张图片 )); // 遍历所有图片信息,输出相应的 HTML 代码 foreach ($photos as $photo) { echo '<a href="'.get_pwg_image_url($photo['id']).'">'; echo '<img src="'.get_thumbnail_url($photo['id']).'">'; echo '</a>'; }
In the above code, we first introduced Piwigo's API in /var/www/html/ piwigo/include/ws_functions.inc.php file, then obtain the album ID, call the API to obtain all picture information in the album, and finally traverse all picture information and output the corresponding HTML code.
At this point, we have completed the construction of the image server and the image display operation. Of course, you can further optimize the server environment and add caching mechanisms and other measures to improve website performance and provide a better user experience.
The above is the detailed content of How to build a picture server in php. For more information, please follow other related articles on the PHP Chinese website!