If you have some of those clients who uploads gigantic image files for simple corporate websites you know what I’m talking about. I know, because I have too and it could be very overwhelming most of time.
We’ve been talked about this with my friend Kenan and we’ve decided to keep future uploads small and optimize past uploads to save our server resources and increase page speeds and seo scores of our clients. There is many wins out there.
# Step 1
Prevent gigantic file uploads with simple but effective plugin: Resize Image After Upload
This little plugin is a smart and very useful companion to prevent big image files from your uploads directory.
You could resize source files with this plugin when uploading files to your server via WordPress. Easy peasy lemon squeezy!
But this plugin can’t resize past uploads and I wouldn’t want to use that feature even if this plugin could resize the past uploads because I could do that for all my clients at the same time over my server :)
# Step 2
Compress and resize all image files in home directory on your server
First of all, I have to say, I love it! It was insane :D
Because the traditional method scared me and I started thinking about how it could be done easily.
I just found it: the bash :)
There are three parts of this process:
First: Install ImageMagick to your server if you had not yet.
Second: Resize and optimize all images under /home directory which holds our all system users’ folders.
Third: Repair ownerships, because of that bash script sets all ownerships as root while process.
Attention! You have to backup your server or home directory before trying this because it uses mogrify which overrides existing files to keep folder structure. Just be careful!
Here is the codes:
# access your server via terminal with root privileges ssh root@your-server-ip-or-domain # install imagemagick apt install imagemagick-6.q16 -y && apt install graphicsmagick-imagemagick-compat -y && apt install imagemagick-6.q16hdri -y # go to home cd /home # compress images with %70 quality and resize # bigger files then 1500px to it find . -name "*.jpg" -print0 | xargs -0 mogrify -quality 70% -resize '>1500x' # repair ownerships via folder names # which are our system user names for dir in *; do sudo chown -R "$dir":"$dir" ./ ; done
Cool, isn’t it :D
Note: You could face some ImageMagick warnings while process because of that corrupted files but don’t worry about it; because those files are already broken before we do this so ImageMagick couldn’t handle it, obviously.
I had used almost 13GB space and after this it become under 12GB whitin 13min.
We could add other file types like png or gif but for start this should be fine for us.
Hope this helps someone ;)