01// 剪切图片
                    02function cut_img($src_image, $out_image = null, int $new_width = null, int $new_height = null, $img_quality = 90)
                    03{
                    04// 输出地址
                    05if (! $out_image)
                    06$out_image = $src_image;
                    07
                    08// 读取配置文件设置
                    09if (! $new_width && ! $new_height)
                    10return;
                    11
                    12// 获取图片属性
                    13list ($width, $height, $type, $attr) = getimagesize($src_image);
                    14switch ($type) {
                    15case 1:
                    16$img = imagecreatefromgif($src_image);
                    17break;
                    18case 2:
                    19$img = imagecreatefromjpeg($src_image);
                    20break;
                    21case 3:
                    22$img = imagecreatefrompng($src_image);
                    23break;
                    24}
                    25
                    26// 不限定是等比例缩放
                    27if (! $new_width) {
                    28$new_width = floor($width * ($new_height / $height));
                    29}
                    30if (! $new_height) {
                    31$new_height = floor($height * ($new_width / $width));
                    32}
                    33// 创建画布
                    34$new_img = imagecreatetruecolor($new_width, $new_height);
                    35
                    36// 创建透明画布,避免黑色
                    37if ($type == 1 || $type == 3) {
                    38$color = imagecolorallocate($new_img, 255, 255, 255);
                    39imagefill($new_img, 0, 0, $color);
                    40imagecolortransparent($new_img, $color);
                    41}
                    42
                    43
                    44// 先缩放
                    45$scale = max($new_width / $width, $new_height / $height);
                    46$scale_width = floor($scale * $width);
                    47$scale_height = floor($scale * $height);
                    48$scale_img = imagecreatetruecolor($scale_width, $scale_height); // 创建画布
                    49if(function_exists("ImageCopyResampled")) {
                    50imagecopyresampled($scale_img, $img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);
                    51} else {
                    52imagecopyresized($scale_img, $img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);
                    53}
                    54//再裁剪
                    55$start_x = ($scale_width - $new_width) / 2;
                    56$start_y = ($scale_height - $new_height) / 2;
                    57
                    58//拷贝剪切的图像数据到画板,生成剪切图像
                    59imagecopy($new_img, $scale_img, 0, 0, $start_x, $start_y, $scale_width, $scale_height);
                    60
                    61check_dir(dirname($out_image), true); // 检查输出目录
                    62
                    63switch ($type) {
                    64case 1:
                    65imagegif($new_img, $out_image, $img_quality);
                    66break;
                    67case 2:
                    68imagejpeg($new_img, $out_image, $img_quality);
                    69break;
                    70case 3:
                    71imagepng($new_img, $out_image, $img_quality / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9
                    72break;
                    73default:
                    74imagejpeg($new_img, $out_image, $img_quality);
                    75}
                    76imagedestroy($new_img);
                    77imagedestroy($img);
                    78return true;
                    79}