1

I backup our family photos to my home Ubuntu 14.04 server, to a Samba directory at /srv/photos. I want to be able to view them by visiting a web page, to browse through them, and have created a php page to do just that. Rather than using the actual 3-5Mb photos themselves though, which would be very slow, I want to use Imagemagick to create 200-300Kb copies in a folder (/var/www/album for instance), and then point my php page at that.

I want to create a script which will recreate the folder structure of /srv/photos/ in /var/www/album, and also run Imagemagick on each photo within each sub-folder to reduce their file size. In the end I want to end up with a folder structure in /var/www/album mirroring that of /srv/photos, with all jpg's of a reduced size.

I've seen commands which will mirror the folder structure:

$ find -type d -exec mkdir -p /target-folder/{} \;

and Imagemagick will reduce file quality & size (some experimentation required...):

convert holiday_pic.jpg reduced_holiday_pic.jpg -quality 50

It feels like I would need some sort of array to do this... for each jpg in /srv/photos/folder_X, convert and save it to /var/www/album/folder_X...

Any help appreciated

0

You can use rsync for synchronizing files and folders structure and then mogrify to lower files quality.

#!/bin/bash

IFS=$'\t\n'

files=`rsync -rvu /srv/photos/ /var/www/album/ |grep -i jpg` && cd /var/www/album
mogrify -quality 50 $files    

Mogrify is the same as convert but it is overwriting changed files (in this example after synchronizing in your album folder)
IFS is set for files and folders with spaces

rsync options:

-r be recursive - for folders structure
-v verbose - tell what you are doing 
-u update - skip files that are newer on the receiver    

When there are different file types in your photos folder you can tell rsync to synchronize only jpg files by adding this to rsync command:

--include '*/' --include '*.jpg' --exclude '*'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.