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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2009
    Location
    Delaware
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Unhappy Frustration with image resizer working and not working depending on filename?

    Hello again. *sighs*

    After scouring the web for help on image-resizing scripts, I thought I had figured out what I needed to get the job done. Everything was going marvelous until I started trying to upload random pictures (via testing on my local machine, mind you) with the script.

    Some would work, some would not.

    Basically, its a news posting script.
    -- member selects a picture for the story, inputs a headline and the article text.
    -- If no image is chosen, a default one will be selected.
    -- If there IS an image chosen, it will be uploaded and resized down to 500 pixels wide AND create another 100pixel thumbnail of the same image.

    It was running just fine until I started messing around and selecting different images. The test images I had been using were not necessarily small, mind you, but they weren't uncompressed .JPG's from digital cameras. I'm not sure if the filesize has anything to do with it (as I'm not setting a max-file-size in the upload form...or should I?) because whenever I try to upload a 2MB+ image the script just jumps to the section where it will insert the information and use a default image, instead of doing what it is supposed to do.

    Excuse the frankenstein-like coding below, which was taken and modified from a script found on the net. I'm just half baffled as to what is going on, because what I would like is for my team members to be able to take a screenshot of our wonderful racing simulation and upload it for a news story. Generally depending on the filetype used in FRAPS (screen capture program that is popular in our circle) the filesizes can range.

    I'm just...lost as to what is going on. I figured at the start it was the spaces in the filename such as "World Series 001.jpg" not working, where something like "iRacingScreenshot.jpg" would. But then I removed the spaces and renamed the files opposite, and they worked just fine.

    Confusion.

    PHP Code:
    <?php

    if(isset($_POST['Submit']))

        {
        
    dbconnect();
        
    selectdb();
        
        
    // define the article variables.
        
    $userID $_SESSION['userID'];
        
    $headline addslashes($_POST['headline']);
        
    $article addslashes($_POST['article']);
        
    $date time();
        
        
        
    //make sure this directory is writable!    
        
    $path_thumbs "../newsimages/";        
        
        
    //the new width of the resized images, in pixels.    
        
    $img_thumb_width 500;
        
    $img_thumb_width2 110// 
        
        //Limit allowed extensions? (no for all extensions allowed)
        
    $extlimit "yes"
        
        
    //List of allowed extensions if extlimit = yes
        
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");        
        
        
    //the image -> variables    
        
    $file_type $_FILES['vImage']['type'];    
        
    $file_name $_FILES['vImage']['name'];
        
    $file_size $_FILES['vImage']['size'];    
        
    $file_tmp $_FILES['vImage']['tmp_name'];
        
        
    //check if you have selected a file
        // IF NOT, INSERT INFORMATION INTO THE DATABASE ACCORDINGLY
        
    if(!is_uploaded_file($file_tmp)){
            
    $imageFilename "newsDefault.jpg";
            
            
    $insertNonPicNews mysql_query("INSERT INTO news (userID, date, headline, article,imageFilename) VALUES ('$userID','$date','$headline','$article','$imageFilename')");
            
            if (
    $insertNonPicNews == TRUE)
                {
                echo 
    "You did not select a picture to upload with your news article. All of the information was entered accordingly, and a default image will be shown for the article.";
                exit(); 
    //exit the script and don't process the rest of it!    
                
    }
            else {
                echo 
    "Ryan screwed up the coding somewhere. Ugh. :(";
                exit(); 
    //exit the script and don't process the rest of it!    
            
    }    
            
                
            }
        
        
    //check the file's extension    
        
    $ext strrchr($file_name,'.');    
        
    $ext strtolower($ext);
        
        
    //uh-oh! the file extension is not allowed!    
        
    if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {    
            echo 
    "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";        
            exit();        
            }
        
        
    //so, whats the file's extension?    
        
    $getExt explode ('.'$file_name);    
        
    $file_ext $getExt[count($getExt)-1];
        
        
    //create a random file name    
        
    $rand_name md5(time());    
        
    $rand_namerand(0,999999999);
        
        
    //the new width variable    
        
    $ThumbWidth $img_thumb_width;
        
    $ThumbWidth2 $img_thumb_width2;
        
        
        
    /////////////////////////////////
        
        // CREATE THE THUMBNAIL //
        
        ////////////////////////////////    
        
        //keep image type
        
        
    if($file_size){    
            if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){        
                
    $new_img imagecreatefromjpeg($file_tmp);            
                }
            elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){        
                
    $new_img imagecreatefrompng($file_tmp);            
                }
            elseif(
    $file_type == "image/gif"){        
                
    $new_img imagecreatefromgif($file_tmp);            
                }
        
            
    //list the width and height and keep the height ratio.        
            
    list($width$height) = getimagesize($file_tmp);        
            
    //calculate the image ratio
            
            
    $imgratio=$width/$height;
            
            if (
    $imgratio>1){        
                
    $newwidth $ThumbWidth;            
                
    $newheight $ThumbWidth/$imgratio;            
                }
            else{
            
                
    $newheight $ThumbWidth;            
                
    $newwidth $ThumbWidth*$imgratio;            
                }
        
        
    //calculate the thumb ratio
        
        
    $imgratio2=$width/$height;
        
        if (
    $imgratio>1){    
            
    $newwidth2 $ThumbWidth2;        
            
    $newheight2 $ThumbWidth2/$imgratio2;        
            }
        else{    
            
    $newheight2 $ThumbWidth2;        
            
    $newwidth2 $ThumbWidth2*$imgratio2;        
            }
        
        
    //function for resize image.
        
        
    if (function_exists(imagecreatetruecolor)){    
            
    $resized_img imagecreatetruecolor($newwidth,$newheight);        
            }
        else{    
            die(
    "Error: Please make sure you have GD library ver 2+");
            }
        
        
    //function for resize thumb.    
        
    if (function_exists(imagecreatetruecolor)){    
            
    $resized_img2 imagecreatetruecolor($newwidth2,$newheight2);        
            }
        else{
            die(
    "Error: Please make sure you have GD library ver 2+");
            }
        
        
    //the resizing is going on here!    
        
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);
        
        
    //the resizing is going on here!    
        
    imagecopyresampled($resized_img2$new_img0000$newwidth2$newheight2$width$height);
        
        
    //finally, save the image    
        
    imagejpeg($resized_img,"$path_thumbs/$rand_name.$file_ext"90);
        
        
    imagedestroy($resized_img);
            
        
    // the thumb
        
    $thumbext "_thumb";
        
    imagejpeg($resized_img2,"$path_thumbs/$rand_name$thumbext.$file_ext"100);
        
        
    imagedestroy($resized_img2);
        
        
    imagedestroy($new_img);    
        
        }    
        
        
    //ok copy the finished file to the thumbnail directory    
        
    if(move_uploaded_file ($file_tmp"$path_big/$rand_name.$file_ext")) {
                        
                        
    $imageFilename $rand_name.".".$file_ext;
                        
    $insertPicNews mysql_query("INSERT INTO news (userID, date, headline, article,imageFilename) VALUES ('$userID','$date','$headline','$article','$imageFilename')");
                        
                        if (
    $insertPicNews == TRUE) {
                        
                        
    //Tells you if its all ok
                        
    echo "The picture and article have been added to our database. You are now being redirected back to the uploader.";
                        echo 
    "<meta http-equiv='refresh' content='3;url=newsPublisher.php' />";
                        }
                        else {
                            echo 
    "There was a problem with Ryan's goddamn script!";
                            exit();
                        }
        }
        else {
            echo 
    "There was a problem with the process. Ryan is probably the idiot who coded it incorrectly! :( ";
        };    
        
        
        
        
        }
        else{    
        
    // Echo the form
        
    ?>
            <form action="<?php $_SERVER[PHP_SELF]; ?>" method="post" enctype="multipart/form-data">   
            <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
            <p><input type="file" name="vImage" /> &laquo; Select the image you want to be displayed with the news story (Not needed, but recommended. Please have no spaces in the filename!)</p>    
            <p><input name="headline" type="text" size="25" maxlength="90" /> &laquo; Article Headline (try to keep it to 90 letters or less. Something catchy.</p>
            News Article Text: (No limit, be as long or short as you want.)<br />
            <textarea name="article" cols="100" rows="7"></textarea>
            <p><input type="Submit" name="Submit" value="Submit" /></p>
            </form>
            <?php
            
            
    }
    ?>

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Try a Google for "php upload size limit". My first result: http://www.radinks.com/upload/config.php
    Are you a Help Vampire?

  • #3
    New Coder
    Join Date
    Feb 2009
    Location
    Delaware
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I increased it, but am still encountering the same problem.

    Perhaps my coding is out of order, or some such?

    Or am I possibly testing the wrong variables, and doing operations based off of those incorrect variables?

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,146
    Thanks
    2
    Thanked 333 Times in 325 Posts
    The code has no logic to check that the upload worked before accessing any of the uploaded file information or data - http://us.php.net/manual/en/features...oad.errors.php

    All code must have error checking (check if something worked or didn't), error reporting (output a meaningful user message about what happened that will prevent the script from performing its intended function and log the error so you have a record of problems that you will need to find and fix), and error recovery logic (what do you do when there is an error, blindly continue executing using nonexistent data or gracefully stop.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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