Загрузка изображений PHP Проверка размеров - PullRequest
2 голосов
/ 16 февраля 2012

Как проверить размеры изображения после его загрузки и удалить его, если он не соответствует размерам, которые я хочу?

Так что после копания я обнаружил, что PHP не может делать измерения. Решение, которое я использую:

  1. Загрузить файл на сервер
  2. Используйте эту новую строку и отметьте
  3. Удалите его или продолжите загрузку, если оно не соответствует ширине и высоте

Это мой код. Может кто-нибудь показать мне, как проверить текущий файл на наличие размеров и как удалить папку и файл, если они не совпадают?

# 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;
    }

Ответы [ 4 ]

6 голосов
/ 16 февраля 2012
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;
}
3 голосов
/ 16 февраля 2012

Я использую функцию getimagesize с библиотекой GD .

Пример использования:

list($width, $height, $type, $attr) = @getimagesize($imageUri);

if (($height > 500) && ($width > 500)) {
    throw new Exception(sprintf('Invalid image: %d(h) and %d(w) are not within tolerable range', $height, $width));
}
1 голос
/ 12 декабря 2014

Для получение измерений n Очень просто и легко с temp_file

$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];
0 голосов
/ 16 февраля 2012

А как насчет этого?http://php.net/manual/en/function.getimagesize.php

http://php.net/manual/en/function.rmdir.php

Чтобы удалить папку, она должна быть пустой.

...