From 76e9a5e7bba79aef8fae35aaa7a1b7b79e970f90 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Fri, 16 Sep 2016 13:10:20 +0200 Subject: [PATCH] Fetch and save aspect ratio for responsive embeds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit – Added new column to db: aspect_ratio – Added method to handle the db schema update – Fetches width and height from oEmbed to determine video’s aspect ratio for proper responsive embedding – Added new module config field: defaultAspectRatio (used when aspect ratio can’t be determined from oEmbed request) – Bumbed version to 112 --- TextformatterVideoEmbed.module | 96 ++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 9 deletions(-) diff --git a/TextformatterVideoEmbed.module b/TextformatterVideoEmbed.module index 6e30bbe..e9f46b0 100644 --- a/TextformatterVideoEmbed.module +++ b/TextformatterVideoEmbed.module @@ -12,6 +12,7 @@ * @property int $maxWidth * @property int $maxHeight * @property int $responsive + * @property float $defaultAspectRatio * */ @@ -20,14 +21,20 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul public static function getModuleInfo() { return array( 'title' => __('Video embed for YouTube/Vimeo', __FILE__), - 'version' => 111, + 'version' => 112, 'summary' => __('Enter a full YouTube or Vimeo URL by itself in any paragraph (example: http://www.youtube.com/watch?v=Wl4XiYadV_k) and this will automatically convert it to an embedded video. This formatter is intended to be run on trusted input. Recommended for use with TinyMCE textarea fields.', __FILE__), 'author' => 'Ryan Cramer', 'href' => 'http://modules.processwire.com/modules/textformatter-video-embed/' ); } + /** + * Name and latest schema version for database table used by this module + * + */ + const dbTableName = 'textformatter_video_embed'; + const dbSchemaVersion = 2; /** * Default configuration values @@ -39,6 +46,9 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul 'maxWidth' => 640, 'maxHeight' => 480, 'responsive' => 0, + 'defaultAspectRatio' => 16/9, + + 'dbSchemaVersion' => 1, ); /** @@ -63,6 +73,21 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul } $this->http = $this->config->https ? 'https' : 'http'; } + + /** + * Initialization function + * + * This function updates the database schema, if necessary. + * + */ + public function init() { + + // update database schema (if not the latest one yet) + if ($this->dbSchemaVersion < self::dbSchemaVersion) { + $this->updateDatabaseSchema(); + } + + } /** * Given a service oembed URL and video ID, return the corresponding embed code. @@ -79,15 +104,16 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul $database = $this->wire('database'); $table = self::dbTableName; - $query = $database->prepare("SELECT embed_code FROM $table WHERE video_id=:video_id"); + $query = $database->prepare("SELECT embed_code, aspect_ratio FROM $table WHERE video_id=:video_id"); $query->bindValue(":video_id", $videoID); $query->execute(); $embedCode = ''; + $aspectRatio = 0; if($query->rowCount()) { - list($embedCode) = $query->fetch(\PDO::FETCH_NUM); + list($embedCode, $aspectRatio) = $query->fetch(\PDO::FETCH_NUM); $query->closeCursor(); } else { @@ -99,18 +125,27 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul if(is_array($data) && isset($data['html'])) { $embedCode = $data['html']; + + if(isset($data['width']) && isset($data['height'])) { + $aspectRatio = $data['width'] / $data['height']; + } $sql = "INSERT INTO $table SET " . "video_id=:videoID, " . "embed_code=:embedCode, " . + "aspect_ratio=:aspectRatio, " . "created=NOW() "; $query = $database->prepare($sql); $query->bindValue(":videoID", $videoID); $query->bindValue(":embedCode", $embedCode); + $query->bindValue(":aspectRatio", $aspectRatio); $query->execute(); } } + + // responsive embed codes + if($this->responsive) $embedCode = $this->makeResponsive($embedCode, $aspectRatio); // account for possibility that stored embed code contains http version while requested on https if($this->http == 'https') $embedCode = str_replace('http://', 'https://', $embedCode); @@ -122,12 +157,22 @@ class TextformatterVideoEmbed extends Textformatter implements ConfigurableModul * Make an iframe-based embed code responsive * * @param string $out + * @param float $aspectRatio * @return string * */ - protected function makeResponsive($out) { + protected function makeResponsive($out, $aspectRatio = 0) { + $aspectRatio = floatval($aspectRatio); + + if(!$aspectRatio) $aspectRatio = $this->defaultAspectRatio; + + // Calculate padding percentage from aspect ratio + $padding = (1 / $aspectRatio) * 100; + $padding = number_format($padding, 2, '.', ''); + $out = str_ireplace('