Hello PointSiters,
I use this script to pull across images and reszie them. However I think it might me a bit inefficient.
Is this a job for Perl.
<html>
<head>
<meta http-equiv="refresh" content="10">
</head>
</html>
<body>
<?
include "connect.inc.php";
include "config.inc.php";
// How many left
$query="SELECT property.property_id, property_photo_id, property_photo_url from property, property_photo WHERE
property.property_id = property_photo.property_id
AND property_photo_processed = '0'";
$result=mysql_query($query);
$rows = mysql_num_rows($result);
echo $rows.' photos left to process <br />';
$query="SELECT property.property_id, property_photo_id, property_photo_url from property, property_photo WHERE
property.property_id = property_photo.property_id
AND property_photo_processed = '0' LIMIT 4";
$result=mysql_query($query);
while ($array=mysql_fetch_assoc($result)) {
$property_photo_id = $array['property_photo_id'];
$property_id = $array['property_id'];
$property_photo_url = $array['property_photo_url'];
echo "property photo id is $property_photo_id <br>";
//if folder doesn't exist create
if (file_exists("../property_pics/$property_id") && is_dir("../property_pics/$property_id")) {
print '<br>'.$property_id . ' is a directory!';
}
else {
print '<br>'.$property_id . ' is not directory Creating';
mkdir("../property_pics/$property_id",0777);
mkdir("../property_pics/$property_id/700",0777);
mkdir("../property_pics/$property_id/580",0777);
mkdir("../property_pics/$property_id/400",0777);
mkdir("../property_pics/$property_id/100",0777);
chmod("../property_pics/$property_id", 0777);
chmod("../property_pics/$property_id/700", 0777);
chmod("../property_pics/$property_id/580", 0777);
chmod("../property_pics/$property_id/400", 0777);
chmod("../property_pics/$property_id/100", 0777);
}
//write original image
$content = file_get_contents($property_photo_url);
$dir = dirname($_SERVER['SCRIPT_FILENAME']);
$filename = $property_id.'-'.$property_photo_id.'.jpg';
$file = $dir.'/../property_pics/'.$property_id.'/'.$filename;
$fp = fopen($dir.'/../property_pics/'.$property_id.'/'.$property_id.'-'.$property_photo_id.'.jpg', 'w');
fwrite($fp, $content);
fclose($fp);
$file_type = exif_imagetype($file);
if ($file_type != IMAGETYPE_JPEG) {
echo "The picture is not a jpeg <br /> it is a $file_type";
}
else {
echo 'The picture is a jpeg <br />';
}
echo "<img src=\\"$site_url/property_pics/$property_id/$property_id-$property_photo_id.jpg\\"><br>";
// resize picture
//Saves 700px version
$save = "../property_pics/$property_id/700/$property_id-$property_photo_id.jpg";
//$file = "pics/$property_id/".$imagepath; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 700;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
echo "700px<img src=\\"$site_url/property_pics/$property_id/700/$property_id-$property_photo_id.jpg\\"><br />";
//Saves 580px version
$save = "../property_pics/$property_id/580/$property_id-$property_photo_id.jpg";
//$file = "pics/$property_id/".$imagepath; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 580;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
echo "580px<img src=\\"$site_url/property_pics/$property_id/580/$property_id-$property_photo_id.jpg\\"><br />";
//Saves 400px version
$save = "../property_pics/$property_id/400/$property_id-$property_photo_id.jpg";
//$file = "pics/$property_id/".$imagepath; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 400;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
echo "400px<img src=\\"$site_url/property_pics/$property_id/400/$property_id-$property_photo_id.jpg\\"><br />";
//Saves 100px version
$save = "../property_pics/$property_id/100/$property_id-$property_photo_id.jpg";
list($width, $height) = getimagesize($file) ;
$modheight = 100;
$diff = $height / $modheight;
$modwidth = $width / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
echo "100px<img src=\\"$site_url/property_pics/$property_id/100/$property_id-$property_photo_id.jpg\\"><br />";
$property_photo_filename = "$property_id-$property_photo_id.jpg";
$update_query = "UPDATE property_photo SET property_photo_filename = '$property_photo_filename', property_photo_processed = '1' WHERE property_photo_id = '$property_photo_id'";
echo "update query is $update_query<br>";
mysql_query($update_query);
//change directory back
chmod("../property_pics/$property_id", 0755);
}
?>
</body>
</html>