uploading Images with GIF extension

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 3483 Views - Last Post: 15 July 2010 - 09:32 AM Rate Topic: -----

#1 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

uploading Images with GIF extension

Posted 06 July 2010 - 04:12 AM

Hey there,
I'm having problem to upload images with GIF extension in directory. Added part of code to allow the the script to upload the gif extension but not doing it. Please help. Thank in advance.

if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg" || $_FILES['imagefile']['type'] == "image/gif") { 
            
          $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php  
          $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location 


Is This A Good Question/Topic? 0
  • +

Replies To: uploading Images with GIF extension

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: uploading Images with GIF extension

Posted 06 July 2010 - 04:16 AM

Are you getting any errors, or is it just avoiding entering the if statement you have there?
Was This Post Helpful? 0
  • +
  • -

#3 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: uploading Images with GIF extension

Posted 06 July 2010 - 04:36 AM

Thanks for reply. I'm not getting any errors. More likely avoid to enter in if statement. But it jumped to this line of code and display the message in screen.

print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong

Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: uploading Images with GIF extension

Posted 06 July 2010 - 04:41 AM

Alter that error message to echo out the file type just to see if it's that you're not getting the type you're expecting, or that your loop is somehow at fault.

EDIT: Also, you might consider changing that if statement to a switch to make it more readable. Something like this, for example:

switch($_FILES['imagefile']['type'])
{
     case "image/jpg":
     case "image/jpeg":
     case "image/pjpeg":
     case "image/gif":       
          $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php  
           $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location 
     break;
     default:
         print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong
     break;
}


This post has been edited by Valek: 06 July 2010 - 04:45 AM

Was This Post Helpful? 1
  • +
  • -

#5 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: uploading Images with GIF extension

Posted 06 July 2010 - 09:12 AM

Thanks for your suggestion but it doing the same.

This what doing after I changed the script! It uploaded the original images into directory folder with gif extension but it doesn't upload the same image in thumbmail folder. That meant it isn't creating thumbnail images with gif extension. Also stop apache working when I upload image with gif extension.

Amended code.

if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg" || $_FILES['imagefile']['type'] == "image/gif") { 
            
          $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php  
          $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location  
     
        if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location  

     // print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image  
      $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From  
      $currwidth = imagesx($simg);   // Current Image Width  
      $currheight = imagesy($simg);   // Current Image Height

Was This Post Helpful? 0
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: uploading Images with GIF extension

Posted 06 July 2010 - 02:26 PM

Of course it's failing on the thumbnail if you're using a gif. You're using imagecreatefromjpeg(), which does exactly as the function name says: it creates a GD image resource from a JPEG image. You'll want it to check the extension, and if it's a gif, you'll want to use imagecreatefromgif() in the case of gif images.

The switch wasn't going to fix the problem, but it cleans the code up a little bit so it's at least easier to read.

