Skip to content
This repository
Browse code

BUGFIX insertion of images (trac #7438)

Allow insertion of images via oEmbed dialog using a direct url
  • Loading branch information...
commit e5829355821bd930e00bed76e2acd15ce96d8816 1 parent e2d7352
mightycoco authored June 25, 2012
3  forms/HtmlEditorField.php
@@ -515,7 +515,8 @@ public function viewfile($request) {
515 515
 		}
516 516
 
517 517
 		// Instanciate file wrapper and get fields based on its type
518  
-		if($file && $file->appCategory() == 'image') {
  518
+		// Check if appCategory is an image and exists on the local system, otherwise use oEmbed to refference a remote image
  519
+		if($file && $file->appCategory() == 'image' && Director::is_site_url($url)) {
519 520
 			$fileWrapper = new HtmlEditorField_Image($url, $file);
520 521
 		} elseif(!Director::is_site_url($url)) {
521 522
 			$fileWrapper = new HtmlEditorField_Embed($url, $file);
20  oembed/Oembed.php
@@ -131,6 +131,13 @@ public static function get_oembed_from_url($url, $type = false, array $options =
131 131
 			// Build the url manually - we gave all needed information.
132 132
 			$oembedUrl = Controller::join_links($endpoint, '?format=json&url=' . rawurlencode($url));
133 133
 		}
  134
+		
  135
+		// If autodescovery failed the resource might be a direct link to a file
  136
+		if(!$oembedUrl) {
  137
+			if(File::get_app_category(File::get_file_extension($url))) {
  138
+				return new Oembed_Result($url, $url, $type, $options);
  139
+			}
  140
+		}
134 141
 
135 142
 		if($oembedUrl) {
136 143
 			// Inject the options into the Oembed URL.
@@ -233,7 +240,20 @@ protected function loadData() {
233 240
 		$body = $body->getBody();
234 241
 		$data = json_decode($body, true);
235 242
 		if(!$data) {
  243
+			// if the response is no valid JSON we might have received a binary stream to an image
236 244
 			$data = array();
  245
+			$image = @imagecreatefromstring($body);
  246
+			if($image !== FALSE) {
  247
+				preg_match("/^(http:\/\/)?([^\/]+)/i", $this->url, $matches);
  248
+				$protocoll = $matches[1];
  249
+				$host = $matches[2];
  250
+				$data['type'] = "photo";
  251
+				$data['title'] = basename($this->url) . " ($host)";
  252
+				$data['url'] = $this->url;
  253
+				$data['provider_url'] = $protocoll.$host;
  254
+				$data['width'] = imagesx($image);
  255
+				$data['height'] = imagesy($image);
  256
+			}
237 257
 		}
238 258
 
239 259
 		// Convert all keys to lowercase

0 notes on commit e582935

Please sign in to comment.
Something went wrong with that request. Please try again.