NEComputer
asked on
Trying to detect thumbnail photos and create thumbnails using php.
I am creating a real estate website and in my listings I am trying to have php detect whether or not a thumbnail of the listing exists and if it doesn't create one. If the origninal image doesn't exist that we want to make a thumbnail of then we just put a generic "no image" picture up. I have code that I created but it doesn't work. I am hoping someone can help fix my code or give me suggestions / code snippets for an already working / developed code that does what I want. The code follows:
while ($search_array = mysql_fetch_array($search_result)){
//Determine what type of property
if($search_array['property_type'] == 'R'){
$property_type = 'Residential';
}
if($search_array['property_type'] == 'F'){
$property_type = 'Farm';
}
//Look for a picture matching the criteria in the thumbnail area. -- Criteria = MLS Number _ 0*
//foreach(glob("images/property_images/thumbs/".$search_array['mls_id']."_0*") as $image_thumb_filename);
$image_thumb_filename = "images/property_images/thumbs/".$search_array['mls_id']."_0.jpg";
//If photo exist then pull reference. If not move on..
if(file_exists($image_thumb_filename)){
$listing_image = $image_thumb_filename;
}
//Look for a picture matching the criteria again this time inside the main picture area.
//foreach(glob("images/property_images/".$search_array['mls_id']."_0*") as $image_filename);
$image_filename = "images/property_images/".$search_array['mls_id']."_0.jpg";
//If a photo exist
if((file_exists($image_filename)) And (file_exists($image_thumb_filename)==False)){
// Set a maximum height and width
$width = 140;
$height = 105;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($image_filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$short_name=substr($image_filename,strrpos($image_filename,'/')+1);
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($image_filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$thumb_image_directory = "images/property_images/thumbs/";
// Output
imagejpeg($image_p, $thumb_image_directory);
$listing_image = $thumb_image_directory.$short_name;
}
if(($image_filename == False) && ($image_thumb_filename == False)){
$listing_image = $thumb_image_directory.'NoPhoto_140x105.png';
}
When you say it "doesn't work" what do you mean? I see some instructions commented out. Please tell us a little more about the symptoms, thanks, ~Ray
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Had everything I needed and i was able to make adjustment to suite my needs. Thanks for the help.