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 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Location
    India
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Unhappy 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 anki; 07-17-2012 at 08:55 PM. Reason: solved


 

Posting Permissions

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