resize image problem

Discussion in 'PHP' started by adsegzy, Aug 28, 2010.

  1. #1
    Hello friends,

    please am having problem with my image resize code the code is as below;

    $originalImage = $_FILES["file"]["name"];
    $toWidth = 300;
    $toHeight = 300;
    
    function resizeImage($originalImage,$toWidth,$toHeight){
    
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;
    
    // Recalculate new size with default ratio
    if ($yscale>$xscale){
    $new_width = round($width * (1/$yscale));
    $new_height = round($height * (1/$yscale));
    }
    else {
    $new_width = round($width * (1/$xscale));
    $new_height = round($height * (1/$xscale));
    }
    
    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    return $imageResized;
    }
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "../upload/" . $imageResized);
    $path2= "../upload/" . $imageResized;
    PHP:
    but the picture is not resized, and if i use

    $image = resizeImage()
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "../upload/" . $image);
    $path2= "../upload/" . $image;
    PHP:
    no picture will be uploaded to my upload folder.

    please what can i do or which other lesser stress way can i resize my picture.
     
    adsegzy, Aug 28, 2010 IP
  2. .TIEU

    .TIEU Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try

    
    $image = resizeImage($originalImage,300,300);
    move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $image);
    $path2= "../upload/" . $image;
    
    Code (markup):
     
    .TIEU, Aug 28, 2010 IP
  3. subashchand

    subashchand Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    Solution for your site can be added by me and i'm available immediatley to you.My hourly rate is $8USD

    I have more than 5 Years of experience in web development.
    Feel free to contact me :
    Messenger ID:
    Gtalk:sanjeev786@gmail.com
    Yahoo:
    Skype: mrsanjeev.chauhan

    Looking forward to hear you.

    Thanks and Regards
    Subash
     
    subashchand, Aug 29, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    function resizeImage($originalImage,$toWidth,$toHeight){
    
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;
    
    // Recalculate new size with default ratio
    if ($yscale>$xscale){
    $new_width = round($width * (1/$yscale));
    $new_height = round($height * (1/$yscale));
    }
    else {
    $new_width = round($width * (1/$xscale));
    $new_height = round($height * (1/$xscale));
    }
    
    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    return $imageResized;
    }
    
    $toWidth = 300;
    $toHeight = 300;
    
    $imageResized = resizeImage($_FILES['file']['tmp_name'],$toWidth,$toHeight);
    
    file_put_contents("../upload/{$_FILES['file']['name']}", $imageResized);
    
    PHP:
    Try that.
     
    danx10, Aug 29, 2010 IP