Geotagging from image EXIF data in WordPress

Displaying imagine location in wordpress using their gps coords proved to be not as easy as I initially thought, but it is something I wanted to get done from some time now, so I decided to get down on it. I have chosen to use geotag plugin for its capability to read exif data from images spread through a post and nicely locate them on a google map, among other stuff.

Unfortunately, after uploading few images I discovered that the cool functionality of searching photos with gps exif data and displaying their location on map was not working. That was because the images displayed don`t have any exif data stored in the file, as wordpress automatically  stripes it down on image upload, but not before data is read so it can be stored serialized in postmeta table from db. Instead of altering wordpress core files to modify this behavior, I preferred to find a different approach and try using the meta info already stored into database. But the problem is that wp save only some tags from exif, and GPS information in not among them, so first step is to add the following filter to the wp_read_image_metadata hook:

add_filter('wp_read_image_metadata', 'saveGeoExif','',3);

function saveGeoExif($meta,$file) {
    $exif = @exif_read_data($file);

    if (isset($exif['GPSLatitude'])){
        $meta['GPSLatitude'] = $exif['GPSLatitude'];
    }
    if (isset($exif['GPSLatitudeRef'])){
        $meta['GPSLatitudeRef'] = trim($exif['GPSLatitudeRef']);
    }
    if (isset($exif['GPSLongitude'])){
        $meta['GPSLongitude'] = $exif['GPSLongitude'] ;
    }
    if (isset($exif['GPSLongitudeRef'])){
        $meta['GPSLongitudeRef'] = trim($exif['GPSLongitudeRef']);
    }

    if (isset($exif['GPSAltitudeRef'])){
        $meta['GPSAltitudeRef'] = trim($exif['GPSLongitudeRef']);
    }
    if (isset($exif['GPSAltitude'])){
        $meta['GPSAltitude'] = trim($exif['GPSLongitudeRef']);
    }
    if (isset($exif['GPSTimeStamp'])){
        $meta['GPSTimeStamp'] = $exif['GPSLongitudeRef'];
    }
    if (isset($exif['GPSDOP'])){
        $meta['GPSDOP'] = $exif['GPSLongitudeRef'];
    }
    if (isset($exif['GPSImgDirectionRef'])){
        $meta['GPSImgDirectionRef'] = trim($exif['GPSLongitudeRef']);
    }
    if (isset($exif['GPSImgDirection'])){
        $meta['GPSImgDirection'] = trim($exif['GPSLongitudeRef']);
    }

	return $meta;
}

This will save all GPS information into database upon image upload.

Next we need to search all the photo attachments belonging to a post, calculate their coordinates and extract image url, and then pass them to geotag plugin.

add_filter("the_content", 'getGeodataFromMeta');

function fractionResult($fraction){
    list($sup, $sub) = explode('/', $fraction);
    if(floatval($sub) != 0){
        return $sup / $sub;
    }else{
        return 0;
    }
}

function getCoord($fractional, $ref){
    $coord = 0;
    if(is_array($fractional)){
        $coord = fractionResult($fractional[0]) + fractionResult($fractional[1]) / 60 + fractionResult($fractional[2]) / 3600;
    }
    if($ref == "S" || $ref == "W"){
        $coord = -$coord;
    }
    return $coord;
}

function getGeodataFromMeta($content){
    global $photoMetaCoords;
    $postId = get_the_ID();
    $attachments = get_posts('post_type=attachment&post_mime_type=image&post_parent=' . $postId);

    foreach ($attachments as $attachment){
        $meta = wp_get_attachment_metadata($attachment->ID);
        if(isset($meta['image_meta']['GPSLatitude'])){
            $lat = getCoord($meta['image_meta']['GPSLatitude'], $meta['image_meta']['GPSLatitudeRef']);
            $long = getCoord($meta['image_meta']['GPSLongitude'], $meta['image_meta']['GPSLongitudeRef']);

            $imageUrl = wp_get_attachment_image_src($attachment->ID, array(133, 133));
            $photo = array(
                'lat' => $lat,
                'lon' => $long,
                'uri' => $imageUrl[0]
            );

            $photoMetaCoords[] = $photo;
        }
    }
    return $content;
}

Last thing we need to do in order to get all this to work is to alter a bit geotag plugin, so it will also load the images from $photoMetaCoords array. In function getGeotagsFromPhotos() add the following line before if (empty($geotags)) {return null;} else… :

$geotags = array_merge($geotags, $photoMetaCoords);
$photoMetaCoords = array();

Also at the begining of getGeotagsFromPhotos() function change:

global $post;

to

global $post, $photoMetaCoords;

That`s it!

All the code presented here should be added in functions.php or in geotag.php (except the last line which should always go in geotag.php, of course :) ).

turda

flowers

flowers

pfff

Iphone finger draw – Brushes and MyPaint2

These are my first attempts to draw something on iphone. Neither one is finished, but is enough to see the possibilities of the two applications.

My Paint 2

Brushes

Is nice to draw with your finger, but it takes a lot of time zooming, choosing color, opacity etc. And a big downside if you have think finger :) . First drawing is made in Brushes, said to be the best drawing application for iphone. Two big points for having opacity and multi-layer support – up to 4 layers (not much, but enough if you manage your things right) that can be rearranged, deleted, merged, and even copied between paintings. Also, fast access to controls as you draw by just taping the screen on any spot saves some amount of time. For color choosing it has the friendly well known color wheel and an opacity slider, wich makes it pretty straight forward, but I find it a bit difficult fine tuning the opacity at low values.

Second drawing (ergh…) is made using MyPaint2, the first iphone painting app I ever tried. Also a decent painting application, but lacks of layer and opacity support. It has a brush softness slider wich slided to the low end point makes the brush transparent but is not what you`d expect, and certainly not a true opacity option. A very interesting feature is the possibility to upload your masterpieces directly to their website, where drawings are rated and commented. I tell you I found some amazing drawings there, made me feel so talent-less. Another good thing is the ability to save some colors, if you frequently need them, but the color choosing tool is… uhm… I don`t like it. Overall, a decent painting application, but when I found out that there is no “redo” button,  I given up.

So, if you ever fell like finger drawing on you Itoy, my advice is to go with Brushes, even though is a bit more expensive, both applications are very cheap (3,99 euro Brushes, 1,59 euro MyPaint) .