To use it, you must create a new folder in your blog directory (e.g. "pix"), within which you'll place folders containing pictures for a specific entry. Each folder should end with the ID of the entry and can be prefixed by a specific word (e.g. "post134").
Please edit the php file with your site URL.
With the sample config below, each set of pictures should be in a folder named "postXXX" (where XXX is your entry's ID) and that folder should be in a folder called "pix" located in your blog's root directory (paths are relative to the blog root). Make sure your folders are writable by everybody (chmod 666), as the script needs to be able to create the thumbnails.
If you do not want to prefix folder names, simply end the url and names below by a "/". In that case, each folder must have the ID of its corresponding entry as a name (e.g. "99" or "134").
SECRET FEATURE: you can batch your whole blog (instead of the entry you are editing) by navigating to your blog index page URL appended by "?glue_all=true". E.g: http://mydomain.com/blog/index.php?glue_all=true
Version: 0.1
Author: dr Dave
Author URI: http://www.unknowngenius.com/blog
License: GNU GPL (refer to Wordpress license for details)
*/
////// CONFIG VARS:
define (kPixDir, "pix/post"); // should be the relative path to pix folder + prefix (needs to be chown'ed "nobody" or chmod'ed 777)
define (kURL, get_bloginfo('url') . "/" . kPixDir); // URL to pix folder + prefix
////// DO NOT EDIT BELOW HERE
function createThumbnail ($myFile, $replace, $maxDim)
{
if (($myFile == "") || !is_file($myFile))
{
echo ("cannot work with the file provided: $myFile");
return;
}
$imageInfo = getimagesize($myFile);
if (!$imageInfo)
{
echo "$myFile doesn't seem to be a valid pict";
return;
}
$src_width = $imageInfo[0];
$src_height = $imageInfo[1];
if (max($src_width, $src_height) < 300) // then we do not need thumbnails
return false;
if ($maxDim < 10 || $maxDim > 500)
{
echo "the max dimension must be between 10 and 500, you entered $maxDim";
return;
}
$myDir = dirname($myFile) . "/thumbnails";
if (! is_dir($myDir))
{
mkdir ($myDir);
chmod ($myDir, 0777);
}
$destFile = "$myDir/" . basename($myFile);
if (is_file($destFile))
{
$imageInfo_thumb = getimagesize($destFile);
if ($imageInfo_thumb)
{
$width = $imageInfo_thumb[0];
$height = $imageInfo_thumb[1];
}
if ($replace == "yes")
{
if (unlink($destFile))
$replaced = "(replaced)";
else
{
return array("h" => $height, "w" => $width);
}
}
else
{
return array("h" => $height, "w" => $width);
}
}
else
$replaced = "";
if ($src_width > $src_height)
{
$height = round ($src_height * $maxDim / $src_width);
$width = $maxDim;
}
else
{
$width = round ($src_width * $maxDim / $src_height);
$height = $maxDim;
}
$type = exif_imagetype($myFile);
if ($type == IMAGETYPE_JPEG)
$src_img = imagecreatefromjpeg ($myFile);
elseif ($type == IMAGETYPE_PNG)
$src_img = imagecreatefrompng ($myFile);
if (!$src_img)
{
echo "error creating thumbnail: could not load file $myFile
";
}
else
{
if ($width >= $src_width) // do not change if scale >= 1
{
$width = $src_width;
$height = $src_height;
imagejpeg($src_img, $destFile);
}
else
{
$dst_img = imagecreatetruecolor($width,$height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $src_width, $src_height);
if ($type == IMAGETYPE_JPEG)
imagejpeg($dst_img, $destFile);
elseif ($type == IMAGETYPE_PNG)
imagepng($dst_img, $destFile);
imagedestroy($dst_img);
}
imagedestroy($src_img);
chmod ($destFile, 0777);
}
return array("h" => $height, "w" => $width);
}
function listFiles($dir , $type)
{
if (strlen($type) == 0)
$type = "all";
$x = 0;
if(! is_dir($dir))
{
return $result;
}
$thisdir = dir($dir);
while($entry=$thisdir->read())
{
if(($entry!='.')&&($entry!='..'))
{
if ($type == "all")
{
$result[$x] = $entry;
$x++;
next;
}
$isFile = is_file("$dir$entry");
$isDir = is_dir("$dir$entry");
if (($type == "files") && ($isFile))
{
$result[$x] = $entry;
$x++;
next;
}
if (($type == "dir") && ($isDir))
{
$result[$x] = $entry;
$x++;
next;
}
$temp = explode(".", $entry);
if (($type == "noext") && (strlen($temp[count($temp) -1]) == 0))
{
$result[$x] = $entry;
$x++;
next;
}
if (($isFile) && (strtolower($type) == strtolower($temp[count($temp) - 1])))
{
$result[$x] = $entry;
$x++;
next;
}
}
}
return $result;
}
function glue_all_pictures ()
{
global $wpdb, $tableposts;
if (!isset($tableposts))
$tableposts=$wpdb->posts;
$ids = $wpdb->get_col("SELECT `ID` FROM `$tableposts` WHERE 1");
foreach($ids as $id)
{
glue_pictures($id);
}
}
function glue_pictures($post_ID)
{
global $wpdb, $tableposts;
if (!isset($tableposts))
$tableposts=$wpdb->posts;
$lim_start = "";
$lim_stop = "";
$pict_table = "";
$modified = false;
if (isset($_REQUEST['glue_all']))
$pixDir = kPixDir . $post_ID;
else
$pixDir = "../" . kPixDir . $post_ID;
$absoluteURL = kURL . $post_ID . "/";
$postdata = $wpdb->get_var("SELECT `post_content` FROM `$tableposts` WHERE `ID` = ". $post_ID);
if (($start = strpos($postdata, $lim_start)) !== false )
{
if (($stop = strpos($postdata, $lim_stop, $start)) !== false )
{
$modified = true;
$postdata = substr_replace($postdata, "", $start, ($stop - $start + strlen($lim_stop)));
}
}
if (($stop || !$modified) && is_dir($pixDir))
{
if (! isset($tsize))
$tsize = 200;
if (! isset($nCol))
$nCol = 3;
$fileList = array_merge(listFiles ("$pixDir/", "jpg"), listFiles ("$pixDir/", "png"));
if (count ($fileList) > 0)
{
$modified = true;
if (count ($fileList) == 1)
{
if ($size_array = createThumbnail("$pixDir/". $fileList[0] ."", "no", $tsize))
{
$pict_table = $lim_start . "