Also, imagecreatefromjpeg() and imagecreatefromgif() only create the image resource. They don't actually write the files. To write the files, you'll want to do something such as imagegif($simg, 'images/image.gif'); or imagejpeg($simg, 'images/image.jpg'); once you're finished creating the image, depending on what type of image you're working with. Seeing as these are thumbnails, you would run this after changing the width and height values in the image resource (I'd recommend using imagecopyresampled() to achieve this).
Was This Post Helpful? 3
  • +
  • -

#7 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: uploading Images with GIF extension

Posted 08 July 2010 - 02:58 AM

Thanks for your reply and suggestioons. But I'm having problem to add imagecreatefromgif() function to script, getting error message. Also I must make function for every images extension ex:bitmap etc. Thank in advance.
Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: uploading Images with GIF extension

Posted 08 July 2010 - 03:07 AM

What error message(s) are you getting?
Was This Post Helpful? 1
  • +
  • -

#9 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: uploading Images with GIF extension

Posted 09 July 2010 - 03:28 AM

Hey there,
error that I'm getting stopping apache working and not creating thumbnail when upload image with gif extension. This what have done I highlight in red the amended in script. Thanks in advance.

if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg" || $_FILES['imagefile']['type'] == "image/gif") 
          { 
            
          $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php  
          $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location  
     
        if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location  

     // print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image  
      $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From  
      [color="#FF0000"]$simg = imagecreatefromgif("$idir" . $url);[/color]      $currwidth = imagesx($simg);   // Current Image Width  
      $currheight = imagesy($simg);   // Current Image Height 
      
  
      if ($currheight > $currwidth) {   // If Height Is Greater Than Width  
         $zoom = $twidth / $currheight;   // Length Ratio For Width  
         $newheight = $theight;   // Height Is Equal To Max Height  
         $newwidth = $currwidth * $zoom;   // Creates The New Width  

      } else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)  
        $zoom = $twidth / $currwidth;   // Length Ratio For Height  
        $newwidth = $twidth;   // Width Is Equal To Max Width  
        $newheight = $currheight * $zoom;   // Creates The New Height  
      }  
     $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail  
      imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete  
      $palsize = ImageColorsTotal($simg);  
      for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image  
       $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used  
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use  
      }  
      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)  
      imagejpeg($dimg, "$tdir" . $url, 100);   // Saving The Image 
      [color="#FF0000"]imagegif($dimg, "$tdir" . $url, 100);   // Saving The Image   [/color]
      imagedestroy($simg);   // Destroying The Temporary Image  
      imagedestroy($dimg);   // Destroying The Other Temporary Image  
     // print 'Image thumbnail created successfully.';   // Resize successful  
    } else { 
	   
      print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed  
    }  
  } else {  
    print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong  
   // print $file_ext;   // Show The Invalid File's Extention  
    print '.</font>';  
  } 

Was This Post Helpful? 1
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: uploading Images with GIF extension

Posted 09 July 2010 - 04:41 PM

If you're uploading a GIF, imagecreatefromjpeg() isn't going to work on it. You'd have to run imagecreatefromgif(), just as I said before.

If you change the if statement to a switch, you can have all of them upload the file as desired, but change which thumbnail creation functions you use depending on what type is uploaded.
Was This Post Helpful? 1
  • +
  • -

#11 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: uploading Images with GIF extension

Posted 12 July 2010 - 04:58 AM

Thank so much for suggestions.
My problem is! How can I use imagecreatefromjpeg() and imagecreatefromgif() in same script to be able to create different thumbnail images extension(gif, jpeg,bitmap). If you checked on my script it create only thumbnail images with jpeg extension. Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#12 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: uploading Images with GIF extension

Posted 12 July 2010 - 08:04 PM

Use an if statement. If it's a JPEG, use imagecreatefromjpeg(). If it's a GIF, use imagecreatefromGIF(). It's very simple logic.

Even simpler, you could turn that massive if statement you have into a switch statement, and run your thumbnail creation code inside the case statements for that. Make sure to remember to break, though. And the default case would be you wrong file type part.

This post has been edited by Valek: 12 July 2010 - 08:04 PM

Was This Post Helpful? 1
  • +
  • -

#13 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: uploading Images with GIF extension

Posted 13 July 2010 - 04:26 AM

I'm getting these errors message:

Warning: imagecreatefromgif() [function.imagecreatefromgif]: 'C:/wamp/www/myproject1/IMGEgypt/Cities/Alexandria.jpg' is not a valid GIF file in C:\wamp\www\myproject1\TestThumbnail.php on line 126

Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\wamp\www\myproject1\TestThumbnail.php on line 128

Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\wamp\www\myproject1\TestThumbnail.php on line 129

Warning: Division by zero in C:\wamp\www\myproject1\TestThumbnail.php on line 138

Warning: imagecreate() [function.imagecreate]: Invalid image dimensions in C:\wamp\www\myproject1\TestThumbnail.php on line 142

Warning: imagetruecolortopalette() expects parameter 1 to be resource, boolean given in C:\wamp\www\myproject1\TestThumbnail.php on line 143

Warning: imagecolorstotal() expects parameter 1 to be resource, boolean given in C:\wamp\www\myproject1\TestThumbnail.php on line 144

Warning: imagecopyresized() expects parameter 1 to be resource, boolean given in C:\wamp\www\myproject1\TestThumbnail.php on line 149

Warning: imagegif() expects parameter 1 to be resource, boolean given in C:\wamp\www\myproject1\TestThumbnail.php on line 150

