0

I am trying to resize images before upload. I added the line $img = resize_image($_FILES['Image']['tmp_name'], 200, 200); to the code below. The code is working properly to upload. Now I am just trying to adjust the size of the image. I am not sure how to take the return value $img and save it with the move_uploaded_file. When I try and replace $_FILES['Image']['tmp_name] with $img, I get a parse error. I also added the resize_image function so you can see what it is exactly returning.

    {
            $image_name= $_FILES['Image']['name'];
            $img = resize_image($_FILES['Image']['tmp_name'], 200, 200);
        $image_name =  $picCounter .$firstName .$image_name;
        move_uploaded_file( $_FILES['Image']['tmp_name'], "IMG/$image_name");
        correctImageOrientation("IMG/$image_name");

        $image_path = "/IMG/" .$image_name;

        $picCounter = $picCounter++;
        $sql = ("UPDATE LoginInformation SET PicCounter = PicCounter + 1 WHERE Email = '" . $useremail . "'" );
        mysqli_query($conn, $sql);
    }
    else
    {
        $image_path = "/IMG/square-image.png";
    }

here is the resize function

function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}
Amin.MasterkinG
  • 805
  • 1
  • 12
  • 23
BEP
  • 39
  • 6

1 Answers1

1

You cannot resize image before upload with PHP, PHP is server side language and will work only once the image is uploaded not before.

You need to do this via JavaScript. See this: Use HTML5 to resize an image before upload

Kalki Truth
  • 51
  • 1
  • 5
  • Roger that and thanks. I will try this solution. I may have more questions for you – BEP May 01 '19 at 11:41
  • so I have been thinking back to my PHP, you say I can't resize before upload. When I say upload, I mean the image has already been passed to server side (using Ajax) and now I am in my php file and pushing the image to my database. the push to the database is what I am calling upload. Looks like I can use php before I actually store it in the database to resize it. No? – BEP May 03 '19 at 03:18