Upload and resize script question

Discussion in 'PHP' started by red-x, Sep 7, 2008.

  1. #1
    I have this script I want to use but it only works with images with extensions .jpg. What should I do to the script so it works with images like .gif, .png, ect. Here's the script...

    <?php
    // This is the temporary file created by PHP
    $uploadedfile = $_FILES['uploadfile']['tmp_name'];
    
    // Create an Image from it so we can do the resize
    $src = imagecreatefromjpeg($uploadedfile);
    
    // Capture the original size of the uploaded image
    list($width,$height)=getimagesize($uploadedfile);
    
    $newwidth=164; //width of the new image.
    $newheight=($height/$width)*158; //height of the new image.
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    
    //Resize the image.
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    
    // Save the new image with a different name.
    $filename = $_FILES['uploadfile']['name'];
    
    
    if (imagejpeg($tmp,$filename,100)) {
      echo 'Picture was uploaded successfully.';
    }
    imagedestroy($src);
    imagedestroy($tmp);
    ?
    PHP:
    >

    Thanks in advance.
     
    red-x, Sep 7, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    jayshah, Sep 7, 2008 IP
  3. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    Jayshah was right, you need to change the function names - But if you want to keep the same file type as what was uploaded, you must perform a check to detect the current image type.

    for example checking the extension;

    
    $pathExt = pathinfo($_FILES['uploadfile']['tmp_name']);
     
    if($pathExt["extension"] == "gif")
    {
    	//Do imagecreatefromgif function
    }
    elseif($pathExt["extension"] == "jpg")
    {
    	//Do imagecreatefromjpeg function
    }
    elseif($pathExt["extension"] == "png")
    {
    	//Do imagecreatefrompng function
    }
    else
    {
    	//Not a supported filetype
    } 
    
    PHP:
    Regards,

    Steve
     
    Steve136, Sep 7, 2008 IP
    jayshah likes this.
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Thanks :cool:

    You can also use the (deprecated) mime_content_type or the newer FileInfo to detect the true filetype without relying on the extension.

    Jay
     
    jayshah, Sep 7, 2008 IP
    Steve136 likes this.
  5. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you guys! :), but I try that and it gave me some errors.

    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\Test\test1\upload.php on line 33

    Line 33: imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

    Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\Test\test1\upload.php on line 44

    Line 44: imagedestroy($src);


    And the script updated...

    <?php
    
    // This is the temporary file created by PHP
    $uploadedfile = $_FILES['uploadfile']['tmp_name'];
    
    $pathExt = pathinfo($uploadedfile);
     
    if($pathExt["extension"] == "gif")
    {
    $src = imagecreatefromgif($uploadedfile);
    }
    elseif($pathExt["extension"] == "jpg")
    {
    $src = imagecreatefromjpeg($uploadedfile);
    }
    elseif($pathExt["extension"] == "png")
    {
    $src= imagecreatefrompng($uploadedfile);
    }
    else
    {
      echo 'Sorry the image you\'re tying to upload it\'s not allowed.';
    }
    
    // Capture the original size of the uploaded image
    list($width,$height)=getimagesize($uploadedfile);
    
    $newwidth=164; //width of the new image.
    $newheight=($height/$width)*158; //height of the new image.
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    
    //Resize the image.
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    
    // Save the new image with a different name.
    
    $new_name = $_FILES['uploadfile']['name'];
    $filename = "images/$new_name";
    
    if (imagejpeg($tmp,$filename,100)) {
      echo 'Picture was uploaded successfully.';
    }
    
    imagedestroy($src);
    imagedestroy($tmp);
    ?>
    PHP:
     
    red-x, Sep 7, 2008 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    That means that your imagecreatefrom is not working properly. imagecopyresampled is not getting anything.
     
    JEET, Sep 7, 2008 IP