Reputation: 49
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;
}
Upvotes: 1
Views: 2768
Reputation: 81
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
Upvotes: 2