Wouter
Wouter

Reputation: 137

Resize image uploading php

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();

Upvotes: 1

Views: 73

Answers (1)

Blackcoat77
Blackcoat77

Reputation: 1625

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

Upvotes: 1

Related Questions