1

is there an easy way to resize an image before uploading. Been looking for a while now but nothing seems to be working well for me. I want to resize everything to ratio and only resize if something is bigger then lets say 150. Height should move down so the image still looks as it should. I have the following code which works for uploading and renaming but now i want to implement a resize on top of this

$uploadDir = 'images/'; //Image Upload Folder

$fileName = $_FILES['file-0']['name'];
$tmpName = $_FILES['file-0']['tmp_name'];
$fileSize = $_FILES['file-0']['size'];
$fileType = $_FILES['file-0']['type'];


$temp = explode(".", $fileName);
$newfilename = $id . round(microtime(true)) . '.' . end($temp);

$result = move_uploaded_file($_FILES["file-0"]["tmp_name"], "images/" . $newfilename);
$filePath = $uploadDir . $newfilename;


if (!$result) {
    echo "Error uploading file";
    exit;
}

$query = " 
            update
                pictures SET picture = '$filePath' Where
                id = :id
        ";
$query_params = array(
    ':id' => $id
);



try {
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
} catch (PDOException $ex) {
    die("Failed to run query: " . $ex->getMessage());
}
$row = $stmt->fetch();
Wouter
  • 137
  • 10
  • You want to resize before uploading, client side? If so you probably need to draw the image to a canvas in javascript and post to the server. there are already answers on here how to do that – andrew Oct 02 '16 at 18:49

1 Answers1

1

You can use php class from the address below. I tried and it works like a charm. It resizes images on the fly.

http://www.bitrepository.com/resize-an-image-keeping-its-aspect-ratio-using-php-and-gd.html

You can check this link below too, to have an idea:

PHP upload and resize image

Community
  • 1
  • 1
Blackcoat77
  • 1,574
  • 1
  • 21
  • 31