PHP converts videos, screenshots, and thumbnails Based on ffmpeg.

Source: Internet
Author: User
Tags imagejpeg

PHP converts videos Based on ffmpeg and generates thumbnails.

This example describes how PHP converts videos Based on ffmpeg and generates thumbnails. We will share this with you for your reference. The details are as follows:

Here we integrate ffmpeg with the thumbnail generation:

Include ("ImageResize. class. php ") // convert video $ cmd =" ffmpeg.exe-I starwar. avi-AB 56-ar 22050-B 500-r 15-s 320x240 1.flv"; exec ($ cmd); // video $ cmd = "ffmpeg.exe-I starwar. avi-f image2-ss 10-s 400*300-vframes 1 1.jpg"; exec ($ cmd); // generate a thumbnail $ thumbnail = new ImageResize (); $ thumbnail-> resizeimage ("1.jpg", 30,30, 0," small1.jpg "); class ImageResize {// image type var $ type; // actual width var $ width; // actual height var $ height; // Var $ resize_width after the image is changed; // var $ resize_height after the image is changed; // whether to cut the image var $ cut; // var $ srcimg of the source image; // target image address var $ dstimg; // a newly created image var $ im; function resizeimage ($ img, $ wid, $ hei, $ c, $ dstpath) {$ this-> srcimg = $ img; $ this-> resize_width = $ wid; $ this-> resize_height = $ hei; $ this-> cut = $ c; // image type $ this-> type = strtolower (substr (strrchr ($ this-> srcimg ,". "), 1); // initialize the image $ this-> initi_img (); // target image address $ this-> Dst_img ($ dstpath); // -- $ this-> width = imagesx ($ this-> im); $ this-> height = imagesy ($ this-> im ); // generate an image $ this-> newimg (); ImageDestroy ($ this-> im);} function newimg () {// ratio of the changed image $ resize_ratio = ($ this-> resize_width)/($ this-> resize_height ); // ratio of the actual image $ ratio = ($ this-> width)/($ this-> height); if ($ this-> cut) = "1 ") {// if ($ ratio >=$ resize_ratio) {$ newimg = imagecreatetruecolor ($ this-> resize_width, $ This-> resize_height); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, $ this-> resize_height, ($ this-> height) * $ resize_ratio), $ this-> height); ImageJpeg ($ newimg, $ this-> dstimg );} // if ($ ratio <$ resize_ratio) {$ newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, $ this-> resize _ Height, $ this-> width, ($ this-> width)/$ resize_ratio); ImageJpeg ($ newimg, $ this-> dstimg );}} else {// undo graph if ($ ratio >=$ resize_ratio) {$ newimg = imagecreatetruecolor ($ this-> resize_width, ($ this-> resize_width)/$ ratio ); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, ($ this-> resize_width)/$ ratio, $ this-> width, $ this-> height); ImageJpeg ($ newimg, $ this-> dstimg);} if ($ ratio <$ res Ize_ratio) {$ newimg = imagecreatetruecolor ($ this-> resize_height) * $ ratio, $ this-> resize_height); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, ($ this-> resize_height) * $ ratio, $ this-> resize_height, $ this-> width, $ this-> height ); imageJpeg ($ newimg, $ this-> dstimg) ;}}// initialize the image function initi_img () {if ($ this-> type = "jpg ") {$ this-> im = imagecreatefromjpeg ($ this-> srcimg);} if ($ this-> type = "gif ") {$ This-> im = imagecreatefromgif ($ this-> srcimg);} if ($ this-> type = "png ") {$ this-> im = imagecreatefrompng ($ this-> srcimg);} if ($ this-> type = "bmp ") {$ this-> im = $ this-> imagecreatefrombmp ($ this-> srcimg) ;}// function dst_img ($ dstpath) {$ full_length = strlen ($ this-> srcimg); $ type_length = strlen ($ this-> type); $ name_length = $ full_length-$ type_length; $ name = substr ($ this-> srcimg, 0, $ name_le Ngth-1); $ this-> dstimg = $ dstpath; // echo $ this-> dstimg;} function ConvertBMP2GD ($ src, $ dest = false) {if (! ($ Src_f = fopen ($ src, "rb") {return false;} if (! ($ Dest_f = fopen ($ dest, "wb") {return false;} $ header = unpack ("vtype/Vsize/v2reserved/Voffset", fread ($ src_f, 14); $ info = unpack ("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread ($ src_f, 40); extract ($ info); extract ($ header); if ($ type! = 0x4D42) {// signature "BM" return false;} $ palette_size = $ offset-54; $ ncolor = $ palette_size/4; $ gd_header = ""; // true-color. palette $ gd_header. = ($ palette_size = 0 )? "\ XFF \ xFE": "\ xFF"; $ gd_header. = pack ("n2", $ width, $ height); $ gd_header. = ($ palette_size = 0 )? "\ X01": "\ x00"; if ($ palette_size) {$ gd_header. = pack ("n", $ ncolor);} // no transparency $ gd_header. = "\ xFF"; fwrite ($ dest_f, $ gd_header); if ($ palette_size) {$ palette = fread ($ src_f, $ palette_size ); $ gd_palette = ""; $ j = 0; while ($ j <$ palette_size) {$ B = $ palette {$ j ++ }; $ g = $ palette {$ j ++}; $ r = $ palette {$ j ++}; $ a = $ palette {$ j ++}; $ gd_palette. = "$ r$ g $ B $ a" ;}$ gd_palette. = str _ Repeat ("\ x00 \ x00 \ x00 \ x00", 256-$ ncolor); fwrite ($ dest_f, $ gd_palette);} $ scan_line_size = ($ bits * $ width) + 7)> 3; $ scan_line_align = ($ scan_line_size & 0x03 )? 4-($ scan_line_size & 0x03): 0; for ($ I = 0, $ l = $ height-1; $ I <$ height; $ I ++, $ l --) {// BMP stores scan lines starting from bottom fseek ($ src_f, $ offset + ($ scan_line_size + $ scan_line_align) * $ l )); $ scan_line = fread ($ src_f, $ scan_line_size); if ($ bits = 24) {$ gd_scan_line = ""; $ j = 0; while ($ j <$ scan_line_size) {$ B = $ scan_line {$ j ++}; $ g = $ scan_line {$ j ++}; $ r = $ scan_line {$ j ++}; $ gd _ Scan_line. = "\ x00 $ r$ g $ B" ;}} else if ($ bits = 8) {$ gd_scan_line = $ scan_line;} else if ($ bits = 4) {$ gd_scan_line = ""; $ j = 0; while ($ j <$ scan_line_size) {$ byte = ord ($ scan_line {$ j ++ }); $ p1 = chr ($ byte> 4); $ p2 = chr ($ byte & 0x0F); $ gd_scan_line. = "$ p1 $ p2";} $ gd_scan_line = substr ($ gd_scan_line, 0, $ width);} else if ($ bits = 1) {$ gd_scan_line = ""; $ j = 0; while ($ j <$ scan_line_size) {$ Byte = ord ($ scan_line {$ j ++}); $ p1 = chr (int) ($ byte & 0x80 )! = 0); $ p2 = chr (int) ($ byte & 0x40 )! = 0); $ p3 = chr (int) ($ byte & 0x20 )! = 0); $ p4 = chr (int) ($ byte & 0x10 )! = 0); $ p5 = chr (int) ($ byte & 0x08 )! = 0); $ p6 = chr (int) ($ byte & 0x04 )! = 0); $ p7 = chr (int) ($ byte & 0x02 )! = 0); $ p8 = chr (int) ($ byte & 0x01 )! = 0); $ gd_scan_line. = "$ p1 $ p2 $ p3 $ p4 $ p5 $ p6 $ p7 $ p8";} $ gd_scan_line = substr ($ gd_scan_line, 0, $ width );} fwrite ($ dest_f, $ gd_scan_line);} fclose ($ src_f); fclose ($ dest_f); return true;} function imagecreatefrombmp ($ filename) {$ tmp_name = tempnam ("/tmp", "GD"); if ($ this-> ConvertBMP2GD ($ filename, $ tmp_name )) {$ img = imagecreatefromgd ($ tmp_name); unlink ($ tmp_name); return $ img;} return false ;}}

Appendix:Click here for the complete instance codeDownload from this site.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.