【发布时间】:2021-08-21 20:09:14
【问题描述】:
我是一名 PHP 初学者,我正在尝试构建一个图库页面,该页面将输出文件夹中所有 195 个图像的缩略图。这些图像平均每个 8 MB。这是目前的情况:
脚本文件夹中的php.ini
allow_url_fopen = On
display_errors = On
enable_dl = Off
file_uploads = On
max_execution_time = 999
max_input_time = 999
max_input_vars = 1000
memory_limit = 999M
post_max_size = 516M
session.gc_maxlifetime = 1440
session.save_path = "/var/cpanel/php/sessions/ea-php74"
upload_max_filesize = 512M
zlib.output_compression = Off
PHP/HTML 代码
<?php
DEFINE('UPLOAD_DIR', 'sources/');
DEFINE('THUMB_DIR', 'thumbs/');
function GenerateThumbnail($src, $dest)
{
$Imagick = new Imagick($src);
$bigWidth = $Imagick->getImageWidth();
$bigHeight = $Imagick->getImageHeight();
$scalingFactor = 230 / $bigWidth;
$newheight = $bigHeight * $scalingFactor;
$Imagick->thumbnailImage(230,$newheight,true,true);
$Imagick->writeImage($dest);
$Imagick->clear();
return true;
}
// Get list of files in upload dir
$arrImageFiles = scandir(UPLOAD_DIR);
// Remove non-images
$key = array_search('.', $arrImageFiles);
if ($key !== false)
unset($arrImageFiles[$key]);
$key = array_search('..', $arrImageFiles);
if ($key !== false)
unset($arrImageFiles[$key]);
$key = array_search('.ftpquota', $arrImageFiles);
if ($key !== false)
unset($arrImageFiles[$key]);
$key = array_search('thumbs', $arrImageFiles);
if ($key !== false)
unset($arrImageFiles[$key]);
?><!DOCTYPE HTML>
<html lang="fr">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Select Image</title>
</head>
<body>
<?php
foreach($arrImageFiles as $imageFile)
{
$thumbFullPath = THUMB_DIR . "th_" . $imageFile;
$imageFullPath = UPLOAD_DIR . $imageFile;
if (! file_exists($thumbFullPath))
{
GenerateThumbnail($imageFullPath, $thumbFullPath);
}
echo "<img alt='' src='" . $thumbFullPath . "'>";
}
?>
</body>
</html>
我不知道如何解决的两个问题是:
这是我已经尝试过的:
感谢您的任何想法,我有点迷路了。
【问题讨论】:
-
看起来服务器超时有问题。过程停止需要多长时间?
-
@DavidPauli -- 感谢您的评论。加载时间变化很大。我测试了页面加载时间,得到:48s、123s、57s、115s。我很困惑!
-
“脚本文件夹中的php.ini” 为什么那个文件会有影响?
-
@Olivier - 感谢您的参与。正如我所说,我是一个新手,我理解(可能是错误的?)在那里放置一个 php.ini 文件可以让我修改一些参数(执行时间,内存限制等)你有什么建议吗?我渴望学习。
标签: php html imagemagick thumbnails