Tag Archives: thumbnail

PHP: How to Create a Thumbnail from a Video file on an Ubuntu Linux Apache Webserver Programming 26 NOV 2013

green php elephants - elephpantsThanks to the powerful command line ffmpeg package and PHP’s ability to execute server commands through shell_exec, generating a thumbnail image from a given video file becomes a snap!

First, install the ffmpeg package onto your Ubuntu server with:

sudo apt-get install ffmpeg

Once installed, we can then turn our attention to the PHP script that will be doing the slog work. Essentially it uses ffmpeg to process the video file and create the image for us, meaning that in actual fact, the PHP script has very little to do with the actual donkey work!

function createVideoThumbnails($videopath,$imagepath) {
    $status = false;

    // where ffmpeg is located, such as /usr/sbin/ffmpeg
    $ffmpeg = '/usr/bin/ffmpeg';

    // the input video file
    $video = $videopath;

    if (file_exists($video)) {

        // where you'll save the image
        $image = $imagepath;

        // default time to get the image
        $second = 1;

        // get the duration and a random place within the video file
        $cmd = "$ffmpeg -i \"$video\" 2>&1";
        if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
            $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
            $second = rand(1, ($total - 1));

        // get the screenshot
        $cmd = "$ffmpeg -i \"$video\" -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg \"$image\" 2>&1";
        $return = `$cmd`;

        if (file_exists($image)) {
            $status = true;
        } else {
            $status = false;
    } else {
        $status = false;
    return $status;

We make two calls to the ffmpeg function, the first to work out the duration of the video which we then use to make a random call as to where to take the thumbnail from in terms of starting point. The second call actually generates the thumbnail image, using the fully qualified video and image paths that we passed to the function right at the start.

So pretty simple stuff in other words. Note that if you want to specify the size of the generated screenshot, you can make use of the -s parameter. For example:

$cmd = "$ffmpeg -i \"$video\" -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -s 180x135 -f mjpeg \"$image\" 2>&1";


PHP: Generate a Thumbnail from a JPG, PNG or GIF Image File Programming 28 JAN 2011

green php elephants - elephpantsWith PHP’s built in access to the decent GD graphics library, generating a thumbnail image from a larger photo turns out to be pretty simple to achieve. Below is a function you can use to create a thumbnail image from either a JPG, PNG or GIF source image.

To use, simply supply the file path to the source image as well as the desired width (though if not given it will be defaulted to 180 pixels wide). The height of the thumbnail is automatically generated so as to keep aspect ration the same.

The function is question:

function createThumbnail($pathToImage, $thumbWidth = 180) {
    $result = 'Failed';
    if (is_file($pathToImage)) {
        $info = pathinfo($pathToImage);

        $extension = strtolower($info['extension']);
        if (in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) {

            switch ($extension) {
                case 'jpg':
                    $img = imagecreatefromjpeg("{$pathToImage}");
                case 'jpeg':
                    $img = imagecreatefromjpeg("{$pathToImage}");
                case 'png':
                    $img = imagecreatefrompng("{$pathToImage}");
                case 'gif':
                    $img = imagecreatefromgif("{$pathToImage}");
                    $img = imagecreatefromjpeg("{$pathToImage}");
            // load image and get image size

            $width = imagesx($img);
            $height = imagesy($img);

            // calculate thumbnail size
            $new_width = $thumbWidth;
            $new_height = floor($height * ( $thumbWidth / $width ));

            // create a new temporary image
            $tmp_img = imagecreatetruecolor($new_width, $new_height);

            // copy and resize old image into new image
            imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                $pathToImage = $pathToImage . '.thumb.' . $extension;
            // save thumbnail into a file
            imagejpeg($tmp_img, "{$pathToImage}");
            $result = $pathToImage;
        } else {
            $result = 'Failed|Not an accepted image type (JPG, PNG, GIF).';
    } else {
        $result = 'Failed|Image file does not exist.';
    return $result;

//calling the function
$result = explode('|',createThumbnail($sourceImagePath, 180));
            if ($result[0] != 'Failed'){
                header('Location: /thumbnails/' . basename($result[0]));
            } else {
                header('Location: error.gif');

As you can see, the function above returns a string, with the string containing ‘Failed’ if the thumbnail generation has failed for some reason, otherwise returning the path to the generated thumbnail image.


Image Resize: TimThumb PHP Script CodeUnit 17 DEC 2009

PHP-logoTimThumb, created and maintained by Tim McDaniels, Ben Gillbanks and Darren Hoyt, is a rather popular open source, image resizing PHP script, mostly used for generating thumbnails for blogging purposes on the fly. The script is fairly powerful, executes quickly and needs almost nothing from a developer in order to get it up and running on their own project.

The script makes use of the GD library to resize images based on the parameters used when calling the script. Thumbnails maintain aspect ratio and the script deals with jpg, gif and png files pretty well. It also has a caching system built in which helps keeps things fast.

In terms of parameters, the script allows you to set any of these for an image to be resized: