我目前正在尝试使用php脚本来建立一个html画廊,该脚本会自动调整它们的大小并将其放到画廊中。我现在的问题是大约有30张图片,并且:
那是我正在运行的唯一php代码:
<?php
$directory = "images/gallery/feet";
$images = glob($directory . "/*.jpg");
foreach ($images as $image) {
//getting the image dimensions
list($width, $height) = getimagesize($image);
//saving the image into memory (for manipulation with GD Library)
$image = imagecreatefromjpeg($image);
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
$thumbSize = 500;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $image, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
imagejpeg($thumb, null, 100);
echo "<div class=\"carousel-item\">";
echo "<img class=\"d-block w-100\" src=\"${thumb}\" />";
echo "</div>";
}
?>
你知道我能做什么吗?
答案 0 :(得分:0)
不要在每次页面加载时都为每个图像运行调整大小算法,这是一个非常昂贵的过程。
为每个文件运行一次-永远-将缩略图保存在子目录中。
然后仅提供指向缩略图文件的链接,并让您的Web服务器处理标题,以便正确呈现图像,而不用获取base64字符串。
类似
<?php
$directory = "images/gallery/feet";
$thmb_dir = $directory . "/thumbnail";
$images = glob($directory . "/*.jpg");
foreach ($images as $image) {
if (/*thumbnail doesn't already exist*/) {
//getting the image dimensions
list($width, $height) = getimagesize($image);
//saving the image into memory (for manipulation with GD Library)
$image = imagecreatefromjpeg($image);
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
$thumbSize = 500;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $image, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
imagejpeg($thumb, null, 100);
// Save this image w/ the same name in /thumbnails - I just made up this function name
saveThumb($thumb, $thmb_dir);
}
// Grab the thumbnail with the right name - another made-up function name
$thumb_link = getThumbLink($image)
echo "<div class=\"carousel-item\">";
echo "<img class=\"d-block w-100\" src=\"${thumb_link}\" />";
echo "</div>";
}
?>