Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Thumbnail creation problem

Posted on 2007-03-22
3
Medium Priority
?
672 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:Yury Merezhkov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Expert Comment

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

Author Comment

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

Accepted Solution

by:
ClickCentric earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

671 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