Category Archives: Gallery2

Gallery Changed to Gallery 3.0 Code.

Now that Gallery 3.0 Final has been released, I have changed my main Gallery to use it. This wasn’t an overnight thing as I have been using the Gallery 3 code in a test environment since it was in beta. Notice I did not say upgrade, but change. This is a completely new code base from Gallery 2. None of the modules or themes from Gallery 2 are compatible. Why the change? Well, the main reason is that working with the code in this version is so much easier. The database schema is also much easier to comprehend and deal with.

Because of this change, I did have to change how I get a random image from the Gallery to display on my blog and main website. It was fairly easy to make the switch. Because the database schema was easier, I was able to just do a direct database connection and MySQL query to get the information I needed. No inclusion of gallery code or use of Curl connections was necessary. A much more efficient process that took an average  of 0.2 seconds off the page load time for my blog!

The other fun part has been locating all the links and image references that needed to be changed. Think I have updated all of them in my blog and main website. For the external links I’ve been modifying the .htaccess file as situations arise. Here are few examples:

   # put redirects for old to new gallery links here
   RewriteBase /
   RewriteRule ^v/MyPics/HomeTheater/([A-Za-z_0-9\)\(\-]*)\.[A-Za-z]*\.html$ http://gallery.markheadrick.com/Home-Theater/$1 [R=301,L]
   RewriteRule ^v/MyPics/([A-Za-z_0-9\)\(\-]*)\.[A-Za-z]*\.html$ http://gallery.markheadrick.com/MiscPics/$1 [R=301,L]
   RewriteRule ^v/Nature/([A-Za-z_0-9\)\(\-]*)\.[A-Za-z]*\.html$ http://gallery.markheadrick.com/Nature/$1 [R=301,L]

The current (default) theme that the Gallery uses is heavy in JavaScript use. I’ll keep my eye out for a suitable theme that doesn’t use as much JavaScript; however, it’s really not that bad. Besides, customizing the themes in this version is much easier and since the final code was just released, it’s probably best to stay with the default theme for now. Actually, I do tend to use the default themes most of the time since they are guaranteed to be the most compatible with the latest core code.

Since this version works with Akismet, I have opened this gallery up for comments for now. I had to turn them off in the other one due to overwhelming amounts of spam at times even with captcha modules installed. So feel free to comment. :)

I guess that’s about it for now. Hope you had a happy 10/10/10! :)

Changed method of displaying random Gallery2 image to bypass DNS performance issues

Yeah, I know this probably will not make much sense to most people other than those dealing with something similar.

In the past I had been using PHP’s CURL functionality to obtain and display a random gallery image on both my main website and blog. The code I used was similar to the following:

$ch = curl_init();
$timeout = 10; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://gallery.markheadrick.com/main.php?g2_view=imageblock.External&g2_blocks=randomImage&g2_linkTarget=_blank&g2_show=title|date|views';);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
if ($file_contents != false) {
   echo $file_contents;
} else {
   echo '
<div class="small">Error, exceeded '.$timeout.' second connection timeout.</div>
 
'."\n";
}

This method worked fine; however, every now and then it would take several seconds to complete causing the webpages to display that much slower. After much investigation from when I started noticing this many months ago, I determined that the delay was being caused by the amount of time it was taking the server to look-up the IP address of gallery.markheadrick.com. I used the following bit of code to test this with:

function getmicrotime()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
} 
 
$port_starttime = getmicrotime();
$fp = @fsockopen('gallery.markheadrick.com', 80, $errno, $errstr, 10);
if (!$fp)
{
    echo "Offline ".number_format((getmicrotime() - $port_starttime), 4)." seconds\n";
}
else
{
    echo "Online ".number_format((getmicrotime() - $port_starttime), 4)." seconds\n";
    fclose($fp);
}

All this does is show the amount of time it takes to make a connection. Normally this should be a fraction of a second (0.0843 seconds for example). Well, on occasion this would take 4, 5 or more seconds to complete. If I used the server’s IP address instead of gallery.markheadrick.com, it would always take a fraction of a second (0.0001 seconds). So, the only difference was the system trying to determine what the IP address of gallery.markheadrick.com was. Similar behavior was displayed when I connected to google.com vs its IP address. In order for CURL to work, I had to supply it with gallery.markheadrick.com as the server has many domains and sub-domains associated with the same IP address.

Over time I’ve submitted numerous support tickets concerning this hostname look-up delay as the problem has surfaced, been fixed, and then resurfaced for whatever reason.

A couple days ago it started happening again and I submitted a new support ticket concerning it which more or less went nowhere and told them to just forget about it and close it.

Since my gallery is on the same server as my website and blog, I thought I should be able to obtain an image directly using PHP and MySQL; however, after looking at Gallery2’s database schema, this was going to be more difficult than I had thought. I then wondered if I could simply include some part of the Gallery code that dealt with the ImageBlock code that displays a random image. Then, I wondered if I could just include Gallery’s main PHP file and somehow feed it the variable values it needed to work. The following is what I eventually ended up with:

$_GET['g2_view'] = 'imageblock.External';
$_GET['g2_blocks'] = 'randomImage';
$_GET['g2_show'] = 'title|date|views';
$_GET['g2_linkTarget'] = '_blank';
ob_start();
include_once ($_SERVER['DOCUMENT_ROOT'].'/gallery/main.php');
$gallery_ob = ob_get_contents(); // Gets the contents of the output buffer
ob_end_clean(); // Ends capture and cleans the buffer so that it will not be displayed
$gallery_ob = str_replace('http://www.markheadrick.com/d/','http://gallery.markheadrick.com/d/',$gallery_ob);
$gallery_ob = str_replace('http://www.markheadrick.com/v/','http://gallery.markheadrick.com/v/',$gallery_ob);
echo $gallery_ob;

Hallelujah!! It worked! :) During my search for answers the following two articles/threads helped me out the most: I want “include” to return a string instead of writing directly into the page. and [SOLVED] Get arguments in include function. Because I was including the code under a different sub-domain, it was using the wrong URL for the image and link. This is why I needed to add the two str_replace functions. On my blog, it was trying to load the image from blog.markheadrick.com instead of gallery.markheadrick.com. I also had to increase the amount of memory PHP could use for scripts on my blog from 32M to 64M.

Gallery has been upgraded to version 2.2.4

My photo gallery has been upgraded to Gallery2, version 2.2.4 which includes a number of security patches and fixes.  The upgrade went smooth and without any hiccups.  I used the 2.2.3 to 2.2.4 upgrade file.  Speaking of the gallery, I have added my 2007 Christmas pictures for any family members to view in the Family Albums section (this is a protected area of my gallery which requires a user name and password to view).