This one is tough. My PHP script slowly eats away at the memory until it finally crashes because it has used too much memory. I have spent hours looking over the code and I cant figure it out. Does anyone see where I could be leaking memory? Here are all of the functions that might be causing the problems:
retsGetImages();
function retsGetImages()
{
global $retsURLs, $location_id, $rets_system, $datetime, $last_prop_update, $last_img_update, $datetime;
// loop through all of the properties
echo "-Getting MLS Numbers";
$sql = "SELECT id, mls_num, xref_status_id, num_photos FROM properties_".$location_id." WHERE xref_mls_id = $location_id AND picture_rec_time >= '$last_img_update' AND num_photos > 0";
$result = db_exec($sql);
$rows = db_numrows($result);
echo ", Downloading and Processing Images for $rows Properties";
$line_number = 0;
for ($i = 0; $i < $rows; $i++)
{
$data = db_fetch_array($result,$i);
$line_number++;
if (is_int($line_number / 10)) echo ".";
retsDeletePhotos($data["id"]);
// format the search for photos, we don't want all photos for every ID
if ($data["xref_status_id"] != 3 && $data["xref_status_id"] != 4)
{
for ($j = 1; $j <= $data["num_photos"]; $j++)
{
$search = "Resource=Property&Type=Photo&ID=".$data["mls_num"].":".$j;
//die($search);
//echo $search."\n";
retsSaveImage($data["mls_num"],$j,retsConnect($retsURLs["GetObject"],$search));
}
}
else
{
// sold or inactive, just get one photo
$search = "Resource=Property&Type=Photo&ID=".$data["mls_num"].":1";
//die($search);
//echo $search."\n";
retsSaveImage($data["mls_num"],1,retsConnect($retsURLs["GetObject"],$search));
}
}
// update the db with latest update date
$sql = "UPDATE mls_companies SET images_updated_date = '$datetime' WHERE id = $location_id";
db_exec($sql);
// clean up larger variables
db_freeresult($result);
echo "\n";
}
function retsSaveImage($mls_num,$photo_num,$binary_data)
{
global $web_root,$location_id;
if (strlen($binary_data) > 200)
{
$image_name = $mls_num."_".$photo_num.".jpg";
$sql = "INSERT INTO all_images_".$location_id."(xref_mlsco_id,name,mls_num) VALUES($location_id,'".$image_name."',".$mls_num.")";
db_exec($sql);
$img_dir = $web_root."/images/".$location_id."/".$image_name;
$img_dir_thumb = $web_root."/images/".$location_id."/tn/".$image_name;
$img_dir_temp = $web_root."/images_temp/".$location_id."/".$image_name;
//file_put_contents($img_dir_temp, $binary_data);
$fh=fopen($img_dir_temp,"w");
fwrite($fh,$binary_data);
fclose($fh);
thumb_jpeg($img_dir_temp,$img_dir_thumb,120,'');
system("mv $img_dir_temp $img_dir");
}
}
// delete all photos associated with a certain MLS number
function retsDeletePhotos($id)
{
global $web_root,$location_id;
$img_dir = $web_root."/images/".$location_id."/";
$img_dir_thumb = $web_root."/images/".$location_id."/tn/";
$sql = "SELECT id, name
FROM all_images_".$location_id." WHERE mls_num = $id AND xref_mlsco_id = $location_id";
$result = db_exec($sql);
$rows = db_numrows($result);
for ($i = 0; $i < $rows; $i++)
{
$data = db_fetch_array($result,$i);
unlink($img_dir.$data["name"]);
unlink($img_dir_thumb.$data["name"]);
}
db_freeresult($result);
db_exec("DELETE FROM all_images_".$location_id." WHERE mls_num = $id AND xref_mlsco_id = $location_id");
}
// resizes images
function thumb_jpeg($source_path,$destination_path,$new_width,$new_height)
{
// only do the file if it is a JPEG
if (strtolower(substr($source_path,(strlen($source_path)-4),4)) == '.jpg')
{
$tmpimg = tempnam($source_path, "MKPH");
system("djpeg $source_path >$tmpimg");
if ($new_width && !$new_height)
{
system("pnmscale -x $new_width $tmpimg | cjpeg -smoo 10 -qual 75 >$destination_path");
}
elseif (!$new_width && $new_height)
{
system("pnmscale -y $new_height $tmpimg | cjpeg -smoo 10 -qual 75 >$destination_path");
}
else
{
system("pnmscale -xy $new_width $new_height $tmpimg | cjpeg -smoo 10 -qual 75 >$destination_path");
}
unlink($tmpimg);
return true;
}
else
{
return false;
}
}