Click here to Skip to main content
16,018,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to resize an image automatically when a user upload a profile picture. This is the image upload PHP
PHP
<?php
$user=$_SESSION['dbUser']['username'];
$db=new mysqli('host','username','password','database');
if($db->connect_errno){
echo $db->connect_error;}
$pull="select * from users where user='$user'";

$allowedExts = array("jpg", "jpeg", "gif", "png","JPG","PNG");
$extension = @end(explode(".", $_FILES["file"]["name"]));
if(isset($_POST['pupload'])){
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/JPG")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/PNG"))
&& ($_FILES["file"]["size"] < 200000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {

    echo "Uploaded Successully<br>";
    echo "It may take up to half an hour to update.<br><br>";

    if (file_exists("upload/" . $_SESSION['dbUser']['username']))
      {
      unlink("upload/" . $_SESSION['dbUser']['username'].".".$ext);
      }
    else
      {
          $pic=$_FILES["file"]["name"];
            $conv=explode(".",$pic);
            $ext=$conv['1'];

      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_SESSION['dbUser']['username'].".jpg");
      $url=$user.".jpg";
    
      $query="update picture set url='$url', lastUpload=now() where user='$user'";
      if($upl=$db->query($query)){
		header('Location: ' . $_SERVER['HTTP_REFERER']);
              }
      }
    }
  }
else
  {
  echo "File Size Limit Crossed 200 KB Use Picture Size less than 200 KB";

  }
}
?>
Posted

1 solution

It's not clear what do you mean by "when". When a file is not yet uploaded, you simply don't have it on the server part. Hence, you need to get the uploaded file and resize it.

For image manipulations, you can use PHP binding of ImageMagic: http://php.net/imagick[^].

This is the resize method: http://www.php.net/manual/en/imagick.resizeimage.php[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900