aboutsummaryrefslogtreecommitdiffstats
path: root/include/photo/photo_driver.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/photo/photo_driver.php')
-rw-r--r--include/photo/photo_driver.php62
1 files changed, 36 insertions, 26 deletions
diff --git a/include/photo/photo_driver.php b/include/photo/photo_driver.php
index 057711f1e..87b1d96fe 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,12 @@ function guess_image_type($filename, $headers = '') {
}
}
- if(is_null($type) && (strpos($filename, 'http') === false)) {
+ if(is_null($type) && strpos($filename, 'http') === 0) {
$size = getimagesize($filename);
- $ph = photo_factory('');
- $types = $ph->supportedTypes();
- $type = ((array_key_exists($size['mime'], $types)) ? $size['mime'] : 'image/jpeg');
+ if (array_key_exists($size['mime'], $types))
+ $type = $size['mime'];
}
+
if(is_null($type)) {
if(strpos(strtolower($filename),'jpg') !== false)
$type = 'image/jpeg';
@@ -139,8 +150,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 +235,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 +275,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 +388,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);
}
}