Solved

Thumbnail creation problem

Posted on 2007-03-22
3
647 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Popup to change image and store url in database 2 30
Animated .jpg? 13 57
Sql query on a varchar that is numeric. 8 24
Problem sending file attachments 8 22
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

10 Experts available now in Live!

Get 1:1 Help Now