here's something I threw together about a week ago to resize a bunch of high resolution photos, not the best script but it got the job done,
as you see you have to write a function that scans the directory, and have that function call itself (beware of infinite recursion)
Code: Select all
<?php
ob_implicit_flush();
set_time_limit(0);
error_reporting(E_ALL);
$location = glob("/path/to/images/high_res/*/");
?>
<html>
<head>
<title>
Generating medium rez's
</title>
<script language="javascript" type="text/javascript">
function writit(id, text) {
if (document.getElementById)
{
x = document.getElementById(id);
x.innerHTML = '';
x.innerHTML = text;
}
else if (document.all)
{
x = document.all[id];
x.innerHTML = text;
}
else if (document.layers)
{
x = document.layers[id];
text2 = '<P CLASS="testclass">' + text + '</P>;';
x.document.open();
x.document.write(text2);
x.document.close();
}
}
</script>
</head>
<body>
<div id="message">
Starting...
</div>
<?php
foreach ($location as $loc) {
$loc = str_replace("/path/to/images/high_res/", NULL, $loc);
echo $loc."<br />";
}
echo ("<br />");
foreach ($location as $loc) {
$loc = str_replace("/path/to/images/high_res/", NULL, $loc);
scanlocation($loc);
}
function writit($msg) {
echo ("<script language=\"javascript\" type=\"text/javascript\">writit('message', '".$msg."')</script>");
}
function scanlocation($location) {
writit('Scanning');
if ($location==NULL) exit();
$imagedir='/var/www/images/high_res/'.$location;
$thumbnaildir='/var/www/images/med_res/'.$location;
if (!file_exists($thumbnaildir)) {
mkdir($thumbnaildir);
chmod($thumbnaildir, 0777);
}
$x=1;
$images = glob($imagedir . "{*.jpg,*.JPG}", GLOB_BRACE);
foreach ($images as $k => $file) {
$images[$k]='/'.str_replace($imagedir, NULL, $file);
}
$thumbs = glob($thumbnaildir . "{*.jpg,*.JPG}", GLOB_BRACE);
foreach ($thumbs as $k => $file) {
$thumbs[$k]='/'.str_replace($thumbnaildir, NULL, $file);
}
$images = array_diff($images, $thumbs);
$total = count($images);
writit($total.' images found, starting');
foreach($images as $file) {
$msg = "<b>$x of $total for $location [";
$percent = round(($x/$total)*100, 1);
$msg .= $percent;
$msg .= "%]";
writit($msg);
if (!$data=@file_get_contents($imagedir.$file)) {
echo ('failed on '.$file.'<br />');
continue;
}
list($width_orig, $height_orig, $type, $attr)=getimagesize($imagedir.$file);
$width=768;
$height=512;
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$filename = $thumbnaildir.$file;
if (!file_exists($filename)) {
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromstring($data);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$image = imagecreatefrompng('/var/www/html/dev/_bin/watermark.png');
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, 768, 510);
imagejpeg($image_p, $filename, 100);
}
$x++;
}
echo ("<font color='green'>$location is done!!!</font><br />");
}
?>
</body>
</html>