Casual Hacking

Main Menu

Quickie Image Gallery Hack for Friendica

Sample

Here's a cheap and dirty quick hack to put image galleries in your Friendica posts, with an AutoViewer slideshow.
You could probably turn it into a plugin or module, as desired.

Friendica is a decentralized communication / social networking platform. It's Free Software (Expat/MIT) and available for download, so anyone can set up (and tinker with) their own node on the federated network.

Software products used in this hack.

Friendicahttp://www.friendica.com/
AjaXplorerhttp://www.ajaxplorer.info/
AutoViewerhttp://www.simpleviewer.net/autoviewer/
ImageMagickhttp://www.imagemagick.org/
PHPhttp://www.php.net/

 

Download

Code available at github.

git@github.com:creamy/friendika-hack.git

Decide where you are going to store your repository. This directory needs to be publicly available to HTTP requests. Install AjaXplorer into a different directory, which should be secured to allow authorized access only, and configure AjaXplorer to manage the photos in your public photo repository.

Example directory paths:

Photo Root/www/html/photos
Repository/www/html/photos/repository
AjaXplorer/www/html/photos/oo

The web server process uid (sometimes "daemon" or "nobody") should own the repository directory.

You will need to put five files in the "photos" directory.

gallery.xmlBlank template for AutoViewer.
gallery.xml.phpOutputs an XML file compatible with AutoViewer.
wcg_gallery.phpScans a requested directory within the repository, generates thumbnails and outputs preview HTML.
g.phpLoads AutoViewer and tells it to use the requested gallery XML configuration.
og.phpWrapper around wcg_gallery.php, called by Friendica script.

The wrapper script handles "external" requests. Direct calls to wcg_gallery.php can be made "inline" from products installed adjacent to photos directory (such as WordPress, Joomla, Drupal, OpenCart, etc).

Also, you need to make a minor modification to index.php in the Friendica installation.

Near the bottom of index.php, just before this code:

if(file_exists($template))
        require_once($template);
else
        require_once(str_replace($lang . '/', '', $template));

session_write_close();
exit;

You will insert the following code.

//HERE
$wcg_url = 'http://example.com';
$wcg_message = $page['content'];

if (strstr($wcg_message,'[gallery:')) {
	$wcg_m = @explode('[gallery:',$wcg_message);
    $tm=array();
    $wcg_message = array_shift($wcg_m);

	foreach ($wcg_m as $ll) {
		$wcg_b = @explode(']',$ll);
        $wcg_gallery = array_shift($wcg_b);
        $wcg_content = @join('',@file($wcg_url.'/og.php/'.$wcg_gallery));
        $tm[] = $wcg_content.join(']',$wcg_b);
	}
    $wcg_message .= join('',$tm);
    $page['content'] = $wcg_message;
}

This hack allows you to specify a gallery in a Friendica post as follows:

[gallery:name_of_gallery]

Uploading Images

Create a new directory in your photos path. It's best to use a URL friendly name like my_photo_gallery. AutoViewer will take care of resizing the images to fit the screen, however as a rule of thumb I generally resize my images to 800 - 1000 pixels tall (height) and let the "x" (width) float. I believe that 900px is probably the best height for modern displays.

Here is a PHP script which calls ImageMagick convert to resize an image:

<?php
$v=$argv[1];
list($w,$h) = getImageSize($v);
$nh=800;
$hs=$nh/$h;
$nw=ceil($w*$hs);
system ('convert -size '.$nw.'x'.$nh.' -scale '.$nw.'x'.$nh.' -sharpen 0x0.02 -density 120 -quality 90 '.$v.' 800/800-'.$v);
echo $v.' done ('.$nw.'x'.$nh.') '."\n";
?>

You can include the iptc_make_tag() function by Thies C. Arntzen if you want to write IPTC tag info to the resulting images.

ie, after the convert line:

$iptc = array(
    '2#120' => 'Client Image '.$v,
    '2#116' => 'Copyright 2012 Waitman Gobble'
);
$data = '';
foreach($iptc as $tag => $string) {
    $tag = substr($tag, 2);
    $data .= iptc_make_tag(2, $tag, $string);
}
$content = iptcembed($data, '800/800-'.$v);
$fp = fopen('800/800-'.$v, "wb");
fwrite($fp, $content);
fclose($fp);

To batch process all the images in a directory, you can iterate and execute from your favorite shell:

#> mkdir 800
#> for i in *.jpg; do php -q resamp.php $i; done;

Friendica status update/post

The code will replace the gallery spec with the HTML returned by the og.php script, and the gallery will be displayed on the page.

Just include [gallery:name_of_gallery] in your post. (probably best on a seperate line).

Here is an example: https://www.uzimac.com/display/waitman/82

 

Related Information and Documentation

Friendica
http://www.friendica.com/
https://github.com/friendica/friendica/archives/master

ImageMagick
http://www.imagemagick.org/script/index.php
http://www.imagemagick.org/script/command-line-tools.php

PHP - Personal Home Page
http://www.php.net/

Git
http://git-scm.com/
https://github.com/
http://forums.freebsd.org/showthread.php?t=10810

Apache 2.2
http://httpd.apache.org/

 


Copyright 2012 Waitman Gobble · waitman@waitman.net