AvatarModel::resizeAvatarImage PHP Метод

resizeAvatarImage() публичный статический Метод

Only works with gif, jpg and png file types. If you want to change this also have a look into method validateImageFile() inside this model. TROUBLESHOOTING: You don't see the new image ? Press F5 or CTRL-F5 to refresh browser cache.
public static resizeAvatarImage ( string $source_image, string $destination, integer $final_width = 44, integer $final_height = 44 ) : boolean
$source_image string The location to the original raw image
$destination string The location to save the new image
$final_width integer The desired width of the new image
$final_height integer The desired height of the new image
Результат boolean success state
    public static function resizeAvatarImage($source_image, $destination, $final_width = 44, $final_height = 44)
    {
        $imageData = getimagesize($source_image);
        $width = $imageData[0];
        $height = $imageData[1];
        $mimeType = $imageData['mime'];
        if (!$width || !$height) {
            return false;
        }
        switch ($mimeType) {
            case 'image/jpeg':
                $myImage = imagecreatefromjpeg($source_image);
                break;
            case 'image/png':
                $myImage = imagecreatefrompng($source_image);
                break;
            case 'image/gif':
                $myImage = imagecreatefromgif($source_image);
                break;
            default:
                return false;
        }
        // calculating the part of the image to use for thumbnail
        if ($width > $height) {
            $verticalCoordinateOfSource = 0;
            $horizontalCoordinateOfSource = ($width - $height) / 2;
            $smallestSide = $height;
        } else {
            $horizontalCoordinateOfSource = 0;
            $verticalCoordinateOfSource = ($height - $width) / 2;
            $smallestSide = $width;
        }
        // copying the part into thumbnail, maybe edit this for square avatars
        $thumb = imagecreatetruecolor($final_width, $final_height);
        imagecopyresampled($thumb, $myImage, 0, 0, $horizontalCoordinateOfSource, $verticalCoordinateOfSource, $final_width, $final_height, $smallestSide, $smallestSide);
        // add '.jpg' to file path, save it as a .jpg file with our $destination_filename parameter
        imagejpeg($thumb, $destination . '.jpg', Config::get('AVATAR_JPEG_QUALITY'));
        imagedestroy($thumb);
        if (file_exists($destination)) {
            return true;
        }
        return false;
    }

Usage 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'));
     }
 }