Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

fix(memory): ensure thumbnails are images and resizable

fixes #42
Prevents an infinite loop when thumbnail urls are not actually images.  Also checks that
the system has enough memory to handle an image resize operation.
  • Loading branch information...
beck24 committed Feb 18, 2017
1 parent 4eacbe8 commit 9e5ce74bbb6e2d18368bbb2f8463481384c96ec5
Showing with 36 additions and 13 deletions.
  1. +36 −13 classes/hypeJunction/Scraper/ScraperService.php
@@ -267,6 +267,13 @@ public function saveImageFromUrl($url) {
$tmp->write($raw_bytes);
$tmp->close();
unset($raw_bytes);
//@Todo - looks like we need some way to check this in core
// instead of elgg_save_resized_image() OOMing
if (!$this->hasMemoryToResize($tmp->getFilenameOnFilestore())) {
$tmp->delete();
return false;
}
$threshold = elgg_get_plugin_setting('cache_thumb_size_lower_threshold', 'hypeScraper', 100);
$imagesize = getimagesize($tmp->getFilenameOnFilestore());
@@ -311,21 +318,18 @@ public function parseThumbs(array $data = []) {
$icons = (array) elgg_extract('icons', $data, []);
// Try 3 images and choose the one with highest dimensions
$thumbnails = $thumbnails + $icons;
$thumbs_parse = 0;
$thumbnails = array_filter(array_unique($thumbnails + $icons));
$thumbs_parsed = 0;
foreach ($thumbnails as $thumbnail) {
if ($thumbnail == $url) {
continue;
}
$thumbnail = elgg_normalize_url($thumbnail);
if (filter_var($thumbnail, FILTER_VALIDATE_URL)) {
$asset = $this->parse($thumbnail, $flush, false);
if ($asset) {
$thumbs_parsed++;
$assets[] = $asset;
}
$asset = $this->parse($thumbnail, false, false);
if ($asset) {
$thumbs_parsed++;
$assets[] = $asset;
}
if ($thubms_parsed == 3) {
if ($thumbs_parsed == 3) {
break;
}
}
@@ -372,5 +376,24 @@ public static function getHttpClientConfig() {
return elgg_trigger_plugin_hook('http:config', 'framework:scraper', null, $config);
}
/**
* Do we estimate that we have enough memory available to resize an image?
*
* @param string $source - the source path of the file
* @return bool
*/
public function hasMemoryToResize($source) {
$imginfo = getimagesize($source);
$requiredMemory1 = ceil($imginfo[0] * $imginfo[1] * 5.35);
$requiredMemory2 = ceil($imginfo[0] * $imginfo[1] * ($imginfo['bits'] / 8) * $imginfo['channels'] * 2.5);
$requiredMemory = (int) max($requiredMemory1, $requiredMemory2);
$mem_avail = elgg_get_ini_setting_in_bytes('memory_limit');
$mem_used = memory_get_usage();
$mem_avail = $mem_avail - $mem_used - 20971520; // 20 MB buffer, yeah arbitrary but necessary
return $mem_avail > $requiredMemory;
}
}

0 comments on commit 9e5ce74

Please sign in to comment.
You can’t perform that action at this time.