Browse Source

Merge pull request #7798 from MrPetovan/task/rework-photo-functions

Rework photo functions
pull/7804/head
Michael Vogel 2 years ago
committed by GitHub
parent
commit
6ba7b5961c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      include/api.php
  2. 4
      mod/fbrowser.php
  3. 21
      mod/photos.php
  4. 26
      mod/wall_upload.php
  5. 4
      src/App/Arguments.php
  6. 7
      src/Content/Text/BBCode.php
  7. 4
      src/Core/Installer.php
  8. 3
      src/Model/Contact.php
  9. 28
      src/Model/Photo.php
  10. 13
      src/Model/User.php
  11. 205
      src/Object/Image.php
  12. 26
      src/Protocol/ActivityPub/Transmitter.php
  13. 3
      src/Protocol/DFRN.php
  14. 5
      src/Protocol/OStatus.php
  15. 236
      src/Util/Images.php
  16. 5
      src/Util/ParseUrl.php
  17. 12
      tests/src/Core/InstallerTest.php

35
include/api.php

@ -45,6 +45,7 @@ use Friendica\Object\Image;
use Friendica\Protocol\Activity;
use Friendica\Protocol\Diaspora;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings;
@ -1167,7 +1168,7 @@ function api_statuses_update($type)
api_user()
);
if (DBA::isResult($r)) {
$phototypes = Image::supportedTypes();
$phototypes = Images::supportedTypes();
$ext = $phototypes[$r[0]['type']];
$description = $r[0]['desc'] ?? '';
$_REQUEST['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $r[0]['nickname'] . '/image/' . $r[0]['resource-id'] . ']';
@ -2564,7 +2565,7 @@ function api_get_attachments(&$body)
$attachments = [];
foreach ($images[1] as $image) {
$imagedata = Image::getInfoFromURL($image);
$imagedata = Images::getInfoFromURLCached($image);
if ($imagedata) {
$attachments[] = ["url" => $image, "mimetype" => $imagedata["mime"], "size" => $imagedata["size"]];
@ -2711,7 +2712,7 @@ function api_get_entitities(&$text, $bbcode)
$start = iconv_strpos($text, $url, $offset, "UTF-8");
if (!($start === false)) {
$image = Image::getInfoFromURL($url);
$image = Images::getInfoFromURLCached($url);
if ($image) {
// If image cache is activated, then use the following sizes:
// thumb (150), small (340), medium (600) and large (1024)
@ -2719,19 +2720,19 @@ function api_get_entitities(&$text, $bbcode)
$media_url = ProxyUtils::proxifyUrl($url);
$sizes = [];
$scale = Image::getScalingDimensions($image[0], $image[1], 150);
$scale = Images::getScalingDimensions($image[0], $image[1], 150);
$sizes["thumb"] = ["w" => $scale["width"], "h" => $scale["height"], "resize" => "fit"];
if (($image[0] > 150) || ($image[1] > 150)) {
$scale = Image::getScalingDimensions($image[0], $image[1], 340);
$scale = Images::getScalingDimensions($image[0], $image[1], 340);
$sizes["small"] = ["w" => $scale["width"], "h" => $scale["height"], "resize" => "fit"];
}
$scale = Image::getScalingDimensions($image[0], $image[1], 600);
$scale = Images::getScalingDimensions($image[0], $image[1], 600);
$sizes["medium"] = ["w" => $scale["width"], "h" => $scale["height"], "resize" => "fit"];
if (($image[0] > 600) || ($image[1] > 600)) {
$scale = Image::getScalingDimensions($image[0], $image[1], 1024);
$scale = Images::getScalingDimensions($image[0], $image[1], 1024);
$sizes["large"] = ["w" => $scale["width"], "h" => $scale["height"], "resize" => "fit"];
}
} else {
@ -4740,7 +4741,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
}
if ($filetype == "") {
$filetype=Image::guessType($filename);
$filetype = Images::guessType($filename);
}
$imagedata = @getimagesize($src);
if ($imagedata) {
@ -4787,19 +4788,19 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
$height = $Image->getHeight();
// create a new resource-id if not already provided
$hash = ($photo_id == null) ? Photo::newResource() : $photo_id;
$resource_id = ($photo_id == null) ? Photo::newResource() : $photo_id;
if ($mediatype == "photo") {
// upload normal image (scales 0, 1, 2)
Logger::log("photo upload: starting new photo upload", Logger::DEBUG);
$r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 0, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
$r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 0, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
if (!$r) {
Logger::log("photo upload: image upload with scale 0 (original size) failed");
}
if ($width > 640 || $height > 640) {
$Image->scaleDown(640);
$r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 1, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
$r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 1, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
if (!$r) {
Logger::log("photo upload: image upload with scale 1 (640x640) failed");
}
@ -4807,7 +4808,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
if ($width > 320 || $height > 320) {
$Image->scaleDown(320);
$r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 2, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
$r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 2, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
if (!$r) {
Logger::log("photo upload: image upload with scale 2 (320x320) failed");
}
@ -4819,7 +4820,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
if ($width > 300 || $height > 300) {
$Image->scaleDown(300);
$r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 4, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
$r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 4, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
if (!$r) {
Logger::log("photo upload: profile image upload with scale 4 (300x300) failed");
}
@ -4827,7 +4828,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
if ($width > 80 || $height > 80) {
$Image->scaleDown(80);
$r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 5, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
$r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 5, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
if (!$r) {
Logger::log("photo upload: profile image upload with scale 5 (80x80) failed");
}
@ -4835,7 +4836,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
if ($width > 48 || $height > 48) {
$Image->scaleDown(48);
$r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 6, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
$r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 6, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
if (!$r) {
Logger::log("photo upload: profile image upload with scale 6 (48x48) failed");
}
@ -4847,10 +4848,10 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
if (isset($r) && $r) {
// create entry in 'item'-table on new uploads to enable users to comment/like/dislike the photo
if ($photo_id == null && $mediatype == "photo") {
post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility);
post_photo_item($resource_id, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility);
}
// on success return image data in json/xml format (like /api/friendica/photo does when no scale is given)
return prepare_photo_data($type, false, $hash);
return prepare_photo_data($type, false, $resource_id);
} else {
throw new InternalServerErrorException("image upload failed");
}

4
mod/fbrowser.php

@ -10,7 +10,7 @@ use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Object\Image;
use Friendica\Util\Images;
use Friendica\Util\Strings;
/**
@ -79,7 +79,7 @@ function fbrowser_content(App $a)
function _map_files1($rr)
{
$a = \get_app();
$types = Image::supportedTypes();
$types = Images::supportedTypes();
$ext = $types[$rr['type']];
$filename_e = $rr['filename'];

21
mod/photos.php

@ -29,6 +29,7 @@ use Friendica\Protocol\Activity;
use Friendica\Util\ACLFormatter;
use Friendica\Util\Crypto;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
use Friendica\Util\Map;
use Friendica\Util\Security;
use Friendica\Util\Strings;
@ -142,7 +143,7 @@ function photos_post(App $a)
Logger::log('mod_photos: REQUEST ' . print_r($_REQUEST, true), Logger::DATA);
Logger::log('mod_photos: FILES ' . print_r($_FILES, true), Logger::DATA);
$phototypes = Image::supportedTypes();
$phototypes = Images::supportedTypes();
$can_post = false;
$visitor = 0;
@ -694,7 +695,7 @@ function photos_post(App $a)
}
if ($type == "") {
$type = Image::guessType($filename);
$type = Images::guessType($filename);
}
Logger::log('photos: upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes', Logger::DEBUG);
@ -748,9 +749,9 @@ function photos_post(App $a)
$smallest = 0;
$photo_hash = Photo::newResource();
$resource_id = Photo::newResource();
$r = Photo::store($image, $page_owner_uid, $visitor, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
$r = Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
if (!$r) {
Logger::log('mod/photos.php: photos_post(): image store failed', Logger::DEBUG);
@ -760,13 +761,13 @@ function photos_post(App $a)
if ($width > 640 || $height > 640) {
$image->scaleDown(640);
Photo::store($image, $page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
$smallest = 1;
}
if ($width > 320 || $height > 320) {
$image->scaleDown(320);
Photo::store($image, $page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
$smallest = 2;
}
@ -790,7 +791,7 @@ function photos_post(App $a)
$arr['parent-uri'] = $uri;
$arr['type'] = 'photo';
$arr['wall'] = 1;
$arr['resource-id'] = $photo_hash;
$arr['resource-id'] = $resource_id;
$arr['contact-id'] = $owner_record['id'];
$arr['owner-name'] = $owner_record['name'];
$arr['owner-link'] = $owner_record['url'];
@ -806,8 +807,8 @@ function photos_post(App $a)
$arr['visible'] = $visible;
$arr['origin'] = 1;
$arr['body'] = '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']'
. '[img]' . System::baseUrl() . "/photo/{$photo_hash}-{$smallest}.".$image->getExt() . '[/img]'
$arr['body'] = '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $resource_id . ']'
. '[img]' . System::baseUrl() . "/photo/{$resource_id}-{$smallest}.".$image->getExt() . '[/img]'
. '[/url]';
$item_id = Item::insert($arr);
@ -846,7 +847,7 @@ function photos_content(App $a)
return;
}
$phototypes = Image::supportedTypes();
$phototypes = Images::supportedTypes();
$_SESSION['photo_return'] = $a->cmd;

26
mod/wall_upload.php

@ -9,16 +9,16 @@
*/
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Core\Session;
use Friendica\Core\Config;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Photo;
use Friendica\Model\User;
use Friendica\Object\Image;
use Friendica\Util\Images;
use Friendica\Util\Strings;
function wall_upload_post(App $a, $desktopmode = true)
@ -166,7 +166,7 @@ function wall_upload_post(App $a, $desktopmode = true)
}
if ($filetype == "") {
$filetype = Image::guessType($filename);
$filetype = Images::guessType($filename);
}
// If there is a temp name, then do a manual check
@ -222,7 +222,7 @@ function wall_upload_post(App $a, $desktopmode = true)
$width = $Image->getWidth();
$height = $Image->getHeight();
$hash = Photo::newResource();
$resource_id = Photo::newResource();
$smallest = 0;
@ -233,7 +233,7 @@ function wall_upload_post(App $a, $desktopmode = true)
$defperm = '<' . $default_cid . '>';
$r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
$r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, 0, $defperm);
if (!$r) {
$msg = L10n::t('Image upload failed.');
@ -247,7 +247,7 @@ function wall_upload_post(App $a, $desktopmode = true)
if ($width > 640 || $height > 640) {
$Image->scaleDown(640);
$r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
$r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $defperm);
if ($r) {
$smallest = 1;
}
@ -255,7 +255,7 @@ function wall_upload_post(App $a, $desktopmode = true)
if ($width > 320 || $height > 320) {
$Image->scaleDown(320);
$r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
$r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $defperm);
if ($r && ($smallest == 0)) {
$smallest = 2;
}
@ -265,7 +265,7 @@ function wall_upload_post(App $a, $desktopmode = true)
$r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
WHERE `resource-id` = '%s'
ORDER BY `width` DESC LIMIT 1",
$hash
$resource_id
);
if (!$r) {
if ($r_json) {
@ -281,9 +281,9 @@ function wall_upload_post(App $a, $desktopmode = true)
$picture["width"] = $r[0]["width"];
$picture["height"] = $r[0]["height"];
$picture["type"] = $r[0]["type"];
$picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
$picture["picture"] = System::baseUrl() . "/photo/{$hash}-0." . $Image->getExt();
$picture["preview"] = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $Image->getExt();
$picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id;
$picture["picture"] = System::baseUrl() . "/photo/{$resource_id}-0." . $Image->getExt();
$picture["preview"] = System::baseUrl() . "/photo/{$resource_id}-{$smallest}." . $Image->getExt();
if ($r_json) {
echo json_encode(['picture' => $picture]);
@ -300,7 +300,7 @@ function wall_upload_post(App $a, $desktopmode = true)
exit();
}
echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";
echo "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $resource_id . '][img]' . System::baseUrl() . "/photo/{$resource_id}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";
exit();
// NOTREACHED
}

4
src/App/Arguments.php

@ -105,9 +105,9 @@ class Arguments
$queryString = '';
if (!empty($server['QUERY_STRING']) && strpos($server['QUERY_STRING'], 'pagename=') === 0) {
$queryString = substr($server['QUERY_STRING'], 9);
$queryString = urldecode(substr($server['QUERY_STRING'], 9));
} elseif (!empty($server['QUERY_STRING']) && strpos($server['QUERY_STRING'], 'q=') === 0) {
$queryString = substr($server['QUERY_STRING'], 2);
$queryString = urldecode(substr($server['QUERY_STRING'], 2));
}
// eventually strip ZRL

7
src/Content/Text/BBCode.php

@ -25,6 +25,7 @@ use Friendica\Model\Photo;
use Friendica\Network\Probe;
use Friendica\Object\Image;
use Friendica\Protocol\Activity;
use Friendica\Util\Images;
use Friendica\Util\Map;
use Friendica\Util\Network;
use Friendica\Util\ParseUrl;
@ -76,7 +77,7 @@ class BBCode extends BaseObject
if (preg_match("/\[img\](.*?)\[\/img\]/ism", $attacheddata, $matches)) {
$picturedata = Image::getInfoFromURL($matches[1]);
$picturedata = Images::getInfoFromURLCached($matches[1]);
if ($picturedata) {
if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1])) {
@ -305,7 +306,7 @@ class BBCode extends BaseObject
$post['preview'] = $pictures[0][2];
$post['text'] = trim(str_replace($pictures[0][0], '', $body));
} else {
$imgdata = Image::getInfoFromURL($pictures[0][1]);
$imgdata = Images::getInfoFromURLCached($pictures[0][1]);
if ($imgdata && substr($imgdata['mime'], 0, 6) == 'image/') {
$post['type'] = 'photo';
$post['image'] = $pictures[0][1];
@ -446,7 +447,7 @@ class BBCode extends BaseObject
}
// guess mimetype from headers or filename
$type = Image::guessType($mtch[1], true);
$type = Images::guessType($mtch[1], true);
if ($i) {
$Image = new Image($i, $type);

4
src/Core/Installer.php

@ -9,7 +9,7 @@ use Exception;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Database\Database;
use Friendica\Database\DBStructure;
use Friendica\Object\Image;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Strings;
@ -569,7 +569,7 @@ class Installer
if (class_exists('Imagick')) {
$imagick = true;
$supported = Image::supportedTypes();
$supported = Images::supportedTypes();
if (array_key_exists('image/gif', $supported)) {
$gif = true;
}

3
src/Model/Contact.php

@ -25,6 +25,7 @@ use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus;
use Friendica\Protocol\Salmon;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Strings;
@ -719,7 +720,7 @@ class Contact extends BaseObject
}
// Creating the path to the avatar, beginning with the file suffix
$types = Image::supportedTypes();
$types = Images::supportedTypes();
if (isset($types[$avatar['type']])) {
$file_suffix = $types[$avatar['type']];
}

28
src/Model/Photo.php

@ -17,8 +17,8 @@ use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\Model\Storage\IStorage;
use Friendica\Object\Image;
use Friendica\Protocol\DFRN;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Security;
use Friendica\Util\Strings;
@ -404,9 +404,9 @@ class Photo extends BaseObject
"photo", ["resource-id"], ["uid" => $uid, "contact-id" => $cid, "scale" => 4, "album" => "Contact Photos"]
);
if (!empty($photo['resource-id'])) {
$hash = $photo["resource-id"];
$resource_id = $photo["resource-id"];
} else {
$hash = self::newResource();
$resource_id = self::newResource();
}
$photo_failure = false;
@ -425,14 +425,14 @@ class Photo extends BaseObject
}
if (empty($type)) {
$type = Image::guessType($image_url, true);
$type = Images::guessType($image_url, true);
}
$Image = new Image($img_str, $type);
if ($Image->isValid()) {
$Image->scaleToSquare(300);
$r = self::store($Image, $uid, $cid, $hash, $filename, "Contact Photos", 4);
$r = self::store($Image, $uid, $cid, $resource_id, $filename, "Contact Photos", 4);
if ($r === false) {
$photo_failure = true;
@ -440,7 +440,7 @@ class Photo extends BaseObject
$Image->scaleDown(80);
$r = self::store($Image, $uid, $cid, $hash, $filename, "Contact Photos", 5);
$r = self::store($Image, $uid, $cid, $resource_id, $filename, "Contact Photos", 5);
if ($r === false) {
$photo_failure = true;
@ -448,7 +448,7 @@ class Photo extends BaseObject
$Image->scaleDown(48);
$r = self::store($Image, $uid, $cid, $hash, $filename, "Contact Photos", 6);
$r = self::store($Image, $uid, $cid, $resource_id, $filename, "Contact Photos", 6);
if ($r === false) {
$photo_failure = true;
@ -456,24 +456,24 @@ class Photo extends BaseObject
$suffix = "?ts=" . time();
$image_url = System::baseUrl() . "/photo/" . $hash . "-4." . $Image->getExt() . $suffix;
$thumb = System::baseUrl() . "/photo/" . $hash . "-5." . $Image->getExt() . $suffix;
$micro = System::baseUrl() . "/photo/" . $hash . "-6." . $Image->getExt() . $suffix;
$image_url = System::baseUrl() . "/photo/" . $resource_id . "-4." . $Image->getExt() . $suffix;
$thumb = System::baseUrl() . "/photo/" . $resource_id . "-5." . $Image->getExt() . $suffix;
$micro = System::baseUrl() . "/photo/" . $resource_id . "-6." . $Image->getExt() . $suffix;
// Remove the cached photo
$a = \get_app();
$basepath = $a->getBasePath();
if (is_dir($basepath . "/photo")) {
$filename = $basepath . "/photo/" . $hash . "-4." . $Image->getExt();
$filename = $basepath . "/photo/" . $resource_id . "-4." . $Image->getExt();
if (file_exists($filename)) {
unlink($filename);
}
$filename = $basepath . "/photo/" . $hash . "-5." . $Image->getExt();
$filename = $basepath . "/photo/" . $resource_id . "-5." . $Image->getExt();
if (file_exists($filename)) {
unlink($filename);
}
$filename = $basepath . "/photo/" . $hash . "-6." . $Image->getExt();
$filename = $basepath . "/photo/" . $resource_id . "-6." . $Image->getExt();
if (file_exists($filename)) {
unlink($filename);
}
@ -664,7 +664,7 @@ class Photo extends BaseObject
public static function stripExtension($name)
{
$name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
foreach (Image::supportedTypes() as $m => $e) {
foreach (Images::supportedTypes() as $m => $e) {
$name = str_replace("." . $e, "", $name);
}
return $name;

13
src/Model/User.php

@ -22,6 +22,7 @@ use Friendica\Model\TwoFactor\AppSpecificPassword;
use Friendica\Object\Image;
use Friendica\Util\Crypto;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Strings;
use Friendica\Worker\Delivery;
@ -829,15 +830,15 @@ class User
$filename = basename($photo);
$img_str = Network::fetchUrl($photo, true);
// guess mimetype from headers or filename
$type = Image::guessType($photo, true);
$type = Images::guessType($photo, true);
$Image = new Image($img_str, $type);
if ($Image->isValid()) {
$Image->scaleToSquare(300);
$hash = Photo::newResource();
$resource_id = Photo::newResource();
$r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 4);
$r = Photo::store($Image, $uid, 0, $resource_id, $filename, L10n::t('Profile Photos'), 4);
if ($r === false) {
$photo_failure = true;
@ -845,7 +846,7 @@ class User
$Image->scaleDown(80);
$r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 5);
$r = Photo::store($Image, $uid, 0, $resource_id, $filename, L10n::t('Profile Photos'), 5);
if ($r === false) {
$photo_failure = true;
@ -853,14 +854,14 @@ class User
$Image->scaleDown(48);
$r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 6);
$r = Photo::store($Image, $uid, 0, $resource_id, $filename, L10n::t('Profile Photos'), 6);
if ($r === false) {
$photo_failure = true;
}
if (!$photo_failure) {
Photo::update(['profile' => 1], ['resource-id' => $hash]);
Photo::update(['profile' => 1], ['resource-id' => $resource_id]);
}
}
}

205
src/Object/Image.php

@ -6,11 +6,9 @@
namespace Friendica\Object;
use Exception;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Util\Network;
use Friendica\Util\Images;
use Imagick;
use ImagickPixel;
@ -32,31 +30,6 @@ class Image
private $type;
private $types;
/**
* @brief supported mimetypes and corresponding file extensions
* @return array
*/
public static function supportedTypes()
{
if (class_exists('Imagick')) {
// Imagick::queryFormats won't help us a lot there...
// At least, not yet, other parts of friendica uses this array
$t = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif'
];
} else {
$t = [];
$t['image/jpeg'] ='jpg';
if (imagetypes() & IMG_PNG) {
$t['image/png'] = 'png';
}
}
return $t;
}
/**
* @brief Constructor
* @param string $data
@ -67,9 +40,9 @@ class Image
public function __construct($data, $type = null)
{
$this->imagick = class_exists('Imagick');
$this->types = static::supportedTypes();
$this->types = Images::supportedTypes();
if (!array_key_exists($type, $this->types)) {
$type='image/jpeg';
$type = 'image/jpeg';
}
$this->type = $type;
@ -108,20 +81,6 @@ class Image
return $this->imagick;
}
/**
* @brief Maps Mime types to Imagick formats
* @return array With with image formats (mime type as key)
*/
public static function getFormatsMap()
{
$m = [
'image/jpeg' => 'JPG',
'image/png' => 'PNG',
'image/gif' => 'GIF'
];
return $m;
}
/**
* @param string $data data
* @return boolean
@ -142,7 +101,7 @@ class Image
/*
* Setup the image to the format it will be saved to
*/
$map = self::getFormatsMap();
$map = Images::getFormatsMap();
$format = $map[$this->type];
$this->image->setFormat($format);
@ -712,6 +671,26 @@ class Image
return $string;
}
/**
* @brief supported mimetypes and corresponding file extensions
* @return array
* @deprecated in version 2019.12 please use Util\Images::supportedTypes() instead.
*/
public static function supportedTypes()
{
return Images::supportedTypes();
}
/**
* @brief Maps Mime types to Imagick formats
* @return array With with image formats (mime type as key)
* @deprecated in version 2019.12 please use Util\Images::getFormatsMap() instead.
*/
public static function getFormatsMap()
{
return Images::getFormatsMap();
}
/**
* Guess image mimetype from filename or from Content-Type header
*
@ -719,102 +698,24 @@ class Image
* @param boolean $fromcurl Check Content-Type header from curl request
* @param string $header passed headers to take into account
*
* @return object
* @throws \ImagickException
* @return string|null
* @throws Exception
* @deprecated in version 2019.12 please use Util\Images::guessType() instead.
*/
public static function guessType($filename, $fromcurl = false, $header = '')
{
Logger::log('Image: guessType: '.$filename . ($fromcurl?' from curl headers':''), Logger::DEBUG);
$type = null;
if ($fromcurl) {
$headers=[];
$h = explode("\n", $header);
foreach ($h as $l) {
$data = array_map("trim", explode(":", trim($l), 2));
if (count($data) > 1) {
list($k,$v) = $data;
$headers[$k] = $v;
}
}
if (array_key_exists('Content-Type', $headers))
$type = $headers['Content-Type'];
}
if (is_null($type)) {
// Guessing from extension? Isn't that... dangerous?
if (class_exists('Imagick') && file_exists($filename) && is_readable($filename)) {
/**
* Well, this not much better,
* 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();
$image->setInterlaceScheme(Imagick::INTERLACE_PLANE);
} else {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$types = self::supportedTypes();
$type = "image/jpeg";
foreach ($types as $m => $e) {
if ($ext == $e) {
$type = $m;
}
}
}
}
Logger::log('Image: guessType: type='.$type, Logger::DEBUG);
return $type;
return Images::guessType($filename, $fromcurl, $header);
}
/**
* @param string $url url
* @return object
* @return array
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @deprecated in version 2019.12 please use Util\Images::getInfoFromURLCached() instead.
*/
public static function getInfoFromURL($url)
{
$data = [];
if (empty($url)) {
return $data;
}
$data = Cache::get($url);
if (is_null($data) || !$data || !is_array($data)) {
$img_str = Network::fetchUrl($url, true, 4);
if (!$img_str) {
return false;
}
$filesize = strlen($img_str);
try {
if (function_exists("getimagesizefromstring")) {
$data = @getimagesizefromstring($img_str);
} else {
$tempfile = tempnam(get_temppath(), "cache");
$a = \get_app();
$stamp1 = microtime(true);
file_put_contents($tempfile, $img_str);
$a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
$data = getimagesize($tempfile);
unlink($tempfile);
}
} catch (Exception $e) {
return false;
}
if ($data) {
$data["size"] = $filesize;
}
Cache::set($url, $data);
}
return $data;
return Images::getInfoFromURLCached($url);
}
/**
@ -822,50 +723,10 @@ class Image
* @param integer $height height
* @param integer $max max
* @return array
* @deprecated in version 2019.12 please use Util\Images::getScalingDimensions() instead.
*/
public static function getScalingDimensions($width, $height, $max)
{
if ((!$width) || (!$height)) {
return false;
}
if ($width > $max && $height > $max) {
// very tall image (greater than 16:9)
// constrain the width - let the height float.
if ((($height * 9) / 16) > $width) {
$dest_width = $max;
$dest_height = intval(($height * $max) / $width);
} elseif ($width > $height) {
// else constrain both dimensions
$dest_width = $max;
$dest_height = intval(($height * $max) / $width);
} else {
$dest_width = intval(($width * $max) / $height);
$dest_height = $max;
}
} else {
if ($width > $max) {
$dest_width = $max;
$dest_height = intval(($height * $max) / $width);
} else {
if ($height > $max) {
// very tall image (greater than 16:9)
// but width is OK - don't do anything
if ((($height * 9) / 16) > $width) {
$dest_width = $width;
$dest_height = $height;
} else {
$dest_width = intval(($width * $max) / $height);
$dest_height = $max;
}
} else {
$dest_width = $width;
$dest_height = $height;
}
}
}
return ["width" => $dest_width, "height" => $dest_height];
return Images::getScalingDimensions($width, $height, $max);
}
}

26
src/Protocol/ActivityPub/Transmitter.php

@ -6,31 +6,33 @@ namespace Friendica\Protocol\ActivityPub;
use Friendica\BaseObject;
use Friendica\Content\Feature;
use Friendica\Database\DBA;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\Plaintext;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Protocol\Activity;
use Friendica\Util\HTTPSignature;
use Friendica\Core\Protocol;
use Friendica\Model\Conversation;
use Friendica\Model\Contact;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Model\APContact;
use Friendica\Model\Contact;
use Friendica\Model\Conversation;
use Friendica\Model\Item;
use Friendica\Model\Profile;
use Friendica\Model\Term;
use Friendica\Model\User;
use Friendica\Protocol\ActivityPub;
use Friendica\Util\DateTimeFormat;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\Plaintext;
use Friendica\Util\XML;
use Friendica\Util\HTTPSignature;
use Friendica\Util\Images;
use Friendica\Util\JsonLD;
use Friendica\Util\LDSignature;
use Friendica\Model\Profile;
use Friendica\Object\Image;
use Friendica\Protocol\ActivityPub;
use Friendica\Core\Cache;
use Friendica\Util\Map;
use Friendica\Util\Network;
use Friendica\Util\XML;
require_once 'include/api.php';
require_once 'mod/share.php';
@ -1049,7 +1051,7 @@ class Transmitter
// Grab all pictures without alternative descriptions and create attachments out of them
if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
foreach ($pictures[1] as $picture) {
$imgdata = Image::getInfoFromURL($picture);
$imgdata = Images::getInfoFromURLCached($picture);
if ($imgdata) {
$attachments[] = ['type' => 'Document',
'mediaType' => $imgdata['mime'],
@ -1062,7 +1064,7 @@ class Transmitter
// Grab all pictures with alternative description and create attachments out of them
if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) {
$imgdata = Image::getInfoFromURL($picture[1]);
$imgdata = Images::getInfoFromURLCached($picture[1]);
if ($imgdata) {
$attachments[] = ['type' => 'Document',
'mediaType' => $imgdata['mime'],

3
src/Protocol/DFRN.php

@ -35,6 +35,7 @@ use Friendica\Object\Image;
use Friendica\Protocol\ActivityNamespace;
use Friendica\Util\Crypto;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Strings;
use Friendica\Util\XML;
@ -504,7 +505,7 @@ class DFRN
$uid
);
$photos = [];
$ext = Image::supportedTypes();
$ext = Images::supportedTypes();
foreach ($rp as $p) {
$photos[$p['scale']] = System::baseUrl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];

5
src/Protocol/OStatus.php

@ -27,6 +27,7 @@ use Friendica\Network\Probe;
use Friendica\Object\Image;
use Friendica\Protocol\ActivityNamespace;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings;
@ -1389,7 +1390,7 @@ class OStatus
switch ($siteinfo["type"]) {
case 'photo':
if (!empty($siteinfo["image"])) {
$imgdata = Image::getInfoFromURL($siteinfo["image"]);
$imgdata = Images::getInfoFromURLCached($siteinfo["image"]);
if ($imgdata) {
$attributes = ["rel" => "enclosure",
"href" => $siteinfo["image"],
@ -1413,7 +1414,7 @@ class OStatus
}
if (!Config::get('system', 'ostatus_not_attach_preview') && ($siteinfo["type"] != "photo") && isset($siteinfo["image"])) {
$imgdata = Image::getInfoFromURL($siteinfo["image"]);
$imgdata = Images::getInfoFromURLCached($siteinfo["image"]);
if ($imgdata) {
$attributes = ["rel" => "enclosure",
"href" => $siteinfo["image"],

236
src/Util/Images.php

@ -0,0 +1,236 @@
<?php
namespace Friendica\Util;
use Friendica\BaseObject;
use Friendica\Core\Cache;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Imagick;
/**
* Image utilities
*/
class Images
{
/**
* Maps Mime types to Imagick formats
*
* @return array
*/
public static function getFormatsMap()
{
$m = [
'image/jpeg' => 'JPG',
'image/png' => 'PNG',
'image/gif' => 'GIF'
];
return $m;
}
/**
* Returns supported image mimetypes and corresponding file extensions
*
* @return array
*/
public static function supportedTypes()
{
$types = [
'image/jpeg' => 'jpg'
];
if (class_exists('Imagick')) {
// Imagick::queryFormats won't help us a lot there...
// At least, not yet, other parts of friendica uses this array
$types += [
'image/png' => 'png',
'image/gif' => 'gif'
];
} elseif (imagetypes() & IMG_PNG) {
$types += [
'image/png' => 'png'
];
}
return $types;
}
/**
* Guess image mimetype from filename or from Content-Type header
*
* @param string $filename Image filename
* @param boolean $fromcurl Check Content-Type header from curl request
* @param string $header passed headers to take into account
*
* @return string|null
* @throws \Exception
*/
public static function guessType($filename, $fromcurl = false, $header = '')
{
Logger::info('Image: guessType: ' . $filename . ($fromcurl ? ' from curl headers' : ''));
$type = null;
if ($fromcurl) {
$headers = [];
$h = explode("\n", $header);
foreach ($h as $l) {
$data = array_map("trim", explode(":", trim($l), 2));
if (count($data) > 1) {
list($k, $v) = $data;
$headers[$k] = $v;
}
}
if (array_key_exists('Content-Type', $headers)) {
$type = $headers['Content-Type'];
}
}
if (is_null($type)) {
// Guessing from extension? Isn't that... dangerous?
if (class_exists('Imagick') && file_exists($filename) && is_readable($filename)) {
/**
* Well, this not much better,
* 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();
} else {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$types = self::supportedTypes();
$type = 'image/jpeg';
foreach ($types as $m => $e) {
if ($ext == $e) {
$type = $m;
}
}
}
}
Logger::info('Image: guessType: type=' . $type);
return $type;
}
/**
* @param string $url
* @return array
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function getInfoFromURLCached($url)
{
$data = [];
if (empty($url)) {
return $data;
}
$data = Cache::get($url);
if (empty($data) || !is_array($data)) {
$data = self::getInfoFromURL($url);
Cache::set($url, $data);
}
return $data;
}
/**
* @param string $url
* @return array
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function getInfoFromURL($url)
{
$data = [];
if (empty($url)) {
return $data;
}
$img_str = Network::fetchUrl($url, true, 4);
if (!$img_str) {
return [];
}
$filesize = strlen($img_str);
try {
if (function_exists("getimagesizefromstring")) {
$data = @getimagesizefromstring($img_str);
} else {
$tempfile = tempnam(get_temppath(), "cache");
$stamp1 = microtime(true);
file_put_contents($tempfile, $img_str);
BaseObject::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
$data = getimagesize($tempfile);
unlink($tempfile);
}
} catch (\Exception $e) {
return [];
}
if ($data) {
$data['size'] = $filesize;
}
return $data;
}
/**
* @param integer $width
* @param integer $height
* @param integer $max
* @return array
*/
public static function getScalingDimensions($width, $height, $max)
{
if ((!$width) || (!$height)) {
return ['width' => 0, 'height' => 0];
}
if ($width > $max && $height > $max) {
// very tall image (greater than 16:9)
// constrain the width - let the height float.
if ((($height * 9) / 16) > $width) {
$dest_width = $max;
$dest_height = intval(($height * $max) / $width);
} elseif ($width > $height) {
// else constrain both dimensions
$dest_width = $max;
$dest_height = intval(($height * $max) / $width);
} else {
$dest_width = intval(($width * $max) / $height);
$dest_height = $max;
}
} else {
if ($width > $max) {
$dest_width = $max;
$dest_height = intval(($height * $max) / $width);
} else {
if ($height > $max) {
// very tall image (greater than 16:9)
// but width is OK - don't do anything
if ((($height * 9) / 16) > $width) {
$dest_width = $width;
$dest_height = $height;
} else {
$dest_width = intval(($width * $max) / $height);
$dest_height = $max;
}
} else {
$dest_width = $width;
$dest_height = $height;
}
}
}
return ['width' => $dest_width, 'height' => $dest_height];
}
}

5
src/Util/ParseUrl.php

@ -11,7 +11,6 @@ use Friendica\Content\OEmbed;
use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\Object\Image;
/**
* @brief Class with methods for extracting certain content from an url
@ -348,7 +347,7 @@ class ParseUrl
}
$src = self::completeUrl($img_tag['src'], $url);
$photodata = Image::getInfoFromURL($src);
$photodata = Images::getInfoFromURLCached($src);
if (($photodata) && ($photodata[0] > 150) && ($photodata[1] > 150)) {
if ($photodata[0] > 300) {
@ -371,7 +370,7 @@ class ParseUrl
unset($siteinfo['image']);
$photodata = Image::getInfoFromURL($src);
$photodata = Images::getInfoFromURLCached($src);
if (($photodata) && ($photodata[0] > 10) && ($photodata[1] > 10)) {
$siteinfo['images'][] = ['src' => $src,

12
tests/src/Core/InstallerTest.php

@ -351,12 +351,9 @@ class InstallerTest extends MockedTest
*/
public function testImagick()
{
$this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
$this->markTestIncomplete('needs adapted class_exists() mock');
$imageMock = \Mockery::mock('alias:'. Image::class);
$imageMock
->shouldReceive('supportedTypes')
->andReturn(['image/gif' => 'gif']);
$this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
$this->setClasses(['Imagick' => true]);
@ -382,11 +379,6 @@ class InstallerTest extends MockedTest
{
$this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
$imageMock = \Mockery::mock('alias:' . Image::class);
$imageMock
->shouldReceive('supportedTypes')
->andReturn([]);
$this->setClasses(['Imagick' => true]);
$install = new Installer();

Loading…
Cancel
Save