Add default thumb for videos without a thumb, a=chris

Chris Pollett [2022-09-05 07:Sep:th]
Add default thumb for videos without a thumb, a=chris
Filename
src/controllers/SearchController.php
src/views/elements/SearchElement.php
src/views/helpers/VideosHelper.php
diff --git a/src/controllers/SearchController.php b/src/controllers/SearchController.php
index bb3ac901c..6609e8e0a 100755
--- a/src/controllers/SearchController.php
+++ b/src/controllers/SearchController.php
@@ -1037,7 +1037,23 @@ class SearchController extends Controller implements CrawlConstants
         }
         if (($view == "search" || $view == 'api')
             && $data["RAW"] == 0 && isset($data['PAGES'])) {
-            $data['PAGES'] = $this->makeMediaGroups($data['PAGES']);
+            list($data['PAGES'], $found_images_or_video) =
+                $this->makeMediaGroups($data['PAGES']);
+            if ($found_images_or_video) {
+                $thumb = imagecreatetruecolor(1, 1);
+                if (!empty($thumb)) {
+                    imagesavealpha($thumb, true);
+                    $trans_colour = imagecolorallocatealpha($thumb,
+                        211, 211, 211, 0);
+                    imagefill($thumb, 0, 0, $trans_colour);
+                    ob_start();
+                    imagepng($thumb);
+                    $data['DEFAULT_THUMB'] = 'data:image/png;base64,' .
+                        base64_encode(ob_get_contents());
+                    imagedestroy($thumb);
+                    ob_end_clean();
+                }
+            }
         }
         $data['IS_LANDING'] = !isset($data['PAGES']) &&
             !isset($data['TRENDING']);
@@ -1499,7 +1515,8 @@ EOD;
      *
      * @param $pages an array of search result pages to group those pages
      *     with thumbs within
-     * @return array $pages after the grouping has been done
+     * @return array [$pages after the grouping has been done, whether images
+     *      or videos found]
      */
     public function makeMediaGroups($pages)
     {
@@ -1533,7 +1550,7 @@ EOD;
                 $out_pages[] = $page;
             }
         }
-        return $out_pages;
+        return [$out_pages, ($first_video > 0 || $first_image)];
     }
     /**
      * Given a page summary extract the words from it and try to find documents
diff --git a/src/views/elements/SearchElement.php b/src/views/elements/SearchElement.php
index ffa6d4f91..8f5c1290d 100644
--- a/src/views/elements/SearchElement.php
+++ b/src/views/elements/SearchElement.php
@@ -222,7 +222,7 @@ class SearchElement extends Element implements CrawlConstants
                 $this->view->helper("videos")->render($page['VIDEOS'],
                     $base_query . "&q=".urlencode($data['QUERY']),
                     $subsearch, $data['VIDEO_SUBSEARCH_ENABLED'],
-                    $data["OPEN_IN_TABS"]);
+                    $data["OPEN_IN_TABS"], $data["DEFAULT_THUMB"]);
                 continue;
             } else if (isset($page['FEED'])) {
                 $this->view->helper("feeds")->render($page['FEED'],
diff --git a/src/views/helpers/VideosHelper.php b/src/views/helpers/VideosHelper.php
index eea55d77a..c54ad606a 100644
--- a/src/views/helpers/VideosHelper.php
+++ b/src/views/helpers/VideosHelper.php
@@ -53,10 +53,12 @@ class VideosHelper extends Helper implements CrawlConstants
      * @param bool $video_subsearch whether the video subsearch is
      *  enabled on this Yioop instance
      * @param boolean $open_in_tabs whether new links should be opened in
-     *    tabs
+     *  tabs
+     * @param string $default_thumb default data url to use if a video does
+     *  not have a thumb
      */
     public function render($video_pages, $query, $subsearch,
-        $video_subsearch = true, $open_in_tabs = false)
+        $video_subsearch = true, $open_in_tabs = false, $default_thumb = "")
     {
         if ($subsearch != 'videos' && !stristr($query, "media%3Avideo")) {
             if (!empty($video_subsearch)) {
@@ -138,7 +140,7 @@ class VideosHelper extends Helper implements CrawlConstants
             ?><a href="<?=$link
                 ?>" rel="noopener nofollow noreferer" ><img class="video-thumb"
                 <?= $open_in_tabs ?> src="<?=
-                    $page[self::THUMB]?? "" ?>"
+                    $page[self::THUMB] ?? $default_thumb ?>"
                 alt="<?= tl('video_helper_videothumb'); ?>" />
             </a>
             <div class="video-quality"><?= $quality ?></div>
ViewGit