MyBB Community Forums

Full Version: Resize image attachments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have seen a number of tools that talk of resizing images, but so far each of those tools was just resizing the display of the image, not the image file itself.  Cameras these days are creating images with a far higher resolution than is needed for the display of images attachments on my BB.  I would rather not waste the space of storing a 5184×2456 image when it is only going to be displayed at a resolution of 900x426.

Mybb already attempts to make a thumbnail file of an attachment, in "upload_attachment" in inc/functions_upload.php.  I modified this code to have it also create a second bigger thumbnail, what I will call a toenail, with max height and width of 900. After line 632, I added code to create the toenail file, and if successful, rename the toenail file to be the original downloaded attachment file.  It seems to work.  I am sharing what I have done here, asking if anyone has dealt with this themselves and if so, are there any "gotchas" that you can see in the code I have created.

The code added at the end of "upload_attachment (after line 632).


                $toename = str_replace(".attach", "_small.$ext", $filename);
                $toepathname = $mybb->settings['uploadspath']."/".$toename;
                $filepathname = $mybb->settings['uploadspath']."/".$filename;
                $toenail = generate_thumbnail($filepathname, $mybb->settings['uploadspath'], $toename, 900, 900);
                if($toenail['filename'])
                {
                        if (!rename($toepathname, $filepathname))
                                unlink($toepathname);
                }
Thx for sharing....it's a nice function!

I did the same on another way and saved with it 80% of server storage space.

btw: this should be a core function Wink
This is a great idea for plugin Wink
Wow, very nice work Smile
I agree with @SvePu, this should be a core function.
This is great! I noticed that it doesn't update the attachment size in the forum database so even though when I download the image it appears to be significantly smaller, myBB still thinks it's several MB. Does anyone know if there could be a fix for this?

(2019-03-15, 01:30 PM)PEpler Wrote: [ -> ]This is great! I noticed that it doesn't update the attachment size in the forum database so even though when I download the image it appears to be significantly smaller, myBB still thinks it's several MB. Does anyone know if there could be a fix for this?

I managed to update the code so that it updates the 'filesize' value in the attacharray so when it is uploaded it will have the correct value. It doesn't seem to break anything so I thought I would share it.
 
$toename = str_replace(".attach", "_small.$ext", $filename);
$toepathname = $mybb->settings['uploadspath']."/".$toename;
$filepathname = $mybb->settings['uploadspath']."/".$filename;
$toenail = generate_thumbnail($filepathname, $mybb->settings['uploadspath'], $toename, 900, 900);
if($toenail['filename'])
{
       if (!rename($toepathname, $filepathname))
             unlink($toepathname);
             $attacharray['filesize'] = filesize($filepathname);
}
Its only working fine for me. I have tried lots of methods to resize the image before try your codes. Thank you so much for your tutorial.
(2019-03-15, 01:30 PM)PEpler Wrote: [ -> ]This is great! I noticed that it doesn't update the attachment size in the forum database so even though when I download the image it appears to be significantly smaller, myBB still thinks it's several MB. Does anyone know if there could be a fix for this?

(2019-03-15, 01:30 PM)PEpler Wrote: [ -> ]This is great! I noticed that it doesn't update the attachment size in the forum database so even though when I download the image it appears to be significantly smaller, myBB still thinks it's several MB. Does anyone know if there could be a fix for this?

I managed to update the code so that it updates the 'filesize' value in the attacharray so when it is uploaded it will have the correct value. It doesn't seem to break anything so I thought I would share it.
 
$toename = str_replace(".attach", "_small.$ext", $filename);
$toepathname = $mybb->settings['uploadspath']."/".$toename;
$filepathname = $mybb->settings['uploadspath']."/".$filename;
$toenail = generate_thumbnail($filepathname, $mybb->settings['uploadspath'], $toename, 900, 900);
if($toenail['filename'])
{
       if (!rename($toepathname, $filepathname))
             unlink($toepathname);
             $attacharray['filesize'] = filesize($filepathname);
}
It looks like with 1.8.23 they have changed the upload_attachment function and this code will no longer work in the way it is described above. I haven't fully reviewed the code yet to see if they have added this functionality, but there was a major rewrite to this function with this release.
(2020-07-21, 03:00 PM)PEpler Wrote: [ -> ]It looks like with 1.8.23 they have changed the upload_attachment function and this code will no longer work in the way it is described above. I haven't fully reviewed the code yet to see if they have added this functionality, but there was a major rewrite to this function with this release.

I don't know whether there were any changes than the one I submitted, which only refactors error checking, so unless other changes were made, your code will probably work just fine still.
Is there any chance to make this as a stabdalone plugin or send a PR into the mybb repo?
(2020-12-19, 09:08 PM)Eldenroot Wrote: [ -> ]Is there any chance to make this as a stabdalone plugin or send a PR into the mybb repo?

After discussing this privately, @Eldenroot and I decided to create a standalone plugin, to be maintained by the unofficial MyBB Group, based on azalea4va's excellent idea and code snippet in this thread. Here is its thread:

Image Auto-Resizer