Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zanakka's avatar

What is the best way thumbnail image for video?

Hello,

According to my project, I need to upload video files at the back-end and will display these video files at the front-end(as like laracasts).

So in this case, the thumbnail image for those uploaded single videos, should I ask user to upload single thumbnail image for each video file when they create. Or is it possible to generate thumbnail image from video files?

Thanks, Kyaw

0 likes
8 replies
kingpabel's avatar

First install ffmpeg-php (http://ffmpeg-php.sourceforge.net/)

$frame = 10;
$movie = 'test.mp4';
$thumbnail = 'thumbnail.png';

$mov = new ffmpeg_movie($movie);
$frame = $mov->getFrame($frame);
if ($frame) {
    $gd_image = $frame->toGDImage();
    if ($gd_image) {
        imagepng($gd_image, $thumbnail);
        imagedestroy($gd_image);
        echo '<img src="'.$thumbnail.'">';
    }
}
robgeorgeuk's avatar

I would probably do both. Give the user an option to upload a thumbnail image otherwise fallback to generating one as per @kingpabel code. It's common to overlay a play button too so it's obvious that this is a video.

pmall's avatar

There is many command line tools for generating thumbnails. Generate some when the video is saved on your server and let the user choose one.

christiangerdes's avatar

I use Cloudinary for my images. They are awesome and also very helpfull!

1 like
martinbean's avatar

@kyawzinwin Do you transcode videos to a common format after the user uploads them? I use Amazon’s Elastic Transcoder for video uploads in my video on demand site, which generates thumbnails from stills at every n seconds (can’t remember the exact interval) and stored them in an S3 bucket. I then present these thumbnails as a “gallery” to allow my users to select the one they want to use (similar to YouTube, but they only give you three options last time I used it).

ganeshghalame's avatar

I recently implemented the thumbnail generation for Video using FFMPEG(https://github.com/PHP-FFMpeg/PHP-FFMpeg)

To generate the thumbnail from the video you need, duration of video so that you can get any from between this duration. I implemented the logic to get 3 frame durations from the video, I am showing 3 thumbnails by rotating them on hover of thumbnail like youtube. To get the dynamic frame durations from the video you can write below function which returns 3 frame durations for thumbnails.

Use below code to get duration from video:

$ffprobe = FFMpeg\FFProbe::create();
$duration = $ffprobe->streams($file) // extracts streams informations
        ->videos()                      // filters video streams
        ->first()                       // returns the first video stream
        ->get('duration');

Use below code to get 3 frame durations: $thumbnailIntervalTimeArr = $this->getThumbnailIntervalTimeArr($duration); private function getThumbnailIntervalTimeArr($duration){ $durationInSec = intval($duration); $intervalArr = range(0,$duration, $duration/4); return array_slice($intervalArr, 1, -1); }

Now you have to generate the thumbnails for $thumbnailIntervalTimeArr, you can do it using below loop:

$counter = 1;
foreach ($thumbnailIntervalTimeArr as $interval){
    $framePath = $this->contentBasePath."/frame_$counter.jpg";
    $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($interval))->save($framePath);
     // use code listen below code is optional to generate the thumbnail image from frame
        $counter++;
    }

Above code will generate the 3 thumbnail frames which has same resolutions as the Video, best practice is to save the thumbnail by keeping its aspect ratio and with less size so that it can load faster, and to achieve that I used http://image.intervention.io/

$image = Image::make($imagePath)->encode('jpg', 50);
$image->resize($width, $height, function ($constraint) {
        $constraint->aspectRatio();
});
$image->save("some path.jpg");

Please or to participate in this conversation.