Performance hacks

This is a page that details the variety of ZenPhoto performance “modifications” or “hacks” that can be done to Zenphoto’s core code and functions that extend the capabilities of ZenPhoto. Please bear in mind that there may be little or no support for these hacks, so use them at your own risk. If you have questions about any hacks, search the ZenphotoForums for help, or start a new topic there. ZenPhoto has a very helpful community, so don’t be shy!

These hacks are not officially supported by the ZenPhoto development team.

  1. HTTP Caching
  2. 1a. Client side optimization
  3. 1b. Server side optimization
  4. 2a. Reduce load on the DB server
  5. 2b Bypassing directory traversal

HTTP Caching

Developed by tummblr

View the following links for details.

HTTP cache control

HTTP Static cache control


Client side optimization

Developed by vikaskbh

AS per yahoo! optimization guidelines http://developer.yahoo.com/yslow/
for making Zenphoto faster at client-side, here are some hacks.

FIRST: Edit .htaccsss file and add the following lines.
<ifmodule mod_expires.c><filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>FileETag None
That will make your image, javascript,css files cache lifetime 1 year+ and Removes Etags, If Apache WebServer?has attached any. So that webpage header size can be reduced.

SECOND: If your web server is supporting gzip compression, edit index.php and add following line at Top of the page.
header("Vary: Accept-Encoding");if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’))
ob_start(”ob_gzhandler”);
else ob_start();

So, Web server will deliver entire webpage as gzip compression format to the browser. So, network bandwidth can be reduced.

THIRD: Convert your all JavaScript?, CSS files into PHP code (Make simple echo statement) and follow SECOND step for them. Finally, load them in templates. That will make your JavaScript?, css gzip-compressed & faster deliverable.
SampleJavaScript.js.php
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'],
‘gzip’))
ob_start(”ob_gzhandler”);
else ob_start();
header(”content-type: application/x-javascript; charset: UTF-8″);
$gd = “Expires: Monday, January 18, 2038 12:44:06 AM”;
header($gd);
zen.css.php
<?phpif (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’))
ob_start(”ob_gzhandler”);
else ob_start();
header(”Content-type: text/css; charset: UTF-8″);
$gd = “Expires: Sun, 1 Jan 2017 05:00:00 GMT”;
header($gd);
?>

image.php

<link rel=”stylesheet” href=”/themes/sterile/zen.css.php”></link>

try to put javascript before </body>
image.php

<script src=”/themes/sterile/SampleJavaScript.js.php”></script>

So, The loading of web-page won’t blocked untill javascript is loaded.

You can check the performance of the site http://actress.bollysite.com and test It with Firefox firebug-Yslow plug-in.


Server side optimization

Developed by sagasw

Database result cache

As we know, the database will use many resource and time to query the information of Image. If we want to save time, we could do optimization with cache the result.
What I use is two functions, you could use other PHP cache library too. The code I use is version 1.13.
!! Please note: because I don’t open the comment function, so it may have problem with comment.

You could go to site http://henku.info to see the cache result.

1. Edit the functions-db.php under zp-core directory.

2. Search function query_single_row, change it to:
function query_single_row($sql){
if ($cache = get_cache($sql))
return $cache; $result = query($sql);
if ($result) {
$singlerow = mysql_fetch_assoc($result);
store_cache($sql, $singlerow);
return $singlerow;
} else {
return false;
}
}

3. Search function query_full_array, change it to:
function query_full_array($sql){
if ($cache = get_cache($sql))
return $cache; $result = query($sql);
if ($result) {
$allrows = array();
while ($row = mysql_fetch_assoc($result))
$allrows[] = $row;
store_cache($sql, $allrows);
return $allrows;
} else {
return false;
}
}

4. Last thing, add two functions in functions-db.php. You could change “cachedirectory” to your cache directory, and change $cache_time_out to the expire time you want. 30* 24 * 3600 is 30 days.
function store_cache($query, $result_cache){
if (preg_match("/^(insert|delete|update|replace)\s+/i",$query))
return;
if (stristr($query, "ORDER BY RAND"))
return; $cache_file = 'cachedirectory/'. md5($query);
error_log (serialize($result_cache), 3, $cache_file);
}


function get_cache($query)
{
$cache_time_out = 30* 24 * 3600;
if (preg_match("/^(insert|delete|update|replace)\s+/i",$query))
return;
if (stristr($query, "ORDER BY RAND"))
return;
$cache_file = 'cachedirectory/'. md5($query);
if ( file_exists($cache_file) )
{
if ( (time() - filemtime($cache_file)) > $cache_time_out )
{
unlink($cache_file);
}
else
{
$result_cache = unserialize(file_get_contents($cache_file));
return $result_cache;
}
}
}


2a. Reduce load on the DB server

Developed by invarbrass

