So I have a image upload script, the issue is I want to resize the image before uploading. Here's the code I have right now,
<?php
session_start();
$username = $_SESSION['user'];
include_once 'db.php';
define("UPLOAD_DIR", "uploads/");
$fileType = exif_imagetype($_FILES["myFile"]["tmp_name"]);
$allowed = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
if (!in_array($fileType, $allowed)) {
header('Location: edit.php');
exit();
}
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "<p>An error occurred.</p>";
exit;
}
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
//Start resize
$filename = $myFile;
$width = 200;
$height = 200;
header('Content-Type: image/jpeg');
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);
// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
$upload_url = 'http://localhost/uploads/'.$name;
$stmt = $con->prepare("UPDATE users SET profile_picture=:picture WHERE username=:username;");
$stmt->bindValue(':picture', $upload_url, PDO::PARAM_STR);
$stmt->bindValue(':username', $username, PDO::PARAM_STR);
$stmt->execute();
}
?>
<a href="<?php echo $upload_url ?>">Click here</a>
And this is what I get
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Users/matt/Desktop/Likers/upload.php:1) in /Users/matt/Desktop/Likers/upload.php on line 2
Warning: Cannot modify header information - headers already sent by (output started at /Users/matt/Desktop/Likers/upload.php:1) in /Users/matt/Desktop/Likers/upload.php on line 32
Warning: getimagesize() expects parameter 1 to be string, array given in /Users/matt/Desktop/Likers/upload.php on line 34
Warning: Division by zero in /Users/matt/Desktop/Likers/upload.php on line 37
Warning: imagecreatetruecolor(): Invalid image dimensions in /Users///Desktop/Likers/upload.php on line 45
Warning: imagecreatefromjpeg() expects parameter 1 to be string, array given in /Users///Desktop/Likers/upload.php on line 46
Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /Users///Desktop/Likers/upload.php on line 47
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /Users///Desktop/Likers/upload.php on line 49
I'm not really sure what to do. Am I doing this correctly? Is there a better way to do this? Any help would be great.