Resizing Images to Save Space

As the collection of my content grows, so would my need for data storage and network. Although hundreds of megabytes might not be that big, it can be cumbersome to transfer over the network. I would also like to keep things scalable.

I noticed that most of the space in my content is in image, specifically the ones I took with my phone camera. On the directory user/pages/01.journal where my blog contents are, the size got up to 80 megabytes.

I don't need the high resolution, so I decided to reduce it.

I started by installing imagemagick to get convert command

# apt install imagemagick

I don't need to convert all the images. The screenshots, for example, are within hundreds of kilobytes and we can skip those. So I came up with the list of images bigger than 1M with:

$ find -size +1M
./20220924--x230-customization/orico-msata-box-open-2.jpg
./20220924--x230-customization/orico-msata-box-open-1.jpg
./20220924--x230-customization/orico-msata-box.jpg
./20230720--w530-coreboot/ch341a-arduino-flasher.jpg
./20230717--w530-from-parts/cardboard-setup.jpg
./20230717--w530-from-parts/almost-complete.jpg
./20230717--w530-from-parts/w530-motherboard.jpg
./20230717--w530-from-parts/keyboard-fitted.jpg
./20230717--w530-from-parts/with-bottom-case.jpg
./20220919--x230-coreboot/skulls-free-mint-grub.jpg
./20220919--x230-coreboot/skulls-free-splash.jpg
./20220919--x230-coreboot/skulls-mint-grub.jpg
./20220919--x230-coreboot/skulls-splash.jpg
./20220921--x230-me-cleaning/exposing-flash-chips.jpg
./20220921--x230-me-cleaning/keyboard-palmrest-removed.jpg
./20220921--x230-me-cleaning/attaching-clip.jpg

Then, a little test run with one of the image file:

$ convert -resize 20% ./20230717--w530-from-parts/with-bottom-case.jpg a.jpg
$ ls -lh ./20230717--w530-from-parts/with-bottom-case.jpg
-rw-rw-r-- 1 user www-data 3.2M Jun 19  2023 ./20230717--w530-from-parts/with-bottom-case.jpg
ls -lh a.jpg 
-rw-r--r-- 1 user user  173K Apr 12 13:35 a.jpg

From 3.2 mb to 173 kb. Not bad.

Now onto the real thing

$ du -sh
80M     .
$ find -name '*.jpg' -size +1M -exec convert -resize 20% {} {} \;
$ du -sh
6.1M    .

From 80 mb to 6.1 mb. Imagine that times a hundred!

Fixing Image Orientation

Simple! Also with imagemagick's convert command.

convert -rotate <degree> image.jpg image.jpg

I'd use either 90, -90, or 180 for the <degree> depending on the image.

Tuning Performance

I never have any problem with my site's performance, but just for the sake of it, I'd play around a little bit.

I installed APCu for in-memory caching, and php-yaml for native YAML processing

# sudo apt install php-apcu php-yaml

Then on user/config/system.yaml, I appended these to ensure it's using APCu as well as using hash-based cache checking:

cache:
  check:
    method: hash
  driver: apc

Fixing Cache Permissions

I've been quite annoyed by the permission error when cleaning cache as user.

$ bin/grav clean

DELETING
file: /srv/timkenhan.co/user/config/security.yaml

 [ERROR] Failed to delete /srv/timkenhan.co/cache/compiled/: Unknown error

I started by appending user/config/system.yaml with:

images:
  cache_perms: '0775'

I then created setup.php with:

<?php
umask(0002);

Last but not least, I ran:

# usermod -aG www-data user

Just in case:

# chown user:www-data -R logs/
# chmod 664 logs/grav.log

And for completeness:

$ find user -type d -exec chmod 775 {} \;
$ find user -type f -exec chmod 664 {} \;

I tried to disable group write permission on user/, but the system tends to fail inexplicably, so I gave up and just left it as is.