code

Hello, there is a code that takes a picture from the input, cuts off the excess, removes the encoding and saves the picture in the desired folder with the extension jpg. I have a question, how can I check if a picture is an image? Ie only .png .jpeg .jpg .bmp

    1 answer 1

    You need:

    $info = getimagesize($this->file['tmp_name']); $info['mime']; 

    I give an example of the image validation class. I think that everything is clear.

     /** * Класс для работы с изрображениями */ class Image { private $file; private $error; private $allow = array('png' => 'image/png', 'gif' => 'image/gif', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg'); public function __construct($file) { $this->file = $file; $this->error = NULL; } public function __destruct() { if ($this->file && $this->file['tmp_name']) { unlink($this->file['tmp_name']); } } public function validate() { $this->error = NULL; if (!$this->file['tmp_name']) { $this->error = 'Файл не загружен (слишком большой)'; return FALSE; } // проверка непосредственно расширения файла $file_info = pathinfo($this->file['name']); if (!isset($file_info['extension']) || !in_array(mb_strtolower($file_info['extension'], 'UTF-8'), array_keys($this->allow))) { $this->error = 'Разрешено загружать только '.implode(', ', array_keys($this->allow)); return FALSE; } // проверка типа файла и mime информации об изображении $info = getimagesize($this->file['tmp_name']); if (!in_array($this->file['type'], $this->allow) || !in_array($info['mime'], $this->allow)) { $this->error = 'Разрешено загружать только '.implode(', ', array_keys($this->allow)); return FALSE; } return TRUE; } }