我正在托管一个充满图片的Web服务器,这些图片是通过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>";
}
getThumb
中使用file_exists
函数检查缩略图是否存在,如果不存在则创建它。在谷歌上有很多关于从图像制作缩略图的资料。 - Dan Smith