02 | function cut_img($src_image, $out_image = null, int $new_width = null, int $new_height = null, $img_quality = 90) |
06 | $out_image = $src_image; |
09 | if (! $new_width && ! $new_height) |
13 | list ($width, $height, $ type , $attr) = getimagesize($src_image); |
16 | $img = imagecreatefromgif($src_image); |
19 | $img = imagecreatefromjpeg($src_image); |
22 | $img = imagecreatefrompng($src_image); |
28 | $new_width = floor($width * ($new_height / $height)); |
31 | $new_height = floor($height * ($new_width / $width)); |
34 | $new_img = imagecreatetruecolor($new_width, $new_height); |
37 | if ($ type == 1 || $ type == 3) { |
38 | $color = imagecolorallocate($new_img, 255, 255, 255); |
39 | imagefill($new_img, 0, 0, $color); |
40 | imagecolortransparent($new_img, $color); |
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); // 创建画布 |
49 | if (function_exists( "ImageCopyResampled" )) { |
50 | imagecopyresampled($scale_img, $img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height); |
52 | imagecopyresized($scale_img, $img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height); |
55 | $start_x = ($scale_width - $new_width) / 2; |
56 | $start_y = ($scale_height - $new_height) / 2; |
59 | imagecopy($new_img, $scale_img, 0, 0, $start_x, $start_y, $scale_width, $scale_height); |
61 | check_dir( dirname ($out_image), true ); // 检查输出目录 |
65 | imagegif($new_img, $out_image, $img_quality); |
68 | imagejpeg($new_img, $out_image, $img_quality); |
71 | imagepng($new_img, $out_image, $img_quality / 10); // $quality参数取值范围0-99 在php 5.1.2之后变更为0-9 |
74 | imagejpeg($new_img, $out_image, $img_quality); |
76 | imagedestroy($new_img); |
|