News:

Wondering if this will always be free?  See why free is better.

Main Menu

Automatic Attachment Rotation (and Resize)

Started by dougiefresh, March 04, 2016, 07:34:59 PM

Previous topic - Next topic

GL700Wing

Quote from: Sir Osis of Liver on April 08, 2023, 11:59:59 PMNot really sure what's happening.  Trying to update my oldest forum (Oct 2010) to run in php 8.0 with responsive theme (customized Nightbreeze).  Started with the theme, then everything went sideways, lot of mod errors.  I've restored files/database back to Apr 1 and reverted to old theme and everything's working nicely in 8.0, no errors.  This mod is currently uninstalled, will install it tomorrow and see what happens.  Host is migrating my account to new cloud platform sometime tonight, don't want to be in middle of something when they pull the plug.  I suspect it's a theme issue, I've installed the mod on several forums without problems.  Will let you know once I've got it reinstalled.

OK - I've looked into this some more just now and the line of code in Subs-AutoRotation.php that's throwing the error calls the 'getAttachmentFilename' function in Subs.php and that function expects the setting 'attachmentUploadDir' to have a value.

Maybe try running 'repair_settings' and/or the checking that all the required avatar settings are configured.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

GL700Wing

#421
I've been able to reproduce this error with SMF 2.0.19 and both PHP 7.4 and 8.0 but it specifically relates to user uploaded avatars for which all of the following conditions are met:
1. The uploaded avatar has been saved into the default attachments directory (ie, not into a separate avatars directory)*;
2. The avatar have been saved in JPG format;
3. This mod is enabled; and
4. The mod option 'Resize, compress and/or reformat existing images when messages are displayed' is enabled.


* This is relevant because, and contrary to the information posted here, uploaded avatars that are saved into the default attachments directory are given an 'attachment_type' value of 0 in the database whereas uploaded avatars that are saved into a separate avatars directory are given an 'attachment_type' value of 1.
You cannot view this attachment.

By default this mod will not try to resize JPG avatars but the test used for determining if a JPG image is an avatar was based on the assumption that all user uploaded avatars had an 'attachment_type' value of 1.

For the next release I've updated the mod to use a more robust test for determining if a JPG image is an avatar.

In the meantime the workaround is to disable the mod option 'Resize, compress and/or reformat existing images when messages are displayed'.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Sir Osis of Liver

Ok, cleaned up install and theme, all mods installed ok, one manual edit for this mod in Subs-Graphics.php.  Member's avatar does not display, getting these errors:


https://www.thekrashsite.com/ferrarum/smf/index.php?action=dlattach;attach=25;type=avatar
Path cannot be empty

https://www.thekrashsite.com/ferrarum/smf/index.php?action=dlattach;attach=25;type=avatar
2: Trying to access array offset on value of type null
File: /home/thekrash/public_html/ferrarum/smf/Sources/Subs-AutoRotation.php
Line: 477


Unistalled mod, avatar reappears.  Avatar is an uploaded jpg in /attachments.

Manual edit was for this -


    );

    require_once($sourcedir . '/Subs-Package.php');
    @ini_set('memory_limit', '512M');


I had increased memory limit at some point.  There's a stray character in replacement code -


,
        // Automatic Attachment Rotation (and Resize) mod.
        // Added support for 'webp' images.
        '18' => 'webp',
    );

    require_once($sourcedir . '/Subs-Package.php');

    // Begin Automatic Attachment Rotation (and Resize) mod.
    //@ini_set('memory_limit', '90M');

    global $modSettings;
    $useImagick = $imageMemory = false;
    if (!empty($modSettings['attachment_enableAutoRotateMod']))
    {
        require_once($sourcedir . '/Subs-AutoRotation.php');
        $useImagick = class_exists('Imagick', false) && !empty($modSettings['attachment_use_imagick']);
        $imageMemory = !empty($modSettings['attachment_memory_limit']);
    }
    // End Automatic Attachment Rotation (and Resize) mod.


Comma upper left, makes no difference either way.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

GL700Wing

Quote from: Sir Osis of Liver on April 09, 2023, 07:56:25 PMOk, cleaned up install and theme, all mods installed ok, one manual edit for this mod in Subs-Graphics.php.  Member's avatar does not display, getting these errors:

https://www.thekrashsite.com/ferrarum/smf/index.php?action=dlattach;attach=25;type=avatar
Path cannot be empty

https://www.thekrashsite.com/ferrarum/smf/index.php?action=dlattach;attach=25;type=avatar
2: Trying to access array offset on value of type null
File: /home/thekrash/public_html/ferrarum/smf/Sources/Subs-AutoRotation.php
Line: 477

Unistalled mod, avatar reappears.  Avatar is an uploaded jpg in /attachments.
Okay - as per my reply above the workaround is to disable the mod option 'Resize, compress and/or reformat existing images when messages are displayed'.

