Skip to main content
deleted 164 characters in body
Source Link
Uwe Keim
  • 40.9k
  • 61
  • 193
  • 310
if ($_FILES) {
                //Put file properties into variables
                $file_name = $_FILES['profile-image']['name'];
                $file_size = $_FILES['profile-image']['size'];
                $file_tmp_name = $_FILES['profile-image']['tmp_name'];
                
                //Determine filetype
                switch ($_FILES['profile-image']['type']) {
                    case 'image/jpeg': $ext = "jpg"; break;
                    case 'image/png': $ext = "png"; break;
                    default: $ext = ''; break;
                }
                
                if ($ext) {
                    //Check filesize
                    if ($file_size < 500000) {
                        //Process file - clean up filename and move to safe location
                        $n = "$file_name";
                        $n = ereg_replace("[^A-Za-z0-9.]", "", $n);
                        $n = strtolower($n);
                        $n = "avatars/$n";
                        move_uploaded_file($file_tmp_name, $n);
                    } else {
                        $bad_message = "Please ensure your chosen file is less than 5MB.";
                    }
                } else {
                    $bad_message = "Please ensure your image is of filetype .jpg or.png.";
                }
            } 

$query = "INSERT INTO users (image) VALUES ('$n')";
mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
if ($_FILES) {
                //Put file properties into variables
                $file_name = $_FILES['profile-image']['name'];
                $file_size = $_FILES['profile-image']['size'];
                $file_tmp_name = $_FILES['profile-image']['tmp_name'];
                
                //Determine filetype
                switch ($_FILES['profile-image']['type']) {
                    case 'image/jpeg': $ext = "jpg"; break;
                    case 'image/png': $ext = "png"; break;
                    default: $ext = ''; break;
                }
                
                if ($ext) {
                    //Check filesize
                    if ($file_size < 500000) {
                        //Process file - clean up filename and move to safe location
                        $n = "$file_name";
                        $n = ereg_replace("[^A-Za-z0-9.]", "", $n);
                        $n = strtolower($n);
                        $n = "avatars/$n";
                        move_uploaded_file($file_tmp_name, $n);
                    } else {
                        $bad_message = "Please ensure your chosen file is less than 5MB.";
                    }
                } else {
                    $bad_message = "Please ensure your image is of filetype .jpg or.png.";
                }
            }
$query = "INSERT INTO users (image) VALUES ('$n')";
mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
if ($_FILES) {
    //Put file properties into variables
    $file_name = $_FILES['profile-image']['name'];
    $file_size = $_FILES['profile-image']['size'];
    $file_tmp_name = $_FILES['profile-image']['tmp_name'];
    
    //Determine filetype
    switch ($_FILES['profile-image']['type']) {
        case 'image/jpeg': $ext = "jpg"; break;
        case 'image/png': $ext = "png"; break;
        default: $ext = ''; break;
    }
    
    if ($ext) {
        //Check filesize
        if ($file_size < 500000) {
            //Process file - clean up filename and move to safe location
            $n = "$file_name";
            $n = ereg_replace("[^A-Za-z0-9.]", "", $n);
            $n = strtolower($n);
            $n = "avatars/$n";
            move_uploaded_file($file_tmp_name, $n);
        } else {
            $bad_message = "Please ensure your chosen file is less than 5MB.";
        }
    } else {
        $bad_message = "Please ensure your image is of filetype .jpg or.png.";
    }
} 

$query = "INSERT INTO users (image) VALUES ('$n')";
mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
deleted 6 characters in body
Source Link
Adelin
  • 19.2k
  • 27
  • 133
  • 205

I'm wantingI want to write some PHP code whichthat automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I'm a relative PHP novice).

So far, I've got images uploading successfully, filetypes being recognisedrecognized and names cleaned up, but I'd like to add the resize functionality into the code. For example, I've got a test image whichthat is 2.3MB, and 1331x1331 in dimension, and I'd like the code to size it down, which I'm guessing will dramatically compress the filesizefile size of the image, too.

I'm wanting to write some PHP code which automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I'm a relative PHP novice).

So far, I've got images uploading successfully, filetypes being recognised and names cleaned up, but I'd like to add the resize functionality into the code. For example, I've got a test image which is 2.3MB, and 1331x1331 in dimension, and I'd like the code to size it down, which I'm guessing will dramatically compress the filesize of the image, too.

I want to write some PHP code that automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I'm a relative PHP novice).

So far, I've got images uploading successfully, filetypes being recognized and names cleaned up, but I'd like to add the resize functionality into the code. For example, I've got a test image that is 2.3MB, and 1331x1331 in dimension, and I'd like the code to size it down, which I'm guessing will dramatically compress the file size of the image, too.

Question Protected by CommunityBot
fixed formatting
Link
Vadim Kotov
  • 8.3k
  • 8
  • 51
  • 63

resize Resize image in PHP

Source Link
Alex Ryans
  • 1.9k
  • 5
  • 23
  • 32
Loading