Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-03-2009, 10:10 PM   PM User | #1
treeleaf20
Regular Coder

 
Join Date: Oct 2009
Posts: 438
Thanks: 9
Thanked 7 Times in 7 Posts
treeleaf20 is an unknown quantity at this point
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?
treeleaf20 is offline   Reply With Quote
Old 11-03-2009, 10:26 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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'
    
); 
__________________
Fumigator is offline   Reply With Quote
Old 11-03-2009, 10:42 PM   PM User | #3
treeleaf20
Regular Coder

 
Join Date: Oct 2009
Posts: 438
Thanks: 9
Thanked 7 Times in 7 Posts
treeleaf20 is an unknown quantity at this point
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.
treeleaf20 is offline   Reply With Quote
Old 11-03-2009, 11:55 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.";

__________________
Fumigator is offline   Reply With Quote
Old 11-04-2009, 02:10 AM   PM User | #5
treeleaf20
Regular Coder

 
Join Date: Oct 2009
Posts: 438
Thanks: 9
Thanked 7 Times in 7 Posts
treeleaf20 is an unknown quantity at this point
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?
treeleaf20 is offline   Reply With Quote
Old 11-04-2009, 04:02 PM   PM User | #6
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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
__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 AM.


Advertisement
Log in to turn off these ads.