1

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.

Community
  • 1
  • 1

1 Answers1

1

This error explains the problem:

Warning: getimagesize() expects parameter 1 to be string, array given in /Users/matt/Desktop/Likers/upload.php on line 34

You are giving the whole $_FILES["myFile"] instead only the filename. Check line #27:

$filename = $myFile;

It should be:

$filename = $myFile['tmp_name'];

Hope it helps :)

Ian Mustafa
  • 598
  • 4
  • 18
  • It seems to resize it, and displays it. I just can't get and display its name. –  Jul 05 '14 at 23:39
  • Plus it only displays resized, it won't store resized in the uploads folder. When I try `$filename = $name;`, I don't get an image –  Jul 05 '14 at 23:41
  • So it doesn't successfully upload the file to upload dir? – Ian Mustafa Jul 05 '14 at 23:43
  • 1
    It uploads, but not resized. –  Jul 05 '14 at 23:43
  • @user3100859 Another mistake by me. We shouldn't use a safe name yet, since the filename will be used in image processing. Have you try `$filename = $myFile['tmp_name'];`? – Ian Mustafa Jul 06 '14 at 00:01
  • I just tried `$filename = $myFile['tmp_name'];` and it still won't upload resized. –  Jul 06 '14 at 00:07
  • Weird. Your code is (almost) the same with mine. Try `$image = imagecreatefromstring(file_get_contents($filename);` instead of `$image = imagecreatefromjpeg($filename);` – Ian Mustafa Jul 06 '14 at 00:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56827/discussion-between-user3100859-and-ian-mustafa). –  Jul 06 '14 at 00:21