This is based on the caching technique described on http://www.zenphoto.org/2008/01/hacks/ We’ll use eAccelerator shm cache instead of storing the query results on disk.
1. Edit the functions-db.php under zp-core directory.
2. Search function query_single_row, change it to:
function query_single_row($sql, $noerrmsg=false) {
if ($cache = get_cache($sql))
return $cache;
$result = query($sql, $noerrmsg);
if ($result) {
$data = mysql_fetch_assoc($result);
store_cache($sql, $data);
return $data;
}
else {
return false;
}
}

3. Search function query_full_array, change it to:
function query_full_array($sql, $noerrmsg = false) {
if ($cache = get_cache($sql))
return $cache;
$result = query($sql, $noerrmsg);
if ($result) {
$allrows = array();
while ($row = mysql_fetch_assoc($result))
$allrows[] = $row;
store_cache($sql, $allrows);
return $allrows;
}
else {
return false;
}
}

4. Last thing, add two functions in functions-db.php. You could change “cachedirectory” to your cache directory, and change $cache_time_out to the expire time you want.
function store_cache($query, $result_cache) {
$cache_time_out = 600;
if (preg_match("/^(insert|delete|update|replace)\s+/i",$query))
return;
if (stristr($query, "ORDER BY RAND"))
return;
$key = 'sql_' . md5($query);
$val = serialize($result_cache);
eaccelerator_put($key, $val, $cache_time_out);
}


function get_cache($query) {
if (preg_match("/^(insert|delete|update|replace)\s+/i",$query))
return;
if (stristr($query, "ORDER BY RAND"))
return;
$key = 'sql_' . md5($query);
$val = eaccelerator_get($key);
if ( $val != NULL )
return unserialize($val);
}

2b. Bypassing directory traversal

Developed by invarbrass
ZP scans the albums directory every time the script is executed. Since disk operations are expensive, we can try to limit disk-based I/O to make it load faster. We’ll be using eAccelerator to cache our stuff, but you can easily adopt this code for APC, XCache, memcached or even a disk-file.
1. Open class-album.php in zp-core directory.
2. Put the following code at the beginning of the script:
function store_filenames_cache($dir, $has_dir, $pic_cache, $vid_cache) {
$cache_time_out = 600;
if ($has_dir) {
$pic_key = 'dp_' . md5($dir);
$vid_key = 'dv_' . md5($dir);
}
else {
$pic_key = 'fp_' . md5($dir);
$vid_key = 'fv_' . md5($dir);
}
$pic_val = serialize($pic_cache);
$vid_val = serialize($vid_cache);
eaccelerator_put($pic_key, $pic_val, $cache_time_out);
eaccelerator_put($vid_key, $vid_val, $cache_time_out);
}
function get_filenames_cache($dir, $has_dir, &$pic_cache, &$vid_cache) {
if ($has_dir) {
$pic_key = 'dp_' . md5($dir);
$vid_key = 'dv_' . md5($dir);
}
else {
$pic_key = 'fp_' . md5($dir);
$vid_key = 'fv_' . md5($dir);
}
$pic_cache = eaccelerator_get($pic_key);
$vid_cache = eaccelerator_get($vid_key);
if ( $pic_cache != NULL ) {
$pic_cache = unserialize($pic_cache);
}
else {
return FALSE;
}
if ( $vid_cache != NULL ) {
$vid_cache = unserialize($vid_cache);
}
else {
return FALSE;
}
return TRUE;
}

3. Locate the loadFileNames() function, and replace it with this:
function loadFileNames($dirs=false) {
if ($this->isDynamic()) { // there are no 'real' files
return array();
}
$albumdir = getAlbumFolder() . $this->name . "/";
if (!is_dir($albumdir) || !is_readable($albumdir)) {
$msg = gettext("Error: The 'albums' directory")." (" . $this->albumdir . ") ";
if (!is_dir($this->albumdir)) {
$msg .= gettext("cannot be found.");
} else {
$msg .= gettext("is not readable.");
}
die($msg);
}
//masroore - cache these results
if (get_filenames_cache($albumdir, $dirs, $files, $videos) === FALSE) {
$dir = opendir($albumdir);
$files = array();
$videos = array();
while (false !== ($file = readdir($dir))) {
if ($dirs && (is_dir($albumdir.$file) && (substr($file, 0, 1) != '.') || hasDyanmicAlbumSuffix($file))) {
$files[] = $file;
} else if (!$dirs && is_file($albumdir.$file)) {
if (is_valid_video($file)) {
$files[] = $file;
$videos[] = $file;
} else if (is_valid_image($file)) {
$files[] = $file;
}
}
}
closedir($dir);
store_filenames_cache($albumdir, $dirs, $files, $videos);
}
if (count($videos) > 0) {
$video_thumbs = array();
foreach($videos as $video) {
$video_root = substr($video, 0, strrpos($video,”.”));
foreach($files as $image) {
$image_root = substr($image, 0, strrpos($image,”.”));
if ($image_root == $video_root && $image != $video) {
$video_thumbs[] = $image;
}
}
}
$files = array_diff($files, $video_thumbs);
}
return $files;
}