Results 1 to 4 of 4

Thread: upload and resize

  1. #1
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default upload and resize

    Hi,

    I've created this function:

    PHP Code:
    function resizeImage($originalImage){
        list(
    $width$height) = getimagesize($originalImage);
        
    $new_width 54;
        
    $new_height 54;

        
    // Resize the original image
        
    $imageResized imagecreatetruecolor($new_width$new_height);
        
    $imageTmp     imagecreatefromjpeg ($originalImage);
        
    imagecopyresampled($imageResized$imageTmp0000$new_width$new_height$width$height);
        return 
    $imageResized;

    that enables me to resize images to size of 54*54, however I don't know how to send the function the image with php, and upload it to the server.

    Any help?
    Thanks

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Look up any tutorial on uploading a file. There are many. In short you will use action="POST" for the form and a <input type="file"> to choose the image. Then submit that to your PHP script. After that it gets a little complex, but basically it will have a filename available to you (based on your settings), and you can do security things like only allow .jpg, etc (best to only allow certain types, rather than trying to block all bad types-- you won't be able to predict everything dangerous).
    Then once that image is on your server, you can either save it to the server as a file (actually, move the temporary file) or use it as is. Either way you will supply the path and create a GD image using imagecreatefromJPEG() (or -GIF, -PNG, or even google for imagecreatefromBMP() which is not included by default).
    That will return a GD image: $im = imagecreatefromJPEG().
    Then you can call the function above with $im as the parameter.

    Take each step slowly and separately and this should not be too difficult to work out.

    After that you can save the resulting image as a file or display it directly, as you wish. Since it is immediately upon the upload, it is probably better to save it as a file and embed that new link into an html page so the user has some more feedback than just viewing an image.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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

    lord22 (11-12-2009)

  4. #3
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default

    Thanks
    Last edited by lord22; 11-12-2009 at 09:59 PM.

  5. #4
    Join Date
    Nov 2009
    Location
    Maputo, Mozambique, Africa
    Posts
    14
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    hi lord22

    this works for me...

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <input name="new_image" id="new_image" size="30" type="file">
    <button name="submit" type="submit">Upload/Resize Image</button>
    </form>
    <?php
    if(isset($_POST['submit'])){
    if (isset ($_FILES['new_image'])){
    $imagename = $_FILES['new_image']['name'];
    $source = $_FILES['new_image']['tmp_name'];
    $target = "./myphotos/".$imagename;
    move_uploaded_file($source, $target);

    $imagepath = $imagename;
    $save = "./myphotos/" . $imagepath; //This is the new file you saving
    $file = "./myphotos/" . $imagepath; //This is the original file

    list($width, $height) = GetImageSize($file) ;

    $modwidth = 500;
    $diff = $width / $modwidth;

    $modheight = $height / $diff;
    $tn = imagecreate($modwidth, $modheight) ;
    $image = imagecreatefromjpeg($file) ;
    imagecopyresized($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
    imagejpeg($tn, $save, 500) ;

    echo "Resized image:<br><br> <img src='./myphotos/".$imagepath."'>";
    }
    }
    ?>

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
  •