how and where to insert image resize function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinpkl
    New Member
    • Oct 2008
    • 41

    how and where to insert image resize function

    hi
    i created a simple upload script which works fine and uploads image.

    i m not involving validation at present.
    Code:
    <?php
    if($_FILES['photo']['name'])
    {
        echo $file_name = $_FILES['photo']['name'];
        echo "<br>";
        echo $file_tmp =$_FILES['photo']['tmp_name'];
        
        move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$_FILES['photo']['name']);
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" enctype="multipart/form-data">
        Your Photo: <input type="file" name="photo" size="25" />
        <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
    </html>
    now i have this code that resizes the image
    Code:
    // The file
    $filename = 'images/s1.jpg';
    
    // Set a maximum height and width
    $width = 200;
    $height = 200;
    
    // Content type
    header('Content-Type: image/jpeg');
    
    // Get new dimensions
    list($width_orig, $height_orig) = getimagesize($filename);
    
    $ratio_orig = $width_orig/$height_orig;
    
    if ($width/$height > $ratio_orig) {
       $width = $height*$ratio_orig;
    } else {
       $height = $width/$ratio_orig;
    }
    
    // Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    
    // Output
    imagejpeg($image_p, null, 100);
    so i tried to combine them as they both work fine independently
    Code:
    <?php
    if($_FILES['photo']['name'])
    {
        echo $file_name = $_FILES['photo']['name'];
        echo "<br>";
        echo $file_tmp =$_FILES['photo']['tmp_name'];
        
        //resize function starts
        $filename = 'images/'.$file_name;
    
        // Set a maximum height and width
        $width = 200;
        $height = 200;
    
        // Get new dimensions
        list($width_orig, $height_orig) = getimagesize($filename);
    
        $ratio_orig = $width_orig/$height_orig;
    
        if ($width/$height > $ratio_orig) {
       $width = $height*$ratio_orig;
        } else {
        $height = $width/$ratio_orig;
        }
    
        // Resample
        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    
        // Output
        imagejpeg($image_p, null, 100);
        // resize function ends
        
        move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$_FILES['photo']['name']);
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" enctype="multipart/form-data">
        Your Photo: <input type="file" name="photo" size="25" />
        <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
    </html>
    so i have two questions related to above combined code

    1) The resample or resize function code will come before or after "move_uploaded_ file" function ??

    2) what will i write in the "move_uploaded_ file" function so that resized thumbnail gets uploaded ??

    vineet
Working...