getimagesize() is terribly slow for when loading many remote images (see comment here http://au2.php.net/manual/en/function.getimagesize.php#88793 ). Thus, for things like a view which is an RSS feed of a bunch of nodes that are caption_filter enabled, then we end up with a long loading time on the RSS feed.

Fortunately, this can be rectified, with a slightly cleaned up version of the answer posted here on SO: http://stackoverflow.com/questions/4635936/super-fast-getimagesize-in-php

Since the code is only getting width, I modified the answer code to only return us width. I saw a 6x improvement locally when measured with cachegrind & xdebug after making this change.

Patch forthcoming.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

coderintherye’s picture

Status: Active » Needs review
FileSize
1.4 KB
coderintherye’s picture

Curl call could possibly be replaced by a drupal_http_request but I read in some discussions that a straight Curl call is going to outperform the drupal_http_request().

coderintherye’s picture

Better patch for when imagecreatefromstring() returns FALSE.

coderintherye’s picture

Status: Needs review » Needs work

Sadly, this seems to sometimes throw an error when imagecreatefromstring() can't properly determine the file type. The below fixes that...but suppressing the error isn't a good long-term solution. Maybe someone else has an idea?

diff --git a/sites/all/modules/caption_filter/caption_filter.module b/sites/all/modules/caption_filter/caption_filter.module
index 83db208..63ad358 100644
--- a/sites/all/modules/caption_filter/caption_filter.module
+++ b/sites/all/modules/caption_filter/caption_filter.module
@@ -320,10 +320,10 @@ function caption_filter_get_image_width($url) {
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
-
- $im = imagecreatefromstring($data);
+ $im = @imagecreatefromstring($data);
if($im) {
$width = imagesx($im);
}
+ $width = $width ?: 'auto';
return $width;
}