However, if you don't want to do that another option is to add the following code to ./Sources/Subs-AutoRotation.php
Find:
    if (!is_array($attachment) && ($attachmentType == 0 || $attachmentType == 3))

Add Before:
    // Nothing to do for JPG avatars that have snuck through ...
    if (isset($_REQUEST['type']) && $_REQUEST['type'] == 'avatar')
        return;

Note: This is the new avatar test I've added for the next version of this mod and, due to this file being removed/replaced when the mod is subsequently uninstalled/upgraded, making this code change will not cause any problems for either the uninstall/upgrade processes.



QuoteManual edit was for this -

    );

    require_once($sourcedir . '/Subs-Package.php');
    @ini_set('memory_limit', '512M');


I had increased memory limit at some point.  There's a stray character in replacement code -

,
        // Automatic Attachment Rotation (and Resize) mod.
        // Added support for 'webp' images.
        '18' => 'webp',
    );

    require_once($sourcedir . '/Subs-Package.php');

    // Begin Automatic Attachment Rotation (and Resize) mod.
    //@ini_set('memory_limit', '90M');

    global $modSettings;
    $useImagick = $imageMemory = false;
    if (!empty($modSettings['attachment_enableAutoRotateMod']))
    {
        require_once($sourcedir . '/Subs-AutoRotation.php');
        $useImagick = class_exists('Imagick', false) && !empty($modSettings['attachment_use_imagick']);
        $imageMemory = !empty($modSettings['attachment_memory_limit']);
    }
    // End Automatic Attachment Rotation (and Resize) mod.

Comma upper left, makes no difference either way.

It's not a stray character - the comma is needed to add another element to the 'default_formats' array in the 'resizeImageFile' function and the net effect of this code update in a default SMF 2.0.19 ./Sources/Subs-Graphics.php file is to change this block of code ...
    static $default_formats = array(
        '1' => 'gif',
        '2' => 'jpeg',
        '3' => 'png',
        '6' => 'bmp',
        '15' => 'wbmp'
    );

    require_once($sourcedir . '/Subs-Package.php');
    @ini_set('memory_limit', '90M');

    $success = false;

... to this block of code:
    static $default_formats = array(
        '1' => 'gif',
        '2' => 'jpeg',
        '3' => 'png',
        '6' => 'bmp',
        '15' => 'wbmp',
        // Automatic Attachment Rotation (and Resize) mod.
        // Added support for 'webp' images.
        '18' => 'webp',
    );

    require_once($sourcedir . '/Subs-Package.php');

    // Begin Automatic Attachment Rotation (and Resize) mod.
    //@ini_set('memory_limit', '90M');

    global $modSettings;
    $useImagick = $imageMemory = false;
    if (!empty($modSettings['attachment_enableAutoRotateMod']))
    {
        require_once($sourcedir . '/Subs-AutoRotation.php');
        $useImagick = class_exists('Imagick', false) && !empty($modSettings['attachment_use_imagick']);
        $imageMemory = !empty($modSettings['attachment_memory_limit']);
    }
    // End Automatic Attachment Rotation (and Resize) mod.

    $success = false;

Finally, the attachment memory processing limit can be configured via a mod setting.
You cannot view this attachment.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Sir Osis of Liver

Will do the code change tonight.  Hard part will be remembering which forums I've installed this on, but haven't had any complaints.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Ricsca

This mod should default to smf, it saves a lot of memory by resizing images.
Thanks

Arantor

Quote from: Ricsca on April 14, 2023, 04:53:44 PMThis mod should default to smf

It should be an option, yes, but not necessarily the default; there are people who will not want the rotation or resizing automatically. Photography forums, for example.

GL700Wing

Quote from: Arantor on April 14, 2023, 04:56:57 PMIt should be an option, yes, but not necessarily the default; there are people who will not want the rotation or resizing automatically. Photography forums, for example.
Automatic image rotation is the default behaviour in SMF 2.1.x and without this mod there is no option to disable it ...
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Arantor

It is? I don't remember it being added, and honestly... it's not a solid default to have that can't be changed.

Permanently altering an image as an upload is not the sort of thing that should be done lightly.

GL700Wing

#429
Quote from: Arantor on April 14, 2023, 06:43:21 PMIt is? I don't remember it being added, and honestly... it's not a solid default to have that can't be changed.
Definitely happens for JPEG images with orientation EXIF metadata ...


Interestingly, ./Sources/Subs-Graphics.php also contains code in the 'resizeImage' function that is probably intended to automatically save JPEG images at 82% compression (a hard coded value rather than a configurable setting) if iMagick is available but that isn't happening because the wrong iMagick method is used.
            if ($default_formats[$preferred_format] == 'jpeg')
                $imagick->setCompressionQuality(!empty($modSettings['avatar_jpeg_quality']) ? $modSettings['avatar_jpeg_quality'] : 82);

The method should be 'setImageQuality' instead of 'setCompressionQuality'.  Also, I can't find any setting for avatar JPEG quality ...
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Kindred

