Solved

Thumbnail creation problem

Posted on 2007-03-22
3
667 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
[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: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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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…

635 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