3
3
defined ('BASEPATH ' ) OR exit ('No direct script access allowed ' );
4
4
5
5
class Image{
6
+ protected $ CI ;
7
+
8
+ //构造函数
9
+ public function __construct (){
10
+ //附属类,让其可以访问CI的资源
11
+ $ this ->CI = & get_instance ();
12
+ }
6
13
public function thumbnail ($ source ,$ width ,$ height ){
7
14
//获取缩略图名称
8
15
$ source = str_replace ("\\" ,"/ " ,$ source );
@@ -23,19 +30,48 @@ public function thumbnail($source,$width,$height){
23
30
$ dirname = dirname ($ source ); //获取的路径最后没有/
24
31
//缩略图完整路径
25
32
$ thumbnail_full = $ dirname .'/ ' .$ thumbnail_name ;
26
- $ image = new Imagick ($ source );
27
33
// 创建缩略图
28
34
//原图宽高大于缩略图
29
35
if (($ img_w > $ width ) || ($ img_h > $ height )){
30
- //$image->setImageCompressionQuality(90);
31
- $ image ->cropThumbnailImage ( $ width , $ height );
36
+ //检测是否支持ImageMagick
37
+ if ($ this ->check ()){
38
+ //使用ImageMagick裁剪图像
39
+ $ image = new Imagick ($ source );
40
+ $ image ->cropThumbnailImage ( $ width , $ height );
41
+ //将缩略图输出到文件
42
+ $ image ->writeImage ( $ thumbnail_full );
43
+ //清理工作
44
+ $ image ->clear ();
45
+ }
46
+ //不支持ImageMagick,使用GD2进行裁剪
47
+ else {
48
+ //配置裁剪参数,参考:https://codeigniter.org.cn/user_guide/libraries/image_lib.html
49
+ $ config ['image_library ' ] = 'gd2 ' ;
50
+ $ config ['source_image ' ] = $ source ;
51
+ $ config ['create_thumb ' ] = TRUE ;
52
+ $ config ['maintain_ratio ' ] = TRUE ;
53
+ $ config ['width ' ] = $ width ;
54
+ $ config ['height ' ] = $ height ;
55
+ $ this ->CI ->load ->library ('image_lib ' , $ config );
56
+ $ this ->CI ->image_lib ->resize ();
57
+ }
58
+
32
59
}
33
60
34
- //将缩略图输出到文件
35
- $ image ->writeImage ( $ thumbnail_full );
36
61
37
- //清理工作
38
- $ image ->clear ();
62
+
63
+
64
+ }
65
+ //检测是否支持ImageMagick
66
+ protected function check (){
67
+ $ ext = get_loaded_extensions ();
68
+ //如果已经安装ImageMagick
69
+ if (array_search ('imagick ' ,$ ext )){
70
+ return TRUE ;
71
+ }
72
+ else {
73
+ return FALSE ;
74
+ }
39
75
}
40
76
//压缩图片
41
77
public function compress ($ source ){
0 commit comments