That's browser behavior based on the exif,  not the system
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

GL700Wing

#431
Quote from: Kindred on April 14, 2023, 07:50:25 PMThat's browser behavior based on the exif,  not the system
I've tested this and can confirm that even though a browser will automatically display a rotated JPEG image when it is uploaded it is the code in the 'resizeImage' function in ./Sources/Subs-Graphics.php that saves it as a rotated JPEG image to the file system.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Arantor

I remember fighting so hard with EXIF orientation in LevGal because browsers *didn't* display that correctly, haha.

But yeah, auto-damaging images (folks without ImageMagick will get the EXIF data all stripped) isn't cool. Especially as a non trivial selection of hosts don't use either Imagick or MagickWand but punt it out to the shell version.

@rjen

I like this mod, mod not the amount of code changes it makes.

I would like the auto resize option as a stand-alone hooks only mod.
Also would not mind helping putting that mod together ...
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

GL700Wing

Quote from: @rjen on April 15, 2023, 02:12:30 AMI like this mod, mod not the amount of code changes it makes.
Most of the source code changes are for handling automatic and/or manual image rotation.

QuoteI would like the auto resize option as a stand-alone hooks only mod.
I worked with @dougiefresh just over five years ago to add automatic image resizing to this mod because at the time it made sense to both of us for images that needed to be rotated to also be resized but also for both of these features to work independently of each other.

This mod has four separate features for image resizing:
1. Automatically resize new images using mod settings when they are saved to a new or existing message;
2. Automatically resize existing images using mod settings when a message is displayed (this function is of use when the mod is installed on a forum where uploaded images have not previously been resized and/or when the mod settings are changed.);
3. Function to batch resize all existing images using mod settings (this function is also of use when the mod is installed on a forum where uploaded images have not previously been resized and/or when the mod settings are changed but once this is done the setting for automatically resizing existing images using mod settings when a message is displayed can be disabled); and
4. Option to have different two different sets of mod settings for resizing (this enables some member groups to choose which set of resizing options to use when they upload an image - I find this particularly useful for managing the resizing of technical images that have been saved in JPEG format).  That said, I have not been able to work out how to make this a selectable option in SMF 2.1.x ...

I'll start looking into what I need to do to develop a separate 'Resize' mod that is hooks only (but only SMF 2.1.x).

QuoteAlso would not mind helping putting that mod together ...
Thanks.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Volker S

I'm an absolute layman and can therefore not judge whether my proposal is feasible or not.
A great option, which is admittedly not often needed, would be a presentation in an external graphics program to be able to show details better.
This means that the graphic does not need to be scaled down to forum size, but just enough to fill a standard screen (adjustable).
Otherwise, your addon is absolutely top class and I thank you very much for the time you spent on it.

Ricsca

the ability to resize images is more fundamental than rotation.

Arantor

Says you. I know at least one community that would rather not have rotation *OR* resizing in favour of full size full quality. That alone means it must be an option rather than the ONLY option as you're outlining.

In any case, storage isn't that expensive - Hetzner has 100GB storage for around $10/month, 300GB for $20/month which includes web hosting. But if you're in a place where your budget is that constrained, I'd be wondering if running a forum at all is viable - especially since I've seen you complain so furiously at how expensive IPS is over at TAZ...

bmwplow

Hello,

Testing this on my test forum. SMF 2.0.19 with Nightbreeze before I roll it out on my production forum. 5.9 has been working great, but when changing to 6.16 I am getting some errors and can't even load the attachment settings section:

When loading the attachment settings section:

An Error Has Occurred!
syntax error, unexpected 'attachment_enableAutoRotateMod' (T_STRING), expecting ')'

At top of forum, also have:
// Automatic Attachment Rotation (and Resize) mod. $txt['attachment_autorotation_settings'] = 'Automatic Attachment Rotation (and Resize) mod.'; $txt['attachment_manager_resize'] = 'Resize/Reformat Existing Images'; $txt['attachment_manager_resize_no_entries'] = 'There are no images for
Resize/Reformat Existing Images
to process.';

Running php 7.4 currently but hoping to fix this and convert to at least 8.0 soon to keep host happy.

Any suggestions would be appreciated. I love the mod.

Thank you so much.

GL700Wing

The problem you've reported is most likely due to a corruption of the ./Themes/default/languages/Admin.english.php file.

When this mod is installed it adds these strings just before the end of the file but sometimes other mods do not install their strings correctly which results in the last line of file not being in the correct/expected format.

The last line of the file should look like this (ie, the '?>' characters should be on a separate line):
?>

However, if another mod has added its strings incorrectly the '?>' characters may now be at the end of a text string line with the result that the strings added by this mod now appear below these characters.

The fix is to edit the language file, delete the '?>' characters from the end of the text string line where they currently appear, and add the following line to the end of the file:
?>

Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Advertisement: