Solved

Thumbnail creation problem

Posted on 2007-03-22
3
645 Views
Last Modified: 2008-01-09
Hi guys,

I'm using this code to create a thumbnail from an uploaded picture:

                  $destination_file = "../".$thumbsFolder."thumbnail_".$file_name[count($file_name) - 1];
                    
                  // Create thumbnail of the painting
                  list($img_width,$img_height) = getimagesize($source_file); // Get the original dimensions
                  $aspect_ratio = $img_width / $img_height;
                    
                  if ( ($img_width > $max_dimension) || ($img_height > $max_dimension) ) // If either dimension is too big...
                  {
                  if ( $img_width > $img_height ) // For wide images...
                  {
                        $new_width = $max_dimension;
                        $new_height = $new_width / $aspect_ratio;
                  }
                  elseif ( $img_width < $img_height ) // For tall images...
                  {
                        $new_height = $max_dimension;
                        $new_width = $new_height * $aspect_ratio;
                  }
                  elseif ( $img_width == $img_height ) // For square images...
                  {
                        $new_width = $max_dimension;
                        $new_height = $max_dimension;
                  }
                  else { echo "Error reading image size."; return FALSE; }
                  }
                  else { $new_width = $img_width; $new_height = $img_height; } // If it's already smaller, don't change the size.
                    
                  // Make sure these are integers.
                  $new_width = intval($new_width);
                  $new_height = intval($new_height);
                    
                  $thumbnail = imagecreatetruecolor($new_width,$new_height); // Creates a new image in memory.

                  // The following block retrieves the source file.  It assumes the filename extensions match the file's format.
                  if ( strpos($source_file,".gif") ) { $img_source = imagecreatefromgif($source_file); }
                  if ( (strpos($source_file,".jpg")) || (strpos($source_file,".jpeg")) || (strpos($source_file,".JPG")) || (strpos($source_file,".JPEG")) )
                  { $img_source = imagecreatefromjpeg($source_file); }
                  if ( strpos($source_file,".bmp") ) { $img_source = imagecreatefromwbmp($source_file); }
                  if ( strpos($source_file,".png") ) { $img_source = imagecreatefrompng($source_file); }
                    
                  // Here we resample and create the new jpeg.
                  imagecopyresampled($thumbnail, $img_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
                  imagejpeg( $thumbnail, $destination_file, 100 );
                    
                  // Finally, we destroy the two images in memory.
                  imagedestroy($img_source);
                  imagedestroy($thumbnail);


Everything works fine with images with jpg/jpeg extension. But I get just black thumbnails if I use files with JPG/JPEG extension. And the warnings I get are these:

Warning: imagecopyresampled(): 10 is not a valid Image resource in C:\Test\add.php on line 146

Warning: imagedestroy(): 10 is not a valid Image resource in C:\Test\add.php on line 150

0
Comment
Question by:RealSnaD
  • 2
3 Comments
 
LVL 10

Expert Comment

by:ClickCentric
ID: 18776617
Do you have the gd libraries installed?
0
 
LVL 7

Author Comment

by:RealSnaD
ID: 18776733
Yes. Everything is working fine when I use jpg/jpeg in lower case.
0
 
LVL 10

Accepted Solution

by:
ClickCentric earned 500 total points
ID: 18777398
Hmm...php on windows isn't something I'm too familiar with, but could it be because windows files aren't case sensitive?  Test what img_source is returning when you call it inside of that if statement.  My guess is that the filename is coming up invalid and so img_source never ends up holding a valid resource identifier.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now