【问题标题】:How can i automatically generate thumbnails on a web server?如何在 Web 服务器上自动生成缩略图?
【发布时间】: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


【解决方案1】:

我找到了 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>";
            }
        ?>

【讨论】:

猜你喜欢
  • 2017-05-18
  • 1970-01-01
  • 2012-12-08
  • 2014-07-02
  • 2018-06-06
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多