diff options
author | Max Kostikov <max@kostikov.co> | 2021-02-01 09:53:09 +0100 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2021-02-01 09:53:09 +0100 |
commit | de91d2c804085c2a169863e67b38d2ac7da8f9c7 (patch) | |
tree | 6b10cb29235f0c6bd9440b46fcf47f2a3282b383 /include | |
parent | a235917d4848fe1a890ae8f0c7cd1db27d0eecdd (diff) | |
download | volse-hubzilla-de91d2c804085c2a169863e67b38d2ac7da8f9c7.tar.gz volse-hubzilla-de91d2c804085c2a169863e67b38d2ac7da8f9c7.tar.bz2 volse-hubzilla-de91d2c804085c2a169863e67b38d2ac7da8f9c7.zip |
Better profile photo fetch processing
Diffstat (limited to 'include')
-rw-r--r-- | include/photo/photo_driver.php | 59 |
1 files changed, 34 insertions, 25 deletions
diff --git a/include/photo/photo_driver.php b/include/photo/photo_driver.php index 057711f1e..97871e139 100644 --- a/include/photo/photo_driver.php +++ b/include/photo/photo_driver.php @@ -64,15 +64,23 @@ function photo_factory($data, $type = null) { * * @param string $filename * Image filename - * @param string $headers (optional) - * Headers to check for Content-Type (from curl request) + * @param string $data (optional) + * Data array fetched from cURL with z_fetch_url * @return null|string Guessed mimetype */ -function guess_image_type($filename, $headers = '') { -// logger('Photo: guess_image_type: '.$filename . ($headers?' from curl headers':''), LOGGER_DEBUG); +function guess_image_type($filename, $data = '') { + + if($data) + $headers = (is_array($data) ? $data['header'] : $data); + + // logger('Photo: guess_image_type: '.$filename . ($headers?' from curl headers':''), LOGGER_DEBUG); + $type = null; $m = null; + $ph = photo_factory(''); + $types = $ph->supportedTypes(); + if($headers) { $hdrs = []; $h = explode("\n", $headers); @@ -81,19 +89,14 @@ function guess_image_type($filename, $headers = '') { $hdrs[strtolower($k)] = $v; } logger('Curl headers: ' .var_export($hdrs, true), LOGGER_DEBUG); - if(array_key_exists('content-type', $hdrs)) { - $ph = photo_factory(''); - $types = $ph->supportedTypes(); - - if(array_key_exists($hdrs['content-type'], $types)) - $type = $hdrs['content-type']; - } + if(array_key_exists('content-type', $hdrs) && array_key_exists($hdrs['content-type'], $types)) + $type = $hdrs['content-type']; } if(is_null($type)){ $ignore_imagick = get_config('system', 'ignore_imagick'); // Guessing from extension? Isn't that... dangerous? - if(class_exists('Imagick') && file_exists($filename) && is_readable($filename) && !$ignore_imagick) { + if(class_exists('Imagick') && ! $ignore_imagick) { $v = Imagick::getVersion(); preg_match('/ImageMagick ([0-9]+\.[0-9]+\.[0-9]+)/', $v['versionString'], $m); if(version_compare($m[1], '6.6.7') >= 0) { @@ -102,8 +105,18 @@ function guess_image_type($filename, $headers = '') { * but at least it comes from the data inside the image, * we won't be tricked by a manipulated extension */ - $image = new Imagick($filename); - $type = $image->getImageMimeType(); + $body = false; + if (strpos($filename, 'http') === false && file_exists($filename) && is_readable($filename)) + $body == file_get_contents($filename); + elseif (is_array($data) && array_key_exists('body', $data)) + $body = $data['body']; + if ($body) { + $image = new Imagick($filename); + $image->readImageBlob($body); + $r = $image->identifyImage(); + if ($r && is_array($r) && array_key_exists($r['mimetype'], $types)) + $type = $r['mimetype']; + } } else { // earlier imagick versions have issues with scaling png's @@ -115,8 +128,6 @@ function guess_image_type($filename, $headers = '') { if(is_null($type)) { $ext = pathinfo($filename, PATHINFO_EXTENSION); - $ph = photo_factory(''); - $types = $ph->supportedTypes(); foreach($types as $m => $e) { if($ext === $e) { $type = $m; @@ -124,12 +135,11 @@ function guess_image_type($filename, $headers = '') { } } - if(is_null($type) && (strpos($filename, 'http') === false)) { + if(is_null($type) && strpos($filename, 'http') === false) { $size = getimagesize($filename); - $ph = photo_factory(''); - $types = $ph->supportedTypes(); $type = ((array_key_exists($size['mime'], $types)) ? $size['mime'] : 'image/jpeg'); } + if(is_null($type)) { if(strpos(strtolower($filename),'jpg') !== false) $type = 'image/jpeg'; @@ -139,8 +149,8 @@ function guess_image_type($filename, $headers = '') { $type = 'image/gif'; elseif(strpos(strtolower($filename),'png') !== false) $type = 'image/png'; - elseif(strpos(strtolower($filename),'webp') !== false) - $type = 'image/webp'; + elseif(strpos(strtolower($filename),'webp') !== false) + $type = 'image/webp'; } } @@ -224,7 +234,7 @@ function import_xchan_photo($photo, $xchan, $thing = false, $force = false) { $photo_failure = false; $img_str = ''; - if($photo) { + if($photo && strpos($photo, z_root() . '/' . get_default_profile_photo()) === false) { if($force || empty($modified)) $result = z_fetch_url($photo, true); @@ -264,7 +274,7 @@ function import_xchan_photo($photo, $xchan, $thing = false, $force = false) { if($result['success']) { $img_str = $result['body']; - $type = guess_image_type($photo, $result['header']); + $type = guess_image_type($photo, $result); if(is_null($type)) $photo_failure = true; } @@ -377,8 +387,7 @@ function import_channel_photo_from_url($photo, $aid, $uid) { if($result['success']) { $img_str = $result['body']; - $type = guess_image_type($photo, $result['header']); - + $type = guess_image_type($photo, $result); import_channel_photo($img_str, $type, $aid, $uid); } } |