androidcodeexamples.com
Offcode ltd logo
ANDROID
APACHE
THUMBNAILS
SCRIPTS
GFX
World-class examples brought to you by Offcode ltd
APACHE
Getting started Upload to server Image server Image manipulation Image cache reCAPTCHA Front Page
androidcodeexamples.com

Apache Scripting: Manipulating images in cloud storage

The second Apache example illustrated how a jpeg image may be served via a combined shell script / Apache cgi-bin. Here we get more technical and actually change the image before it's downloaded or served to the browser. Usually this could happen right after storing the image so next time it's accessed it's ready. Of course the situation is different if you wish to manipulate the images online upon user-request.

First, let's install the imagemagick:

root@server261583:/var/www/html# apt-get install imagemagick

Recall our previous example script that only outputs the jpeg image. However, we no longer output the Content-Length. It works without as well.

#!/bin/sh

cd /home/imageserviceagent/ 2>/dev/null > /dev/null

if [ $? -eq 0 ]; then
    /bin/echo "Content-type: image/jpeg"
    /bin/echo ""
    /bin/cat /home/imageserviceagent/testimage.jpg
    exit
fi

Shell script to resize an image

The next example is similar except it does some image manipulation on the server side. Now we use the imagemagick to resize the original image by 50%. Let's create a script called 'shrink':

root@server261583:/usr/lib/s# pico shrink

It would look like:

#!/bin/sh

cd /home/imageserviceagent/ 2>/dev/null > /dev/null

if [ $? -eq 0 ]; then
    /usr/bin/convert game_part.jpg -resize 50% game_part_resized.jpg > /dev/null 2>/dev/null

    if [ $? -eq 0 ]; then
      /bin/echo "Content-type: image/jpeg"
      /bin/echo ""
      /bin/cat /home/imageserviceagent/game_part_resized.jpg
    fi
    exit
fi

Remember the permissions:

root@server261583:/usr/lib/s# chmod a+x shrink

Check that you get the image:

http://yourservice.com/s/shrink

Notive that this is not quite useful yet. Perhaps it's better to resize and change the compression ratio just once and store such image(s) permanently. Resizing the image on every user hit is not efficient.


Getting input parameters via the QUERY_STRING environment variable

Next, we might apply a parameter via the $QUERY_STRING. The question mark is the divider indicating of a potential input string. What follows the question mark is passed to the script. For example, accessing the URL below sends the 'parameter' accessible to the shell script:

http://yourservice.com/s/shrink?parameter

Let's put the resize ratio as an input parameter, so the parameter RATIO gets the value parameter as seen above (and below).

#!/bin/sh

cd /home/imageserviceagent/ 2>/dev/null > /dev/null

if [ $? -eq 0 ]; then
    RATIO=`/bin/echo "$QUERY_STRING"`
    if [ $RATIO -gt 10 ]; then
      if [ $RATIO -lt 101 ]; then
        /usr/bin/convert game_part.jpg -resize $RATIO% game_part_resized.jpg > /dev/null 2>/dev/null

        if [ $? -eq 0 ]; then
          /bin/echo "Content-type: image/jpeg"
          /bin/echo ""
          /bin/cat /home/imageserviceagent/game_part_resized.jpg
        fi

      fi
    fi
    exit
fi

Now you can choose the resize ratio from 11% up to 100%. This test script doesn't care about the error scenarios at all, for it looks a lot easier to understand this way.

http://yourserver.com/s/shrink?90
http://yourserver.com/s/shrink?50
http://yourserver.com/s/shrink?20

Advanced image manipulation for cloud services

Now you should be able to perform different operations for images. Imagemagick is a feature-rich program that may be used for endless use-cases. Discover it via its documentation and feature list. Moreover, any command line program may be run through the scripts as well.