【发布时间】:2019-02-24 05:25:32
【问题描述】:
我正在托管一个充满图像的网络服务器。 Theese 通过 php 以编程方式加载。为了防止加载 100 个 10mb 文件,我制作了缩略图。如果我现在想上传新图像,我需要手动制作缩略图。一旦我将它上传到服务器,或者一旦 php 意识到拇指丢失,我可以自动执行此操作吗?
如果你需要我目前拥有的代码:
function getThumb($file)
{
//Strip the file down to its name.
//$suffix = pathinfo($file,PATHINFO_EXTENSION); //Depricated.
//$filename = basename($file); //Depricated.
$filename = pathinfo($file,PATHINFO_FILENAME);
$thumbpath = "imgres/posts/thumb/";
$thumbnail = $thumbpath.$filename.".jpg";
return $thumbnail;
}
//Image loader
//Folder containing images.
$filedir = "imgres/posts/";
//Retrieve the images in the folder.
$images = glob($filedir."*.{jpg,JPG,png,PNG,gif,GIF}",GLOB_BRACE);
//Make sure the image array is not empty, or null.
if (!empty($images))
{
//Load the images into the website.
foreach ($images as $image)
{
if (pathinfo($image,PATINFO_EXTENSION) == "gif" || pathinfo($image, PATHINFO_EXTENSION))
{
echo '<a href="'.$image.'"><img src="'.$image.'"/></a>';
}
else
{
echo '<a href="'.$image.'"><img src="'.getThumb($image).'"/></a>';
}
}
}
else
{
//Write out an error message to warn the user.
echo "<p>No images were found in the server. This is most likely an error in the PHP code, incompatibility, or something went wrong with our storage solution. Contact admin! Information needed: Browser, OS, and has the site worked before?</p>";
}
【问题讨论】:
-
是吗?您已经获得了大部分核心代码,只需使用
file_exists函数检查缩略图是否存在于getThumb中,如果它没有创建它。谷歌上有很多从图像制作缩略图的方法。 -
@Bulk 好的。谢谢。我会再挖掘一点,看看我能找到什么。我会带着我的结果回来的!
-
@Bulk 我测试了一些代码。 php.net/manual/en/function.imagecopyresampled.php,这两个示例都显示了一堆随机文本和问号图标,不久之后也出现了第一个崩溃。
标签: php image thumbnails