AvatarModel::validateImageFile PHP Method

validateImageFile() public static method

Validates the image Only accepts gif, jpg, png types
See also: http://php.net/manual/en/function.image-type-to-mime-type.php
public static validateImageFile ( ) : boolean
return boolean
    public static function validateImageFile()
    {
        if (!isset($_FILES['avatar_file'])) {
            Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_IMAGE_UPLOAD_FAILED'));
            return false;
        }
        // if input file too big (>5MB)
        if ($_FILES['avatar_file']['size'] > 5000000) {
            Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_UPLOAD_TOO_BIG'));
            return false;
        }
        // get the image width, height and mime type
        $image_proportions = getimagesize($_FILES['avatar_file']['tmp_name']);
        // if input file too small, [0] is the width, [1] is the height
        if ($image_proportions[0] < Config::get('AVATAR_SIZE') or $image_proportions[1] < Config::get('AVATAR_SIZE')) {
            Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_UPLOAD_TOO_SMALL'));
            return false;
        }
        // if file type is not jpg, gif or png
        if (!in_array($image_proportions['mime'], array('image/jpeg', 'image/gif', 'image/png'))) {
            Session::add('feedback_negative', Text::get('FEEDBACK_AVATAR_UPLOAD_WRONG_TYPE'));
            return false;
        }
        return true;
    }

Usage Example

Example #1
0
 /**
  * Create an avatar picture (and checks all necessary things too)
  * TODO decouple
  * TODO total rebuild
  */
 public static function createAvatar()
 {
     // check avatar folder writing rights, check if upload fits all rules
     if (AvatarModel::isAvatarFolderWritable() and AvatarModel::validateImageFile()) {
         // create a jpg file in the avatar folder, write marker to database
         $target_file_path = Config::get('PATH_AVATARS') . Session::get('user_id');
         AvatarModel::resizeAvatarImage($_FILES['avatar_file']['tmp_name'], $target_file_path, Config::get('AVATAR_SIZE'), Config::get('AVATAR_SIZE'), Config::get('AVATAR_JPEG_QUALITY'));
         AvatarModel::writeAvatarToDatabase(Session::get('user_id'));
         Session::set('user_avatar_file', AvatarModel::getPublicUserAvatarFilePathByUserId(Session::get('user_id')));
         Session::add('feedback_positive', Text::get('FEEDBACK_AVATAR_UPLOAD_SUCCESSFUL'));
     }
 }