Solved

Thumbnail creation problem

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hosting images 4 29
paypal ipn to mysql 3 38
How would I do a website like this? 5 57
How to obtain the string from a PHP StdObject ? 6 20
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

778 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