]> git.mxchange.org Git - friendica.git/commitdiff
Guess the mimetype in advance
authorMichael <heluecht@pirati.ca>
Wed, 30 Jun 2021 04:28:03 +0000 (04:28 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 30 Jun 2021 04:28:03 +0000 (04:28 +0000)
src/Model/Photo.php
src/Module/Photo.php

index fe416c360f54e55d1c19c16e99867dd4a65e9ebf..61fc2df4ea1f2d7bcf7909797a4866acd2415caf 100644 (file)
@@ -245,13 +245,17 @@ class Photo
         * Construct a photo array for a system resource image
         *
         * @param string $filename Image file name relative to code root
-        * @param string $mimetype Image mime type. Defaults to "image/jpeg"
+        * @param string $mimetype Image mime type. Is guessed by file name when empty.
         *
         * @return array
         * @throws \Exception
         */
-       public static function createPhotoForSystemResource($filename, $mimetype = "image/jpeg")
+       public static function createPhotoForSystemResource($filename, $mimetype = '')
        {
+               if (empty($mimetype)) {
+                       $mimetype = Images::guessTypeByExtension($filename);
+               }
+
                $fields = self::getFields();
                $values = array_fill(0, count($fields), "");
 
@@ -269,13 +273,17 @@ class Photo
         *
         * @param string $url      Image URL
         * @param int    $uid      User ID of the requesting person
-        * @param string $mimetype Image mime type. Defaults to "image/jpeg"
+        * @param string $mimetype Image mime type. Is guessed by file name when empty.
         *
         * @return array
         * @throws \Exception
         */
-       public static function createPhotoForExternalResource($url, $uid = 0, $mimetype = "image/jpeg")
+       public static function createPhotoForExternalResource($url, $uid = 0, $mimetype = '')
        {
+               if (empty($mimetype)) {
+                       $mimetype = Images::guessTypeByExtension($url);
+               }
+
                $fields = self::getFields();
                $values = array_fill(0, count($fields), "");
 
index e5042fad4e8b1d2b5747c913fe04f4509e9edfb9..3b4b7cc56967782bb272074b392d6637545a4d9d 100644 (file)
@@ -29,6 +29,7 @@ use Friendica\Model\Contact;
 use Friendica\Model\Photo as MPhoto;
 use Friendica\Model\Post;
 use Friendica\Model\Storage\ExternalResource;
+use Friendica\Model\Storage\SystemResource;
 use Friendica\Util\Proxy;
 use Friendica\Object\Image;
 use Friendica\Util\Images;
@@ -102,8 +103,8 @@ class Photo extends BaseModule
                $stamp = microtime(true);
                $imgdata = MPhoto::getImageDataForPhoto($photo);
 
-               // The mimetype for an external resource can only be known after it had been fetched
-               if ($photo['backend-class'] == ExternalResource::NAME) {
+               // The mimetype for an external or system resource can only be known reliably after it had been fetched
+               if (in_array($photo['backend-class'], [ExternalResource::NAME, SystemResource::NAME])) {
                        $mimetype = Images::getMimeTypeByData($imgdata);
                        if (!empty($mimetype)) {
                                $photo['type'] = $mimetype;