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.
Comment | File | Size | Author |
---|---|---|---|
#3 | caption_filter-speedupgetimagewidth-2299925-3.patch | 1.42 KB | coderintherye |
#1 | caption_filter-speedupgetimagewidth-2299925-1.patch | 1.4 KB | coderintherye |
Comments
Comment #1
coderintherye CreditAttribution: coderintherye commentedComment #2
coderintherye CreditAttribution: coderintherye commentedCurl 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().
Comment #3
coderintherye CreditAttribution: coderintherye commentedBetter patch for when imagecreatefromstring() returns FALSE.
Comment #4
coderintherye CreditAttribution: coderintherye commentedSadly, 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;
}