Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Image Upload Checking Dimensions

How can I check the dimensions of an image after it has uploaded and delete it if it does not match the dimensions I want?

So after digging around I find PHP cannot do dimensions. The solution I am following is:

  1. Upload the file to the server
  2. Use that new string and check
  3. Delete it or continue with upload if it does not match width and height

This is my code. Can someone show me how to check the current file for dimensions and how to delete the folder and file if not matching?

# create our temp dir
    mkdir("./uploads/temp/".$user."/".$mx."/".$hash."/", 0777, true);
    # upload dir settup
    $uploaddir='./uploads/temp/'.$user.'/'.$mx.'/'.$hash.'/';
    $file=$uploaddir . basename($_FILES['file']['name']);

    # upload the file first
    if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) {
        # ok so check the height and width
        # if it is not the width and height assigned delete image and folder
        if (image height= and width =){
            unlink($file);
            rmdir($uploaddir);
            $result=0;
        } else {
        # image matches height and width message ok
            $result=1;
        }
    } else {
        # error uploading
        $result=$errormsg;
    }
like image 882
TheBlackBenzKid Avatar asked Mar 29 '26 22:03

TheBlackBenzKid


1 Answers

mkdir("./uploads/temp/".$user."/".$mx."/".$hash."/", 0777, true);
# upload dir settup
$uploaddir='./uploads/temp/'.$user.'/'.$mx.'/'.$hash.'/';
$file=$uploaddir . basename($_FILES['file']['name']);

# upload the file first
if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) {
    # ok so check the height and width
    # if it is not the width and height assigned delete image and folder
    $size = getimagesize($files);
    $maxWidth = 500;
    $maxHeight = 500;
    if ($size[0] > $maxWidth || $size[1] > $maxHeight)
    {
        unlink($file);
        rmdir("./uploads/temp/".$user."/".$mx."/".$hash."/");
        rmdir("./uploads/temp/".$user."/".$mx."/");
        rmdir("./uploads/temp/".$user."/");
    }
    else
        $result=1;
    end if
} else {
    # error uploading
    $result=$errormsg;
}
like image 65
derekaug Avatar answered Apr 02 '26 02:04

derekaug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!