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 5 of 5

Thread: Resizing code

  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Resizing code

    Hi Guys,

    i have made from a few tutorials code to resize uploaded images but it isn't making the thumbnails code:

    PHP Code:
            #############################################
            # Thumbnail code                            #
            #############################################    
            ## Find out the files extension
            
    $ext explode("."$filename);
             
    $ext $ext[count($ext)-1];
             
             
    ## random numbers
             
    $randomnumber rand(0,999999999);
             
              if(
    $ext == "jpg" || $ext == "jpeg")    
              
                  
    $image imagecreatefromjpeg($filetemp);
                  
              elseif(
    $ext == "png")
              
                  
    $image imagecreatefrompng($filetemp);
                  
              elseif(
    $ext == "gif")
              
                  
    $image imagecreatefromgif($filetemp);
                  
              
    ## save the file in %    
            
    $size 0.50;
            
            
    ## rename the thumbnail
            
    $newimagename_thumb $var_loggedinuser"-" .time();
            
    $save "thumbs/$newimagename_thumb.$ext";
                  
            
    ## get the files dimensions
            
    list($width,$height) = getimagesize($filetemp);
            
            
    $modwidth $width $size
            
    $modheight $height $size
            
    $thumbnail imagecreatetruecolor($modwidth$modheight) ; 
            
    imagecopyresampled($thumbnail$image0000$modwidth$modheight$width$height);        
            
    ## imagejpeg($thumbnail, $save, 100); 
            
            
    if($ext == '.jpg' || $ext == '.jpeg')
         
            
    imagejpeg($thumbnail$save100);
         
            if(
    $ext == '.gif')
         
            
    imagegif($thumbnail$save100);
         
            if(
    $ext == '.png')
         
            
    imagepng($thumbnail$save100);
            
    #############################################
            # Thumbnail code                            #
            ############################################# 
    can anyone see whats wrong at all?

    thanks guys

    Graham

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    while running the script does it throw any error messages?
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    A) Where are the variables $filename and $filetemp getting set at? Are you sure they contain what you expect?

    B) All the image functions return a FALSE value when they fail. Where is your error checking, error reporting, and error recovery logic to test if any of the functions have failed, tell you what failed, and prevent the following code from blindly executing on non-existent data?

    C) You are exploding the $filename using "." and then checking if $ext matches "jpg", "jpeg", "png", and "gif" at one point in your code and checking if $ext matches '.jpg', '.jpeg', '.gif', and '.png' later in your code. Don't you suppose that might be a reason why the conditional tests are not working.
    Last edited by CFMaBiSmAd; 11-07-2007 at 02:51 PM. Reason: made dots bigger and bolder
    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.

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Guys,

    sorry this is the full script:

    PHP Code:
    <?php
      
    ## See if it's a first time user
      
    $query_user "SELECT `photo` FROM `users` WHERE `id`='$var_loggedinuserid'";
      
    $results_user mysql_query($query_user) or die ("Error getting photo 1"); 
      
    $row mysql_fetch_array($results_user) or die ("Error getting the photo array");
      
    $photouploaded $row['photo'];
      
      
    ## HTML form  
      
    echo ('<div style="border: 1px solid black;padding:10px; background: yellow; color: #000000; font-size: 12px;"><b>Upload your main profile headshot here.</b></div><br />');
      echo (
    "<form action=\"uploadphoto.php\" method=\"post\" enctype=\"multipart/form-data\" />"); 
      echo (
    "<table class=\"sub_table\" width=\"500\" border=\"1\" align=\"center\" cellpadding=\"5\" cellspacing=\"0\">");
      echo (
    "<tr>");
      echo (
    "<td colspan=\"2\" class=\"edit\" align=\"left\"><img src=\"images/upload_headshot.jpg\"></td>");
      echo (
    "</tr>");
      echo (
    "<tr>");
      echo (
    "<td align=\"center\"><b>Select a photo to upload</b></td><td align=\"center\"><input name=\"usersphoto\" type=\"file\" size=\"50\" /></td>");
      echo (
    "</tr>");
      echo (
    "<tr>");
      echo (
    "<td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Upload Photo\" /></td>");
      echo (
    "</tr>");
      echo (
    "</table>");
      
    ## HTML form  
             
      ## Deal with the submission
      
    if($_POST['submit']) {
      
        
    $filesize $_FILES['usersphoto']['size'];
        
    $filetype $_FILES['usersphoto']['type'];
        
    $filetemp $_FILES['usersphoto']['tmp_name'];
        
    $filename $_FILES['usersphoto']['name'];
        
        
    ## vars
        
    $maxheight 500;
        
    $maxwidth 500;
        
    $newimagename $var_loggedinuser"-" .time();
        
        
    ## Allowed file types
        
    $allowed_types = array('image/pjpeg','image/gif','image/png','image/jpeg'); 
        
        if(
    $filesize == 0) {
        
         
    stderr("Upload Failed","No file was uploaded.");
         include(
    "includes/footer.php");
         exit;   
        
        }
        
        if(!
    in_array($filetype$allowed_types)) {
        
         
    stderr("Upload Failed","The file you uploaded is not one of the allowed types only .gif and .jpg are allowed.");
         include(
    "includes/footer.php");
         exit;     
        
        }
        
        
    ## Rename the file
        
    $renamedimage $newimagename.".".substr($_FILES["usersphoto"]["name"],strtolower(strlen($_FILES["usersphoto"]["name"]))-3,3); 
        
            
    #############################################
            # Thumbnail code                            #
            #############################################    
            ## Find out the files extension
            
    $ext explode("."$filename);
             
    $ext $ext[count($ext)-1];
             
             
    ## random numbers
             
    $randomnumber rand(0,999999999);
             
              if(
    $ext == "jpg" || $ext == "jpeg")    
              
                  
    $image imagecreatefromjpeg($filetemp);
                  
              elseif(
    $ext == "png")
              
                  
    $image imagecreatefrompng($filetemp);
                  
              elseif(
    $ext == "gif")
              
                  
    $image imagecreatefromgif($filetemp);
                  
              
    ## save the file in %    
            
    $size 0.50;
            
            
    ## rename the thumbnail
            
    $newimagename_thumb $var_loggedinuser"-" .time();
            
    $save "thumbs/$newimagename_thumb.$ext";
                  
            
    ## get the files dimensions
            
    list($width,$height) = getimagesize($filetemp);
            
            
    $modwidth $width $size
            
    $modheight $height $size
            
    $thumbnail imagecreatetruecolor($modwidth$modheight) ; 
            
    imagecopyresampled($thumbnail$image0000$modwidth$modheight$width$height);        
            
    ## imagejpeg($thumbnail, $save, 100); 
            
            
    if($ext == '.jpg' || $ext == '.jpeg')
         
            
    imagejpeg($thumbnail$save100);
         
            if(
    $ext == '.gif')
         
            
    imagegif($thumbnail$save100);
         
            if(
    $ext == '.png')
         
            
    imagepng($thumbnail$save100);
            
    #############################################
            # Thumbnail code                            #
            ############################################# 
          
        
    $uploaddirectory "uploads/".$renamedimage;
        
        
    ## Upload code
        
    if(move_uploaded_file($filetemp$uploaddirectory)) {
        
        
    ## insert the photo in the database
        
    $photoquery mysql_query("UPDATE `users` SET `photo`='$renamedimage',`thumbnail`='$save' WHERE `id`='$var_loggedinuserid'");
        
        
    stderr("Upload Successful","Your image has been uploaded successfully.");

        include(
    "includes/footer.php");
        exit;      
        
        }
      
      } 
    ?>
    the crazy thing is i get NO errors, just the thumbnail doesn't get created! weird

    Graham

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    C) You are exploding the $filename using "." and then checking if $ext matches "jpg", "jpeg", "png", and "gif" at one point in your code and checking if $ext matches '.jpg', '.jpeg', '.gif', and '.png' later in your code. Don't you suppose that might be a reason why the conditional tests are not working.
    CFMaBiSmAd already told you the answer. Remove the periods in the conditionals for the thumb and it will work. Also, you're generating a random number, and not using it. Maybe you want to append that to the time?


  •  

    Posting Permissions

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