Ticket #2804: mythweb.patch

File mythweb.patch, 7.2 KB (added by spencercw@…, 20 years ago)
  • mythweb/includes/db_update.php

    diff -urNp a/mythweb/includes/db_update.php b/mythweb/includes/db_update.php
    a b  
    4040            case 1:
    4141                setting('WebPrefer_Channum', null, 1);
    4242                setting('WebDBSchemaVer',    null, ++$db_vers);
     43        // Table for the timestamps for the video covers, if it changes we need to update the thumbnail
     44            case 2:
     45                $db->query('CREATE TABLE mythweb_video_thumbs (
     46                                videoid         INT(10) UNSIGNED NOT NULL PRIMARY KEY,
     47                                modified        INT(11) UNSIGNED NOT NULL,
     48                                width           INT(3) UNSIGNED NOT NULL,
     49                                height          INT(3) UNSIGNED NOT NULL
     50                            )');
     51                setting('WebDBSchemaVer', null, ++$db_vers);
    4352        // All other numbers should run their changes sequentially
    44             #case 2:
     53            #case 3:
    4554            #    # do something to upgrade the database here
    4655            #    $db_vers++;
    4756        }
  • mythweb/modules/video/handler.php

    diff -urNp a/mythweb/modules/video/handler.php b/mythweb/modules/video/handler.php
    a b  
    8484                        .' to point to the correct location.');
    8585        }
    8686    }
     87// Do it all again for the video cover thumbnails directory
     88    if (file_exists('data/video_cover_thumbs')) {
     89        if (!is_dir('data/video_cover_thumbs')) {
     90            custom_error('An invalid file exists at data/video_cover_thumbs.  Please'
     91                        .' remove it in order to use the video portions of MythWeb.');
     92        }
     93    } else {
     94        if (!mkdir('data/video_cover_thumbs')) {
     95            custom_error('Could not create directory data/video_cover_thumbs. Please create it'
     96                        .' manually in order to use the video portions of MythWeb.');
     97        }
     98    }
    8799
    88100// Editing?
    89101    if ($Path[1] == 'edit') {
     
    184196    if (count($All_Shows))
    185197        sort_programs($All_Shows, 'video_sortby');
    186198
     199// Generate video cover thumbnails (if necessary)
     200    if (!function_exists('imagecreate')) {
     201        custom_error("Please install the GD library for PHP.\n"
     202                    .'The package is usually called something like php-gd.');
     203    }
     204    function generate_thumbnail($videoid, $cover_filename, $cover_thumbnail_filename, $last_modified) {
     205        global $db, $Last_Modified;
     206        $filetype = trim(`file -bi "$cover_filename"`);
     207        switch ($filetype) {
     208            case 'image/jpeg':
     209                $im = imagecreatefromjpeg($cover_filename);
     210                break;
     211            case 'image/png':
     212                $im = imagecreatefrompng($cover_filename);
     213                break;
     214            case 'image/gif':
     215                $im = imagecreatefromgif($cover_filename);
     216                break;
     217            default:
     218                return false;
     219        }
     220        list($width, $height) = getimagesize($cover_filename);
     221        $thumb = imagecreatetruecolor(video_img_width, video_img_height);
     222        imagecopyresampled($thumb, $im, 0, 0, 0, 0, video_img_width, video_img_height, $width, $height);
     223        imagejpeg($thumb, $cover_thumbnail_filename, 100);
     224        if (isset($Last_Modified[$videoid]['modified'])) {
     225            $sh = $db->query('UPDATE mythweb_video_thumbs SET modified='.$last_modified.', width='.video_img_width.', height='.video_img_height.' WHERE videoid='.$videoid);
     226            $sh->finish();
     227        } else {
     228            $sh = $db->query('INSERT INTO mythweb_video_thumbs (videoid, modified, width, height) VALUES (\''.$videoid.'\', \''.$last_modified.'\', '.video_img_width.', '.video_img_height.')');
     229            $sh->finish();
     230        }
     231        return true;
     232    }
     233    $Last_Modified = array();
     234    $sh = $db->query('SELECT * FROM mythweb_video_thumbs');
     235    while ($row = $sh->fetch_assoc()) {
     236        $Last_Modified[$row['videoid']] = array ('modified' => $row['modified'], 'dimensions' => array ('w' => $row['width'], 'h' => $row['height']));
     237    }
     238    $sh->finish();
     239    foreach ($All_Shows as $show) {
     240        $cover_filename = 'data/video_covers/'.basename($show->coverfile);
     241        $cover_thumbnail_filename = 'data/video_cover_thumbs/'.basename($show->coverfile);
     242    // Do we even need a thumbnail?
     243        if (file_exists($cover_filename)) {
     244            $Image_Last_Modified = filemtime($cover_filename);
     245            if (file_exists($cover_thumbnail_filename)) {
     246            // Update thumbnail
     247                if (isset($Last_Modified[$show->intid])) {
     248                    if ($Last_Modified[$show->intid]['modified'] != $Image_Last_Modified)
     249                        generate_thumbnail($show->intid, $cover_filename, $cover_thumbnail_filename, $Image_Last_Modified);
     250                    else if ($Last_Modified[$show->intid]['dimensions']['w'] != video_img_width || $Last_Modified[$show->intid]['dimensions']['h'] != video_img_height)
     251                        generate_thumbnail($show->intid, $cover_filename, $cover_thumbnail_filename, $Image_Last_Modified);
     252                } else
     253                    generate_thumbnail($show->intid, $cover_filename, $cover_thumbnail_filename, $Image_Last_Modified);
     254            } else
     255                    generate_thumbnail($show->intid, $cover_filename, $cover_thumbnail_filename, $Image_Last_Modified);
     256        } else {
     257        // Delete any old thumbnails
     258            $sh = $db->query('DELETE IGNORE FROM mythweb_video_thumbs WHERE videoid = '.$show->intid);
     259            $sh->finish();
     260            if (file_exists($cover_thumbnail_filename))
     261                unlink($cover_thumbnail_filename);
     262        }
     263    }
     264
    187265// Load the class for this page
    188266    require_once tmpl_dir.'video.php';
    189267
  • mythweb/modules/video/tmpl/default/video.php

    diff -urNp a/mythweb/modules/video/tmpl/default/video.php b/mythweb/modules/video/tmpl/default/video.php
    a b $filters="&category=$Filter_Category&bro  
    111111    foreach ($All_Shows as $show) {
    112112    ?><tr class="recorded">
    113113    <td><?php
    114         if (show_video_covers && file_exists('data/video_covers/'.basename($show->coverfile)))
    115             echo '<a href="'.$show->url.'"><img id="'.html_entities($show->filename).'" src="data/video_covers/'.basename($show->coverfile).'" width="'.video_img_width.'" height="'.video_img_height.'">';
    116         else
     114        if (show_video_covers && file_exists('data/video_covers/'.basename($show->coverfile))) {
     115            echo '<a href="'.$show->url.'"><img id="'.html_entities($show->filename).'" src="';
     116            if (file_exists('data/video_cover_thumbs/'.basename($show->coverfile)))
     117                echo 'data/video_cover_thumbs/';
     118            else
     119                echo 'data/video_covers/';
     120            echo basename($show->coverfile).'" width="'.video_img_width.'" height="'.video_img_height.'">';
     121        } else
    117122            echo '&nbsp;';
    118123    ?></td>
    119124    <td><?php echo '<a href="'.$show->url.'">'.html_entities($show->title).'</a>' ?></td>