-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_helper.php
144 lines (125 loc) · 4.58 KB
/
img_helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* get_img(array('file'=>'image.png', 'path'=>NEWS_PATH, 'size'=>array(100, 100), 'file_no_image'=>'nouser.png'))
*/
function get_img($params = [])
{
$defaults = [
'file' => false,
'path' => false,
'size' => false,
'with_http' => false,
'need_crop' => true,
'center' => true,
'orientation' => 'inside',
'file_no_image' => 'noimage.png',
'path_no_image' => 'media/upload/no_image/'
];
$params = array_merge($defaults, $params);
$real_path = realpath('.'). '/'. $params['path'];
$real_path_file = $real_path . $params['file'];
if (!is_file($real_path_file))
{
$params['file'] = $params['file_no_image'];
$params['path'] = $params['path_no_image'];
$real_path = realpath('.'). '/'. $params['path'];
$real_path_file = $real_path . $params['file_no_image'];
}
if ($params['size'])
{
$resize_file = $params['size'][0].'_'.$params['size'][1].'_'.$params['file'];
$real_path_resize_file = $real_path.$resize_file;
if (!is_file($real_path_resize_file))
{
$file_size = getimagesize($real_path_file); // размеры оригинальной картинки
if (($file_size[0] > $params['size'][0]) || ($file_size[1] > $params['size'][1]))
{
$params['file'] = $resize_file;
if ($file_size[0] <= $params['size'][0])
{
$params['size'][0] = $file_size[0];
}
if ($file_size[1] <= $params['size'][1])
{
$params['size'][1] = $file_size[1];
}
$oCI = & get_instance();
$config = array();
$config['source_image'] = $real_path_file;
$config['new_image']= $real_path_resize_file;
$config['width'] = $params['size'][0];
$config['height'] = $params['size'][1];
$config['image_library'] = 'gd2';
$config['master_dim'] = 'auto';
if ($params['orientation'] == 'outside')
{
$ratio_image = $file_size[0]/$file_size[1]; // 0.6734 картинка была 330*490
$calculation_h = $config['width']/$ratio_image; // при w=100 расчитали h = 148
$calculation_w = $config['height']*$ratio_image; // при h=120 расчитали w = 80
if ($calculation_w < $config['width'])
{
$config['master_dim'] = 'width';
}
elseif ($calculation_h < $config['height'])
{
$config['master_dim'] = 'height';
}
}
$oCI->load->library('image_lib', $config);
if (!$oCI->image_lib->resize())
{
echo $oCI->image_lib->display_errors();
}
}
}
else
{
$params['file'] = $resize_file;
}
}
if ($params['need_crop'] && $params['orientation'] == 'outside')
{
crop_image($params);
}
if ($params['with_http'])
{
$params['path'] = base_url().$params['path'];
}
else
{
$params['path'] = TO_APP_FOLDER.$params['path'];
}
return $params['path'].$params['file'];
}
function crop_image($params)
{
$real_path = realpath('.'). '/'. $params['path'];
$real_path_file = $real_path . $params['file'];
$file_size = getimagesize($real_path_file);
$img_width = $file_size[0];
$img_height = $file_size[1];
$block_width = $params['size'][0];
$block_height = $params['size'][1];
$crop_info = (object) array('x'=>0, 'y'=>0, 'x2'=>0, 'y2'=>0);
if ($params['center'])
{
$crop_info->x = ($img_width - $block_width)/2;
$crop_info->x2 = $crop_info->x + $block_width;
$crop_info->y = ($img_height - $block_height)/2;
$crop_info->y2 = $crop_info->y + $block_height;
}
else
{
$crop_info->x2 = $block_width;
$crop_info->y2 = $block_height;
}
if (($crop_info->x2 != 0) && ($crop_info->y2 != 0))
{
$oCI = & get_instance();
$oCI->load->library('image_moo');
$oCI->image_moo->load($real_path_file);
$oCI->image_moo->crop((int)$crop_info->x, (int)$crop_info->y, (int)$crop_info->x2, (int)$crop_info->y2);
$oCI->image_moo->save_pa('', '', true);
//$response = $oCI->image_moo->display_errors();
}
}