Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    imagecreatefromgif()

    All,
    I have the following code:
    PHP Code:
    function createThumbnail($filename) {  
          
    $final_width_of_image 100;
    $final_height_of_image 100;  
    $path_to_image_directory 'uploaded_files/';  
    $path_to_thumbs_directory 'thumbs/'
      
          
        if(
    preg_match('/[.](jpg)|(JPG)|(jpeg)|(JPEG)$/'$filename)) {  
            
    $im imagecreatefromjpeg($path_to_image_directory $filename);  
        } else if (
    preg_match('/[.](gif)|(GIF)$/'$filename)) {  
            
    $im imagecreatefromgif($path_to_image_directory $filename);  
        } else if (
    preg_match('/[.](png)|(PNG)$/'$filename)) {  
            
    $im imagecreatefrompng($path_to_image_directory $filename);  
        }  
          
        
    $ox imagesx($im);  
        
    $oy imagesy($im);  
          
        
    $nx $final_width_of_image;  
        
    $ny $final_height_of_image;
          
        
    $nm imagecreatetruecolor($nx$ny);  
          
        
    imagecopyresized($nm$im0,0,0,0,$nx,$ny,$ox,$oy);  
          
        if(!
    file_exists($path_to_thumbs_directory)) {  
          if(!
    mkdir($path_to_thumbs_directory)) {  
               die(
    "There was a problem. Please try again!");  
          }   
           }  
      
        
    imagejpeg($nm$path_to_thumbs_directory $filename);  

    When I do this though, it says the .gif file I've uploaded isn't a valid GIF file. Any ideas why that is the case?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Don't rely on a file's extension to determine image type. Use getimagesize() and look at the third index in the return array to find the image type. Here's a conversion chart:

    PHP Code:
        $types = array(
            
    => 'GIF',
            
    => 'JPG',
            
    => 'PNG',
            
    => 'SWF',
            
    => 'PSD',
            
    => 'BMP',
            
    => 'TIFF(intel byte order)',
            
    => 'TIFF(motorola byte order)',
            
    => 'JPC',
            
    10 => 'JP2',
            
    11 => 'JPX',
            
    12 => 'JB2',
            
    13 => 'SWC',
            
    14 => 'IFF',
            
    15 => 'WBMP',
            
    16 => 'XBM'
        
    ); 

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    So how can I pull that and then look it up in the array to get the file name?

    I have:
    PHP Code:
    $x = @getimagesize($file);
    echo 
    "The file type is ".$x[2]; 
    I get "The file type is 2". So it's a JPG from your array. How can I look this up?

    Then I have the following:
    PHP Code:
        if(preg_match('/[.](jpg)|(JPG)|(jpeg)|(JPEG)$/'$filename)) {  
            
    $im imagecreatefromjpeg($path_to_image_directory $filename);  
        } else if (
    preg_match('/[.](gif)|(GIF)$/'$filename)) {  
            
    $im imagecreatefromgif($path_to_image_directory $filename);  
        } else if (
    preg_match('/[.](png)|(PNG)$/'$filename)) {  
            
    $im imagecreatefrompng($path_to_image_directory $filename);  
        } 
    Can I save the result from the array and then do the preg_match on that variable to determine what type of thing to create?

    Thanks.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Why even bother looking at the file extension? Just use that value you get from getimagesize() in your "if" statements (or even better, use a SWITCH statement).

    PHP Code:
    $x = @getimagesize($file);
    switch(
    $x[2]) {
    case 
    1:
        
    $im imagecreatefromgif($path_to_image_directory $filename);  
        break;
    case 
    2:
        
    $im imagecreatefromjpeg($path_to_image_directory $filename);
        break;
    case 
    3:
        
    $im imagecreatefrompng($path_to_image_directory $filename);  
        break;
    default:
        echo 
    "file is not a valid image file.";


  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    I'm not that familiar with SWITCH statements. I'm guessing this mean it's only valid for GIF, JPG and PNG? Which would be perfect because those are the only file types I want. Is this how it works?

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Sometimes there is just no substitute for a good old fashioned PHP Manual study session.

    http://us2.php.net/manual/en/control...res.switch.php


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •