imagecreatefromgif not creating valid gif file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
clem_c_rock
Forum Commoner
Posts: 46
Joined: Mon Jun 07, 2004 9:18 am

imagecreatefromgif not creating valid gif file

Post by clem_c_rock »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

    I'm building a picture gallery and I'm using the imagecreatefromgif function to create thumbnails.     It works perfectly but if you try to run the function again on the gif that was created by the first imagecreatefromgif() call you then get a warning message that the .gif file is not a valid GIF file.

It's strange that the function will create a perfectly sized gif the first time and then when you try to run that gif that it just created through the function again it gives you an error message.

Here's my code:

Code: Select all

if( strpos(strtolower($source_file),".gif") ) 
	 {
       	$img_source = imagecreatefromgif($source_file); /* Attempt to open */
       	if(!$img_source) 
       	{  	/* See if it failed */
       		$img_source = imagecreatetruecolor (150, 30); /* Create a blank image */
       		$bgc = imagecolorallocate ($img_source, 255, 255, 255);
      		 $tc = imagecolorallocate ($img_source, 0, 0, 0);
       		imagefilledrectangle ($img_source, 0, 0, 150, 30, $bgc);
       		/* Output an errmsg */
       		imagestring( $img_source, 1, 5, 5, "Error loading $imgname", $tc);
  	 	}
	 }
Thanks for any help.

Clem C


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That doesn't look like all your code relating to this problem. :? Can you post the rest?
clem_c_rock
Forum Commoner
Posts: 46
Joined: Mon Jun 07, 2004 9:18 am

Post by clem_c_rock »

Here's the full function:

Code: Select all

function create_thumbnail( $source_file, $destination_file, $max_dimension, $added_size )
{
      list( $img_width, $img_height) = getimagesize( $source_file ); // Get the original dimentions
      $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 /*&& $img_width < $max_dimension*/ ) // For wide images...
             {
                   $new_width  = $max_dimension;
                   $new_height = $new_width / $aspect_ratio;
             }
             elseif( $img_width < $img_height /*&& $img_width > $max_dimension*/ ) // For tall 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) + $added_size );
       $new_height = ( intval($new_height) + $added_size );
  
       $thumbnail = imagecreatetruecolor( $new_width,$new_height ); // Creates a new image in memory.

 	 if( strpos(strtolower($source_file),".gif") ) 
	 {
       	$img_source = imagecreatefromgif($source_file); /* Attempt to open */
       	if(!$img_source) 
       	{  	
       		$img_source = imagecreatetruecolor (150, 30); /* Create a blank image */
       		$bgc = imagecolorallocate ($img_source, 255, 255, 255);
      		 $tc = imagecolorallocate ($img_source, 0, 0, 0);
       		imagefilledrectangle ($img_source, 0, 0, 150, 30, $bgc);
       		
       		imagestring( $img_source, 1, 5, 5, "Error loading $imgname", $tc);    /* Output an errmsg */
  	 	}
	 }

       if( strpos(strtolower($source_file),".jpg") || strpos($source_file,".jpeg") ){ $img_source = imagecreatefromjpeg($source_file); }
       if( strpos(strtolower($source_file),".bmp") ) { $img_source = imagecreatefromwbmp($source_file); }
       if( strpos(strtolower($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);

}//___endFunc___
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your function outputs a JPEG file.
Post Reply