Results 1 to 8 of 8

Thread: Uploading / Resizing Images?

  1. #1
    Join Date
    Dec 2008
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Uploading / Resizing Images?

    Am i calling to the function wrong?
    I cant find another reason why this wouldnt work...


    PHP Code:
    <?php

        
    function getFileExtension($str) {

            
    $i strrpos($str,".");
            if (!
    $i) { return ""; }

            
    $l strlen($str) - $i;
            
    $ext substr($str,$i+1,$l);

            return 
    $ext;

        }
        
            if(isset(
    $_POST['image'])){
                
                 
    $filename stripslashes($_FILES['image']['name']);
                  
    $extension getExtension($filename);
                 
    $extension strtolower($extension);
                 
                if(
    $extension == "png"){
                    function 
    uploadImage($percent$n){
                            global 
    $extension;
                            global 
    $filename;
                            list(
    $width$height) = getimagesize($filename);
                            
    $newwidth $width $percent;
                            
    $newheight $height $percent;
                            
    $thumb imagecreatetruecolor($newwidth$newheight);
                            
    $source imagecreatefrompng($filename);
                            
    $image_name=md5(time()).'.'.$extension;
                              
    $newname="images/$n_".$image_name;
                             
    $copied copy($_FILES['image']['tmp_name'], $newname);    
                
                    }                                
                } 
                
                
    uploadImage(1l);
                
    uploadImage(0.60m);
                
    uploadImage(0.35s);                         
            }
            
    ?>

            
    <form method="post" enctype="multipart/form-data">
    <input type="file" id="image" />
    <input type="submit" value="Upload image" id="image" />
    </form>

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Always give name attributes to your form inputs.

    HTML Code:
    <input type="file" name="image" />
    <input type="submit" value="Upload image" name="submit" />
    You cannot have two elements with the same ID.

    Also, it is always better to test for the form being submitted than to test for any element. So instead of:

    Code:
    if(isset($_POST['image'])){
    I would put:

    Code:
    if ( isset($_POST['submit']) ) {
    Doing this forces someone to submit the form rather than just posting an image to your script.
    Last edited by JasonDFR; 01-17-2009 at 09:24 AM. Reason: Saw some other potential problems

  3. The Following User Says Thank You to JasonDFR For This Useful Post:

    mada9369 (01-17-2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    The code below may be easier to use than the function in your code:

    PHP Code:
    $file_name basename$_FILES['image']['name'] ); // File name ex: 'image.jpg'

    $ext substr$file_namestrrpos($file_name'.') + ); // File extension ex 'jpg' 
    Good Luck,

    J

  5. The Following User Says Thank You to JasonDFR For This Useful Post:

    mada9369 (01-17-2009)

  6. #4
    Join Date
    Dec 2008
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by JasonDFR View Post
    Always give name attributes to your form inputs.

    HTML Code:
    <input type="file" name="image" />
    <input type="submit" value="Upload image" name="submit" />
    You cannot have two elements with the same ID.

    Also, it is always better to test for the form being submitted than to test for any element. So instead of:

    Code:
    if(isset($_POST['image'])){
    I would put:

    Code:
    if ( isset($_POST['submit']) ) {
    Doing this forces someone to submit the form rather than just posting an image to your script.
    That worked perfectly in sending the information!
    But when i try to upload an image; it gives errors...

    It says that there is something wrong with the getimagesize(); function.

    PHP Code:
    list($width$height$type$attr) = getimagesize($filename); 
    Last edited by mada9369; 01-17-2009 at 10:29 PM.

  7. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Copy and paste your error or link the page with the error.

    I'm glad the first part is working for you.

    Jason

  8. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    The ['name'] index of the $_FILES array only holds the original file name of the file the user uploaded, AS IT EXISTS ON THE USERS MACHINE.

    You need to use the ['tmp_name'] index to get the actual file location where PHP has copied the user's file to ON YOUR WEB SERVER.

    So,

    PHP Code:
    $tmp_name $_FILES['image'] ['tmp_name'];

    list(
    $width$height$type$attr) = getimagesize($tmp_name); 
    should work. Useing the ['name'] index with getimagesize() will never work.

    There are some other issues with your code:

    Define functions before you start coding. A function definition should never be inside an IF block.

    You are going to have the same problem with $source = imagecreatefrompng($filename); You need to use the ['tmp_name'] when working with the uploaded file.

    You are eventually going to have to use move_uploaded_file("file", "dir/name.ext") to put your files in their final storage place after you are done with your resizing.

    Since you are making numerous resized images, you'll need to think about how to store them without losing the original ['tmp_name'] file.

    If this script is going to be used by anyone other than you, you'll want to think about security too.

    Good luck,

    Jason
    Last edited by JasonDFR; 01-18-2009 at 08:09 AM.

  9. #7
    Join Date
    Dec 2008
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    PHP Code:
    $tmp_name $_FILES['image'] ['tmp_name'];
    $thumb imagecreatetruecolor($newwidth$newheight);
    $source imagecreatefrompng($tmp_name);
    $image  imagecopyresized($thumb$source0000$newwidth$newheight$width$height);
                            
    $image_name=md5(time()).'.'.$extension;
    $newname="images/$name".$image_name;
    $copied copy($image$newname); 
    this is what i was planning on doing for moving and resizing the images.
    but it gives an error


    Code:
    Warning: copy(1) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\testing\i.php on line 34
    
    Warning: copy(1) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\testing\i.php on line 34
    
    Warning: copy(1) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\testing\i.php on line 34

  10. #8
    Join Date
    Dec 2008
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    Maybe there is a way to convert
    Code:
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    into something else besides 1 or 0?

Bookmarks

Posting Permissions

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