SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/EmbedVideoController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Controllers;
  15.  
  16. use Exception;
  17.  
  18. use Kohana;
  19.  
  20. use Sprout\Helpers\EmbedVideo;
  21. use Sprout\Helpers\File;
  22. use Sprout\Helpers\Image;
  23.  
  24.  
  25. /**
  26. * Dynamic cropping for video thumbs
  27. **/
  28. class EmbedVideoController extends Controller
  29. {
  30.  
  31. /**
  32.   * Downloads a thumbnail from the video provider
  33.   * Trims black bars from the top and the bottom of the image
  34.   * Resizes and crops to the specified size
  35.   **/
  36. public function thumb($size, $type, $videoid)
  37. {
  38. if ($type == EmbedVideo::TYPE_YOUTUBE) {
  39. $url = 'http://youtu.be/' . $videoid;
  40. } else if ($type == EmbedVideo::TYPE_VIMEO) {
  41. $url = 'http://vimeo.com/' . $videoid;
  42. } else {
  43. throw new Exception('Unsupported video type');
  44. }
  45.  
  46. $cache_filename = APPPATH . "cache/video-{$size}-{$type}-{$videoid}.jpg";
  47.  
  48. // File doesn't exist in cache or render is forced
  49. if (!file_exists($cache_filename) or @$_GET['force'] == 1) {
  50. list($type, $width, $height, $cropX, $cropY, $quality) = File::parseSizeString($size);
  51.  
  52. // Download thumbnail image file
  53. $thumb_url = EmbedVideo::getThumbFilename($url, 2);
  54. $data = @file_get_contents($thumb_url);
  55. if (! $data) {
  56. $thumb_url = EmbedVideo::getThumbFilename($url, 1);
  57. $data = @file_get_contents($thumb_url);
  58. if (! $data) {
  59. throw new Exception('Unable to download thumbnail image');
  60. }
  61. }
  62.  
  63. $temp_filename = APPPATH . 'temp/video-' . time() . mt_rand(0, 999) . '.jpg';
  64. file_put_contents($temp_filename, $data);
  65.  
  66. $img = new Image($temp_filename);
  67.  
  68. if ($type == 'm') {
  69. // Max size
  70. $file_size = getimagesize($temp_filename);
  71.  
  72. if ($width == 0) $width = PHP_INT_MAX;
  73. if ($height == 0) $height = PHP_INT_MAX;
  74.  
  75. if ($file_size[0] > $width or $file_size[1] > $height) {
  76. $img->resize($width, $height);
  77. }
  78.  
  79. } else if ($type == 'r') {
  80. // Resize
  81. $img->resize($width, $height);
  82.  
  83. } else if ($type == 'c') {
  84. // Crop
  85. if ($width / $img->width > $height / $img->height) {
  86. $master = Image::WIDTH;
  87. } else {
  88. $master = Image::HEIGHT;
  89. }
  90.  
  91. $img->resize($width, $height, $master);
  92. $img->crop($width, $height, $cropY, $cropX);
  93.  
  94. } else {
  95. // What?
  96. unlink($temp_filename);
  97. throw new Exception('Incorrect resize type');
  98. }
  99.  
  100. if ($quality) {
  101. $img->quality($quality);
  102. }
  103.  
  104. $img->save($cache_filename);
  105. unlink($temp_filename);
  106. }
  107.  
  108. // Serve the cached file
  109. header('Content-type: image/jpeg');
  110. header('Content-length: ' . filesize($cache_filename));
  111. Kohana::closeBuffers();
  112. readfile($cache_filename);
  113. exit(0);
  114. }
  115.  
  116.  
  117. /**
  118.   * Finds the black edge between $y1 and $y2
  119.   **/
  120. private function findEdgeY($img, $y1, $y2)
  121. {
  122. $black = imagecolorat($img, 1, 1);
  123.  
  124. for ($y = $y1; $y < $y2; $y++) {
  125. $col = imagecolorat($img, 1, $y);
  126. if ($col != $black) return $y;
  127. }
  128.  
  129. return 1;
  130. }
  131.  
  132. }
  133.