Results 1 to 5 of 5

Thread: php image upload and resize

  1. #1
    Join Date
    Jan 2012
    Location
    India
    Posts
    45
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default php image upload and resize

    i found this code and just trying to save the resized image to folder album but it is saving original image, how to save resized image ?


    PHP Code:
    <?php
    session_start
    ();
    require (
    "config.php");
    error_reporting(5);
    $id $_SESSION['id'];

    $pals $_SESSION['myses'];
    if (
    $pals!=yes)
    {
    header("location:login.php?msgLogin=Login to access this page");
    exit;
    }

    // Check if file was uploaded ok
    if( ! is_uploaded_file($_FILES['image']['tmp_name']) || $_FILES['image']['error'] !== UPLOAD_ERR_OK)
    {
       
        
    header("location:upload_photo.php?msg=Photo not uploaded. Possibly too large.");
        exit;
    }

    // Create image from file
    switch(strtolower($_FILES['image']['type']))
    {
        case 
    'image/jpeg':
            
    $image imagecreatefromjpeg($_FILES['image']['tmp_name']);
            break;
        case 
    'image/png':
            
    $image imagecreatefrompng($_FILES['image']['tmp_name']);
            break;
        case 
    'image/gif':
            
    $image imagecreatefromgif($_FILES['image']['tmp_name']);
            break;
        default:
            
    //exit('Unsupported type: '.$_FILES['image']['type']);
            
    header("location:upload_photo.php?msg=Unsupported image type");
            exit;
    }

    // Target dimensions
    $max_width 800;
    $max_height 600;

    // Get current dimensions
    $old_width  imagesx($image);
    $old_height imagesy($image);

    // Calculate the scaling we need to do to fit the image inside our frame
    $scale      min($max_width/$old_width$max_height/$old_height);

    // Get the new dimensions
    $new_width  ceil($scale*$old_width);
    $new_height ceil($scale*$old_height);

    // Create new empty image
    $new imagecreatetruecolor($new_width$new_height);

    // Resize old image into new
    imagecopyresampled($new$image,
        
    0000,
        
    $new_width$new_height$old_width$old_height);
        
    // Catch the imagedata
    ob_start();
    imagejpeg($newNULL90);
    $data ob_get_clean();

    // Destroy resources
    imagedestroy($image);
    imagedestroy($new);

    // Set new content-type and status code
    //header("Content-type: image/jpeg", true, 200);

    // Output data
    //echo $data;    


    $pic basename($_FILES['image']['name']);
        
    $arr2 explode("."$pic);
        
    $pic_ext $arr2[sizeof($arr2) -];


    $uploaddir "album/";
    $uploaddir $uploaddir time() . "." $pic_ext;
        
        
        if(
    move_uploaded_file$_FILES['image']['tmp_name'],  $uploaddir))
        {
                
                
    // saving image info in the database
                
    $sql "INSERT INTO album (img, user_id)VALUES('$uploaddir', '$id')";
                
    $res mysql_query($sql);
                
                
    ////////////////// redirecting sucessfull msg
                
    header("location:upload_photo.php?msg=Photo sucessfully uploaded, upload another photo.");
                exit;
        }
        else
        {
            
            
    header("location:upload_photo.php?msg=Error uploading your photo, please try again.");
            exit();
        }

    ?>
    Last edited by ankush; 07-17-2012 at 06:52 PM. Reason: resolved

  2. #2
    Join Date
    Jan 2012
    Location
    India
    Posts
    45
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default

    any one please help me

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,258
    Thanks
    96
    Thanked 104 Times in 102 Posts

    Default

    What was the solution?
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    The solution is as simple as changing the file name parameter in the imagejpeg() function to whatever the desired file name is.

    It was:
    PHP Code:
    imagejpeg($newNULL90); 
    When it should be:
    PHP Code:
    imagejpeg($new"myfile.jpg"90); 
    - Josh

  5. #5
    Join Date
    Jan 2012
    Location
    India
    Posts
    45
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default

    i was destroying variable before and then trying to save it and saving in a wrong way like move_uploaded_file

    but i tried this

    PHP Code:
    imagejpeg($new$uploaddir100); 
    and it is working now

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
  •