« back to folderblog
folderblogwiki

Max Image Size Hack

I just tried folderblog for the first time tonight, and it seems pretty nifty! One thing I found, after uploading a bunch of pics from my 7MP digicam is that they don't get scaled down :( So I threw together a little hack to get them to automatically scale to a maximum user defined size. The basic idea is that along with the thumbs folder, you have another folder to hold the resized photos.

Obviously you have to create whatever folder you chose to hold the resized images (see below).

Here are the modifications that i made to beta3.16:

fb_settings.php
--------------------
##### AT the end of the file ADD:
$image_maxsize = 660; // Maximum dimensions for a displayed image (in pixels) 
$photo_resize_directory = "photoresize/"; // Directory for resized photos


fb.php
--------------------
##### BEFORE function createThumb ADD:
function createImage($filename,$altname="") { //MODIFIED 
global $photo_resize_directory, $image_directory, $image_maxsize, $file_extension;
$original = imagecreatefromjpeg($filename);
$x = imagesx($original);
$y = imagesy($original);
$scale = $image_maxsize/max($x, $y);
if ($scale > 1.0) $scale = 1.0; // Prevent upscaling
$newx = $x*$scale;
$newy = $y*$scale;
$image_resize = imagecreatetruecolor($newx, $newy);
imagecopyresampled($image_resize, $original, 0, 0, 0, 0, $newx, $newy, $x, $y);
imagejpeg($image_resize, $photo_resize_directory . basename($filename), 95);
if ($altname) {
imagejpeg($image_resize, $photo_resize_directory . $altname, 95);
}
return array($newx, $newy);
}


##### IN if (($page=="main") CHANGE
$size = getimagesize($fbimage[1]);

##### WITH
$size = getimagesize($fbimage[2]); //MODIFIED


##### IN if (($page=="main") REPLACE
"<#fbimageurl#>" => "$fbdir{$fbimage[1]}",

##### WITH
"<#fbimageurl#>" => "$fbdir{$fbimage[2]}", //MODIFIED


##### IN function getDirList REPLACE
global $fbfile, $include_cats, $categories, $photo_directory, $file_extension, $post, $sort;

##### WITH
global $fbfile, $include_cats, $categories, $photo_directory, $photo_resize_directory, $file_extension, $post, $sort; //MODIFIED


##### IN function getDirList REPLACE
foreach($files as $key => $thisfile) { 
$clean[] = array($thisfile, $key);
}

##### WITH
foreach($files as $key => $thisfile) { //MODIFIED THIS LOOP 
$image_resize_file = "$photo_resize_directory{$thisfile}$file_extension";
if ((!file_exists($image_resize_file)) && (function_exists("imagejpeg"))) {
createImage($key);
}
if (file_exists($image_resize_file)) {
$clean[] = array($thisfile, $key, $image_resize_file);
}
else {
$clean[] = array($thisfile, $key, $key);
}
}


DEVNOTES:

In theory, createThumb and createImage are nearly the same, so they should be refactored, but that would require more work for anyone who wants to make these changes. Also, it would be good to have an option to disable this, since it does take up a bit of time and space. Maybe use 0 maxsize to disable or something. Also, I haven't really tested this with categories, so can't say how well it works there.

Another thought, is that since upscaling is prevented, it doesn't make sense to store a non-resized image. Perhaps a placeholder should be used instead (just an empty text file) to indicate no resize occured.

Finally, If you are a dev on folderblog, and you like this hack enough to include it, please send me an email at enki1337{@]gmail[.}com - don't mind if you use it, I'd just be happy to hear it! :)

Edit This Entry:

Please enter the following text: captcha
Home  Index  Recent Changes  folderblog