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

时间:2015-03-17 11:22:39

标签: php image thumbnails

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

1 个答案:

答案 0 :(得分: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>";
            }
        ?>