如何在Web服务器上自动生成缩略图?

3

我正在托管一个充满图片的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
@批量操作好的。谢谢。我会再深入挖掘一下,看看能找到什么。我会带着我的结果回来! - user4291359
@Bulk 我测试了一些代码。http://php.net/manual/en/function.imagecopyresampled.php,两个示例都只是输出一堆随机文本和问号图标,而且第一个示例很快就会崩溃。 - user4291359
1个回答

0
我发现了一个叫做claviska的simpleimage类,我的代码现在已经完成了。它看起来像这样。

The code that takes care of the thumbnails:

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

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

The code that loads the images:

        <?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>";
            }
        ?>


1
缩略图生成的代码在哪里? - jordan
这是未包含在此答案中的代码:https://packagist.org/packages/abeautifulsite/simpleimage(您可以使用`composer require abeautifulsite/simpleimage`) - Jimmy Adaro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接