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

How can i automatically generate thumbnails on a web server?

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

我发现了一个 class 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>";
            }
        ?>