user4291359
user4291359

Reputation:

How can i automatically generate thumbnails on a web server?

I am hosting a web server full of images. Theese are loaded programatically through php. To prevent the 100 10mb files to be loaded i have made thumbnails. If i now want to upload a new image i need to manually make a thumbnail. Can i do this automatically once i upload it to the server, or once the php realises that the thumb is missing?

If u need the code i have so far:

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

Upvotes: 3

Views: 3221

Answers (1)

user4291359
user4291359

Reputation:

I found a class called simpleimage by claviska. and my code is now done. it all looks like this.

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

Upvotes: 0

Related Questions