Warning: imagedestroy() expects parameter 1 to be resource, boolean given in C:\wamp\www\myproject1\TestThumbnail.php on line 152

Warning: imagedestroy() expects parameter 1 to be resource, boolean given in C:\wamp\www\myproject1\TestThumbnail.php on line 153


Now It created thumbnail and uploaded images with gif extension but not with jpg. This what I tried to do!

 switch($_FILES['imagefile']['type'])  
          {  

      		case "image/jpg": 
      		
      			 

      		case "image/jpeg": 
      		 
				$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location         
					if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location  

     			// print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image  
      			$simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From  
     
     		 	$currwidth = imagesx($simg);   // Current Image Width  
      		 	$currheight = imagesy($simg);   // Current Image Height 
      
  
      			if ($currheight > $currwidth) {   // If Height Is Greater Than Width  
         			$zoom = $twidth / $currheight;   // Length Ratio For Width  
         			$newheight = $theight;   // Height Is Equal To Max Height  
         			$newwidth = $currwidth * $zoom;   // Creates The New Width  

      			} else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)  
        			$zoom = $twidth / $currwidth;   // Length Ratio For Height  
        			$newwidth = $twidth;   // Width Is Equal To Max Width  
        			$newheight = $currheight * $zoom;   // Creates The New Height  
      			}  
     				$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail  
      				imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete  
      				$palsize = ImageColorsTotal($simg);  
      			for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image  
       				$colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used  
       				ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use  
      			}  
      				imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)  
      				imagejpeg($dimg, "$tdir" . $url, 100);   // Saving The Image 
        
      				imagedestroy($simg);   // Destroying The Temporary Image  
     			 	imagedestroy($dimg);   // Destroying The Other Temporary Image  
     				// print 'Image thumbnail created successfully.';   // Resize successful  
    			}
    			
    		break;
    			
     		case "image/pjpeg":  

      		case "image/gif":
      		$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location         
			if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location  

     		// print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image  
      			$simg = imagecreatefromgif("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From  
     
     		 	$currwidth = imagesx($simg);   // Current Image Width  
      		 	$currheight = imagesy($simg);   // Current Image Height 
      
  
      			if ($currheight > $currwidth) {   // If Height Is Greater Than Width  
         			$zoom = $twidth / $currheight;   // Length Ratio For Width  
         			$newheight = $theight;   // Height Is Equal To Max Height  
         			$newwidth = $currwidth * $zoom;   // Creates The New Width  

      			} else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)  
        			$zoom = $twidth / $currwidth;   // Length Ratio For Height  
        			$newwidth = $twidth;   // Width Is Equal To Max Width  
        			$newheight = $currheight * $zoom;   // Creates The New Height  
      			}  
     				$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail  
      				imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete  
      				$palsize = ImageColorsTotal($simg);  
      			for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image  
       				$colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used  
       				ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use  
      			}  
      				imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)  
      				imagegif($dimg, "$tdir" . $url, 100);   // Saving The Image 
        
      				imagedestroy($simg);   // Destroying The Temporary Image  
     			 	imagedestroy($dimg);   // Destroying The Other Temporary Image  
     			// print 'Image thumbnail created successfully.';   // Resize successful  
    		}else { 
	   
      			print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed  
    			}
    			
    		break;	
				 
      		default:  

          	print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong  

      	  
      	}

Was This Post Helpful? -1
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,008
  • Joined: 08-August 08

Re: uploading Images with GIF extension

Posted 13 July 2010 - 05:52 AM

You'll keep getting this error message until you take Valek's advice:

Warning: imagecreatefromgif() [function.imagecreatefromgif]: 'C:/wamp/www/myproject1/IMGEgypt/Cities/Alexandria.jpg' is not a valid GIF file in C:\wamp\www\myproject1\TestThumbnail.php on line 126


Was This Post Helpful? 0
  • +
  • -

#15 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: uploading Images with GIF extension

Posted 13 July 2010 - 06:23 AM

Hey thank for your reply. I did get Valek advise and I changed the script into switch cases but Now I'm get error with jpg extension. If you know why I'm getting error now check my script and let me know why! Thank in advance.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3