How to decrease your attachments folder by 50-80%

You downloaded 8GB of images off your server and then reuploaded them? How long did that take!? Download the software and drag and drop into program? You do this, what, like once a month?
I did it the first time. The frequency depends on your community habits and size. I would probably do it once in 4-6 months since there is not sense doing it every month to save max 100-200MB.

Maybe he has 1GB connection at home :)
I have 50MB symmetric. I put it to upload at night so I don't know how much time it took, probably 4 hours.
 
On your server, within your XF attachments dir ...
Code:
find . -iname *.jpg -exec jpegoptim -p {} \;
 
On your server, within your XF attachments dir ...
Code:
find . -iname *.jpg -exec jpegoptim -p {} \;
The problem with jpegoptim is that you need to set a max quality factor or you compromise quality. I did a lot of testing and jpegoptim is way far from JPEGmini in terms of compression vs. original quality.

Every file in my attachment folders is .data
you need the [bd]attachments store add-on.
 
The problem with jpegoptim is that you need to set a max quality factor or you compromise quality
No, jpegoptim is lossless optimisation, unless you set a max quality factor and then it performs lossy optimisation. By default, as per my example command line above, it's lossless.
 
No, jpegoptim is lossless optimisation, unless you set a max quality factor and then it performs lossy optimisation. By default, as per my example command line above, it's lossless.
JPEGmini does lossy. kraken does lossless. JPEGmini does most of the compression (30-50% on average), while kraken (which compresses the files in greater numbers than jpegoptim) does only the final touches (5-10% on average).
 
I upload a lot of png images to my forum for screen shots, so optipng is great.

Bash script to run on the first of the month, and do the stuff on the files

Code:
#!/bin/bash
# Requires [bd] Attachment Store for Directory Structure
# https://xenforo.com/community/resources/bd-attachment-store.1779/

ATTACHMENTS="/var/www/mattwservices.co.uk/public/data/attachment-files"
YEAR=`date --date='last month' +"%Y"`
MONTH=`date --date='last month' +"%m"`
LOG="/root/scripts/optimize.log"

#Optimize jpg
for i in $ATTACHMENTS/$YEAR/$MONTH/*.jpg;
        do jpegoptim "$i";
        echo "$i optimized" >> $LOG
done

#Optimize png
for i in $ATTACHMENTS/$YEAR/$MONTH/*.png;
        do optipng -preserve -o7 "$i";
        echo "$i optimized" >> $LOG
done
 
Matt, there is might a way without bd attachment over the mime type
jpegoptim can read the mime type from the .data files

Code:
[root@host 1]# jpegoptim 1877-89eb27717f1af1338c5c1a3f789492f6.data
1877-89eb27717f1af1338c5c1a3f789492f6.data 1024x656 24bit N Exif XMP JFIF  [OK] 205880 --> 196801 bytes (4.41%), optimized.
[root@host 1]#
 
  • Like
Reactions: gfc
But then why do you need bd attachment store? You could get the creation or modification date.
 
But then why do you need bd attachment store? You could get the creation or modification date.
For my above example, it works nicely with attachment store, because the images are stores in monthly folders, so I can run the script on the first of each month for last months folder. That means it doesn't need do any checks for edited or previously optimized files.
 
  • Like
Reactions: gfc
New version using mozjpeg (the below works in the standard attachment directory and reads the mime type of the file first, and then applies the correct optimization program to it)

Code:
#!/bin/bash

for i in *.data
        do file --mime-type $i
done | while read line
        do
                if [[ "$line" =~ "png" ]]
                then PNG=`echo $line | cut -d ':' -f1`
                optipng -preserve -o7 $PNG
                elif [[ "$line" =~ "jpeg" ]]
                then JPEG=`echo $line | cut -d ':' -f1`
                mozjpeg -copy none $JPEG > comp_$JPEG; mv -f comp_$JPEG $JPEG
                fi
        done
 
  • Like
Reactions: gfc
With mozjpeg

Before
-rw-rw-rw- 1 php-fpm root 660K Jan 22 19:10 1740_Screenshot_2015-01-22-19-07-25.jpg
After
-rw-r--r-- 1 root root 532K Jan 23 15:41 1740_Screenshot_2015-01-22-19-07-25.jpg
 
If you run this on the .data files, you'll get errors on the attachments.

1748_upload_2015-1-23_16-44-56.png


You need to figure a way to update the file size information in the database table for the attachments.
 
If you run this on the .data files, you'll get errors on the attachments.

1748_upload_2015-1-23_16-44-56.png


You need to figure a way to update the file size information in the database table for the attachments.
Would an attachments rebuild correct the errors?
 
Back
Top Bottom