Пример #1
0
function um_resize($ori)
{
    if (preg_match('/^http:\\/\\/[a-zA-Z0-9]+/', $ori)) {
        return $ori;
    }
    $info = um_getImageInfo(AVATARS_PATH . $ori);
    if ($info) {
        //上传图片后切割的最大宽度和高度
        $dst_width = 100;
        $dst_height = 100;
        $scrimg = AVATARS_PATH . $ori;
        if ($info['type'] == 'jpg' || $info['type'] == 'jpeg') {
            $im = imagecreatefromjpeg($scrimg);
        }
        if ($info['type'] == 'gif') {
            $im = imagecreatefromgif($scrimg);
        }
        if ($info['type'] == 'png') {
            $im = imagecreatefrompng($scrimg);
        }
        if ($info['type'] == 'bmp') {
            $im = imagecreatefromwbmp($scrimg);
        }
        if ($info['width'] <= $dst_width && $info['height'] <= $dst_height) {
            return;
        } else {
            if ($info['width'] > $info['height']) {
                $height = intval($info['height']);
                $width = $height;
                $x = ($info['width'] - $width) / 2;
                $y = 0;
            } else {
                $width = intval($info['width']);
                $height = $width;
                $x = 0;
                $y = ($info['height'] - $height) / 2;
            }
        }
        $newimg = imagecreatetruecolor($width, $height);
        imagecopy($newimg, $im, 0, 0, $x, $y, $info['width'], $info['height']);
        $scale = $dst_width / $width;
        $target = imagecreatetruecolor($dst_width, $dst_height);
        $final_w = intval($width * $scale);
        $final_h = intval($height * $scale);
        imagecopyresampled($target, $newimg, 0, 0, 0, 0, $final_w, $final_h, $width, $height);
        imagejpeg($target, AVATARS_PATH . $ori);
        imagedestroy($im);
        imagedestroy($newimg);
        imagedestroy($target);
    }
    return;
}
Пример #2
0
     $ext = pathinfo($_FILES['file']['name']);
     $ext = strtolower($ext['extension']);
     $tempFile = $_FILES['file']['tmp_name'];
     $targetPath = AVATARS_PATH;
     if (!is_dir($targetPath)) {
         mkdir($targetPath, 0755, true);
     }
     $new_file_name = 'avatar-' . $user_id . '.' . $ext;
     $targetFile = $targetPath . $new_file_name;
     if (!in_array($ext, $filetype)) {
         $message = __('仅允许上传JPG、GIF、BMP、PNG图片', 'um');
     } else {
         move_uploaded_file($tempFile, $targetFile);
         if (!file_exists($targetFile)) {
             $message = __('图片上传失败', 'um');
         } elseif (!($imginfo = um_getImageInfo($targetFile))) {
             $message = __('图片不存在', 'um');
         } else {
             $img = $new_file_name;
             um_resize($img);
             $message = __('头像上传成功', 'um');
             $update_user_avatar = update_user_meta($user_id, 'um_avatar', 'customize');
             $update_user_avatar_img = update_user_meta($user_id, 'um_customize_avatar', $img);
         }
     }
 } else {
     $update_user_avatar = update_user_meta($user_id, 'um_avatar', sanitize_text_field($_POST['avatar']));
     if (!is_wp_error($update_user_id) || $update_user_avatar) {
         $message = __('基本信息已更新', 'um');
     }
 }