gpt4 book ai didi

php - 我怎样才能在网络服务器上自动生成缩略图?

转载 作者:行者123 更新时间:2023-12-05 07:55:57 26 4
gpt4 key购买 nike

我正在托管一个充满图像的网络服务器。这些是通过 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>";
}

最佳答案

我找到了 claviska 的一个叫做 simpleimage 的类。我的代码现在已经完成了。一切看起来像这样。

处理缩略图的代码:

    function isThumb($thumbnail)
{
return file_exists($thumbnail);
}

function makeThumb($original, $destination)
{
$img = new abeautifulsite\SimpleImage($original);
$img->fit_to_width(256);
$img->save($destination);
}
?>

加载图片的代码:

        <?php
//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)=="GIF")
{
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>";
}
?>

关于php - 我怎样才能在网络服务器上自动生成缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com