1. 准备相册图片

2. 创建PHP文件
3. 使用PHP生成缩略图
4. 显示缩略图
1. 准备相册图片
我们需要将所有要展示的图片放在一个目录下。例如,我们创建一个名为 `gallery` 的文件夹,并将图片放在其中。
```
gallery/
- image1.jpg
- image2.jpg
- image3.jpg
...
```
2. 创建PHP文件
接下来,创建一个名为 `thumbnail_gallery.php` 的PHP文件。
```php
// 设置图片文件夹路径
$gallery_folder = 'gallery/';
// 获取文件夹中所有文件
$files = scandir($gallery_folder);
// 筛选出图片文件
$image_files = array_filter($files, function($file) {
return preg_match('/"".(jpg|jpeg|png|gif)$/', $file);
});
// 图片宽度
$image_width = 100;
// 生成缩略图路径
$thumbnail_folder = 'gallery/thumbnails/';
if (!is_dir($thumbnail_folder)) {
mkdir($thumbnail_folder, 0777, true);
}
// 生成缩略图
foreach ($image_files as $image_file) {
$image_path = $gallery_folder . $image_file;
$thumbnail_path = $thumbnail_folder . $image_file;
// 获取图片信息
$image_info = getimagesize($image_path);
// 创建新图像
switch ($image_info[2]) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($image_path);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($image_path);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($image_path);
break;
default:
continue;
}
// 计算缩略图尺寸
$image_width = $image_info[0];
$image_height = $image_info[1];
$ratio = $image_width / $image_height;
$thumbnail_width = $image_width > $image_width ? $image_width : $image_width;
$thumbnail_height = $image_height * ($thumbnail_width / $image_width);
// 创建缩略图
$thumbnail = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $image_width, $image_height);
// 输出图片
imagejpeg($thumbnail, $thumbnail_path);
}
>
```
3. 使用PHP生成缩略图
在上面的PHP代码中,我们遍历了图片文件夹中的所有图片,并为它们生成了缩略图。缩略图会被保存在 `gallery/thumbnails/` 目录下。
4. 显示缩略图
我们可以创建一个简单的HTML页面来显示这些缩略图。
```html
本文由 @一份思念 发布在 共鸣看车网,如有疑问,请联系我们。
文章链接:http://www.gmkc3z.cn/xJWMzb_MGRmBruyBRSvtb









