www.webdeveloper.com
Results 1 to 4 of 4

Thread: image loader problem

  1. #1
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197

    image loader problem

    Hi

    below is code that I had. The part to resize the image and the other part if code that processes images I want to upload. The problem I'm having is with the part that I added to resize the image. I'm getting error messages. any ideas whats going wrong

    error message

    photos/GirlTree.jpg
    Warning: imagecreatefromjpeg(photos/GirlTree.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 82

    Warning: getimagesize(photos/GirlTree.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 85

    Warning: Division by zero in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 93

    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 94

    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 99

    Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 105

    Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 107

    Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 108
    photos/BoyFishing.jpg
    Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 9336 bytes) in C:\xampp\htdocs\familyclickNew\gallery\scripts\uploader.php on line 82


    Code:
    <?php
        // This script has been updated. Please visit: http://www.top-frog.com/archives/2006/12/22/classes_for_file_uploading_in_php
    
        /*
            this sample is proceedural for those not familiar with OOP. Simply include this file in your
            form processing script and it will handle the uploads. You'll definitely want to make changes to the 
            upload directory and to some of the functionality to change it to how you like to work
            
        !!    This file does no security checking - this solely handles file uploads -
        !!    this file does not handle any security functions. Heed that warning! You use this file at your 
        !!    own risk and please do not publically accept files if you don't know what you're doing with
        !!    server security.
    
    
            at the end of this script you will have two variables
            $filenames - an array that contains the names of the file uploads that succeeded
            $error - an array of errors that occured while processing files
            
            
            if the max file size in the form is more than what is set in php.ini then an addition 
            needs to be made to the htaccess file to accomodate this
             
            add this to  your .htaccess file for this directory
            php_value post_max_size 10M
            php_value upload_max_filesize 10M
            
            replace 10M to match the value you entered above for $max_file_size
             
        */    
        
        // images dir - relative from document root
        // this needs to be a folder that is writeable by the server
        $image_dir = 'photos/';
       
        // upload dir<img src="photos/photosGirl.jpg" width="2094" height="3342">
        $destination = /*$_SERVER['DOCUMENT_ROOT'].*/$image_dir;
            
        if(isset($_FILES))
            {
                // initialize error var for processing
                $error = array();     
                
                // acceptable files
                // if array is blank then all file types will be accepted
                $filetypes = array(
                            'bmp' => 'image/x-ms-bmp',
                            'gif' => 'image/gif',
                            'jpg' => 'image/jpeg',
                            'jpe' => 'image/jpeg',
                            'jpeg' => 'image/jpeg',
                        );
                        
                // function to check for accpetable file type
                function okFileType($type)
                    {
                        // if filetypes array is empty then let everything through
                        if(count($GLOBALS['filetypes']) < 1)
                            {
                                return true;
                            }
                        // if no match is made to a valid file types array then kick it back
                        elseif(!in_array($type,$GLOBALS['filetypes']))
                            {
                                $GLOBALS['error'][] = $type.' is not an acceptable file type. '.
                                                      $type.' has been ignored.';
                                return false;
                            }
                        // else - let the file through
                        else
                            {                        
                                return true;
                            }
                    }
                
                // function to check and move file
                function processFile($file)
                    {    
    				
    				
    $uploadedfile = $GLOBALS['destination'].$file['name']/*$file['name']*/;
    echo $uploadedfile;
    $src = imagecreatefromjpeg($uploadedfile);
    
    // Capture the original size of the uploaded image
    list($width,$height)=getimagesize($uploadedfile);
    
    // For our purposes, I have resized the image to be
    // 600 pixels wide, and maintain the original aspect
    // ratio. This prevents the image from being "stretched"
    // or "squashed". If you prefer some max width other than
    // 600, simply change the $newwidth variable
    $newwidth=275;
    $newheight=($height/$width)*275;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    
    
    // this line actually does the image resizing, copying from the original
    // image into the $tmp image
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    
    // now write the resized image to disk. I have assumed that you want the
    // resized, uploaded image file to reside in the ./images subdirectory.
    $finishedFilename = $GLOBALS['destination'].$file['name'];
     
    imagejpeg($tmp,$filename,65);
    
    imagedestroy($src);
    imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
    // has completed.
                        // set full path/name of file to be moved
                        $upload_file = $finishedFilename/*$GLOBALS['destination'].$file['name']*/;
                        
                        if(file_exists($upload_file))
                            {
                                $GLOBALS['error'][] = $file['name'].' - Filename exists - please change your image filename';
                                return false;
                            }
                        
                        if(!move_uploaded_file($file['tmp_name'], $upload_file)) 
                            {
                                // failed to move file
                                $GLOBALS['error'][] = 'File Upload Failed on '.$file['name'].' - Please try again';
                                return false;
                            } 
                        else 
                            {
                                // upload OK - change file permissions
                                chmod($upload_file, 0755);
                                return true;
                            }    
                    }
                    
                // check to make sure files were uploaded
                $no_files = 0;
                $uploaded = array();
                foreach($_FILES as $file)
                    {
                        switch($file['error'])
                            {
                                case 0:
                                    // file found
                                    if($file['name'] != NULL && okFileType($file['type']) != false)
                                        {
                                            // process the file
                                            if(processFile($file) == true)
                                                $uploaded = $file['name'];
                                        }
                                    break;
                                    
                                case (1|2):
                                    // upload too large
                                    $error[] = 'file upload is too large for '.$file['name'];
                                    break;
                                    
                                case 4:
                                    // no file uploaded
                                    break;
                                    
                                case (6|7):
                                    // no temp folder or failed write - server config errors
                                    $error[] = 'internal error - flog the webmaster on '.$file['name'];
                                    break;
                            }
                    }
                    
            }
    ?>
    Kevin

  2. #2
    Join Date
    Mar 2006
    Location
    Chennai
    Posts
    32
    Hi,
    Please, check whether file is existing or not.
    If it exist, check whether current version is PHP 5 or not and chech GD library is set on or off.
    If the file is existing,code will work perfectly.
    By
    John Diraviyam,
    PHP Developer.

  3. #3
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    HI

    I used the exact same script on the same service withthe same version of PHP and it works fine.
    Kevin

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    The script is building a unix file path and you are running the code under windows. You cannot have a file in '/path/to/your/doc/root/photos' under windows because there is no such location. As the error message says, 'No such file or directory'.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles