www.webdeveloper.com
Results 1 to 4 of 4

Thread: set max width and height when uploading image

  1. #1
    Join Date
    Feb 2012
    Posts
    15

    Unhappy set max width and height when uploading image

    This is the code I use to upload an image to my database and my ftp it works great but I want to be able to set the max width and max height, how can I do that?


    In header

    PHP Code:
    <?php
    session_start
    ();

    include(
    "connect_to_mysql.php"); 

    if (!isset(
    $_SESSION['sess_user'])){
      
    header("Location: index.php");
      exit;
    }

    if (isset(
    $_GET['logout'])){
      
    session_unset();
      
    session_destroy();
      
    header("Location: index.php");
      exit;
    }
    $upload_dir 'files/';

    $filetypes 'jpg,jpeg,gif,png,bmp';


    if(isset(
    $_POST['submit'])) {


    $userfile $_FILES['userfile']['name'];



    $types explode(',',$filetypes);
    $file explode('.',$_FILES['userfile']['name']);
    $extension $file[sizeof($file)-1];

    if(!
    in_array($extension,$types)) {

    if(
    $IngenBildSiffra == 1) {
    echo 
    "";
    } else {
    $notAllowedFileType 1;
    }
    }

    if(
    is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_dir.$_FILES['userfile']['name']);
    }

    $sql "INSERT INTO CMS_bild (image) VALUES ('$userfile')";
    mysql_query($sql);
    header("Location: botright.php?sent");
    exit();
    }

    ?>


    <script type="text/javascript"> 

    function submit_form(valVar) { 
        // Sätter vald åtgärd till 'true' 
        document.getElementById(valVar).value = 'true'; 
        document.getElementById('formName').submit(); 
    }
    <?php

    if ($added == 1){
    echo 
    "alert('Uppdateringen skedde utan problem.');\r\n";
    $header 1;
    }


    ?>
    // -->
    </script>

    On page


    PHP Code:
    <?php



    if(isset($_GET['sent'])) {
    echo 
    "Bilden <strong>".$_FILES['userfile']['name']."</strong> laddades upp utan problem!<br>";
    }
    if(isset(
    $_GET['imageexist'])) {
    echo 
    "Bilden existerar redan.<br>";
    }

    }
    ?>

    </td>
    </tr>
    </table>
    </div>
    <form method="post" action="<? echo "".$_SERVER['PHP_SELF'].""?>" enctype="multipart/form-data">

    <tr>
    <td>

    Bild
    </td> <td><input type="file" name="userfile" value="" /></td>
    </tr>



    <tr>
    <td colspan="2"><input type="submit" name="submit" / value="Lägg upp bild"></td>

    </tr>
    </table>
    </form>

  2. #2
    Join Date
    Apr 2010
    Posts
    227
    You can get height and width of an uploaded image by:
    list($width, $height, $type, $attr) = getimagesize($image);

    You can then test if greater than your maximums and either redraw the image for them or bounce back an error.

    Hope this helps

  3. #3
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Or you set the max size in your html or css file for the page, and all images will be resized regardless of actual size. You can also set limits on the file size in bytes in your upload program, so they don't take too long to load in a browser.

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Quote Originally Posted by Dasher View Post
    Or you set the max size in your html or css file for the page, and all images will be resized regardless of actual size. You can also set limits on the file size in bytes in your upload program, so they don't take too long to load in a browser.
    If the image needs to be 200 x 200, but what is uploaded is 1600 x 1600, sizing it down using HTML will look correct, but it is HIGHLY inefficient and not optimized for the web.

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