• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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';
	}

Open in new window

0
NEComputer
Asked:
NEComputer
1 Solution
 
Ray PaseurCommented:
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
0
 
drowning_in_the_howCommented:
Try this out. If you are still having issues, then try changing the paths to absolute paths from the root of the server instead of relative paths.
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;
	} else {
		$listing_image = '';
	}
	
	//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)) && (empty($listing_image))) {
		// 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';
	}

Open in new window

0
 
NEComputerAuthor Commented:
Had everything I needed and i was able to make adjustment to suite my needs. Thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now