-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.php
181 lines (153 loc) · 5.5 KB
/
image.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//this class require autoload cookier helper and url helper
class Image {
function scale_to_smaller($wished_width, $wished_height, $path, $name, $new_name, $quality=100)
{
$info= getimagesize($path.$name);
$width = $info[0];
$height = $info[1];
$type = substr(image_type_to_extension($info[2]),1);
$scale = $width/$height;
$wished_scale = $wished_width/$wished_height;
if(($width > $wished_width || $height > $wished_height))
{
// now make the full image
if($wished_scale > $scale){
$new_width = $wished_height * $scale;
$new_height = $wished_height;
}
elseif($wished_scale <= $scale){
$new_width = $wished_width;
$new_height = $wished_width * $height / $width;
}
if($type == 'jpg' || $type == 'jpeg'){$image = imagecreatefromjpeg($path.$name);}
elseif($type=='gif'){$image = imagecreatefromgif($path.$name);}
elseif($type=='png'){$image = imagecreatefrompng($path.$name);}
$temp = imagecreatetruecolor($new_width, $new_height);
imagealphablending($temp, false);
imagesavealpha($temp, true);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if($type == 'jpg' || $type == 'jpeg'){imagejpeg($temp, $path.$new_name, $quality);}
elseif($type=='gif'){imagegif($temp, $path.$new_name);}
elseif($type=='png'){imagepng($temp, $path.$new_name, 0);}
}
else
{
copy($path.$name, $path.$new_name);
}
}
function scale_to_bigger($wished_width, $wished_height, $path, $name, $new_name, $quality=100)
{
$info= getimagesize($path.$name);
$width = $info[0];
$height = $info[1];
$type = substr(image_type_to_extension($info[2]),1);
$scale = $width/$height;
$wished_scale = $wished_width/$wished_height;
if(($width >= $wished_width || $height > $wished_height))
{
if($wished_scale > $scale){
$new_width = $wished_width;
$new_height = $wished_width * $height / $width;
}
elseif($wished_scale <= $scale){
$new_width = $wished_height * $scale;
$new_height = $wished_height;
}
if($type == 'jpg' || $type == 'jpeg'){$image = imagecreatefromjpeg($path.$name);}
elseif($type=='gif'){$image = imagecreatefromgif($path.$name);}
elseif($type=='png'){$image = imagecreatefrompng($path.$name);}
$temp = imagecreatetruecolor($new_width, $new_height);
imagealphablending($temp, false);
imagesavealpha($temp, true);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if($type == 'jpg' || $type == 'jpeg'){imagejpeg($temp, $path.$new_name, $quality);}
elseif($type=='gif'){imagegif($temp, $path.$new_name);}
elseif($type=='png'){imagepng($temp, $path.$new_name, 0);}
}
else
{
copy($path.$name, $path.$new_name);
}
}
function resize($target_width, $target_height, $path, $name, $new_name, $quality=100){
$info= getimagesize($path.$name);
$width = $info[0];
$height = $info[1];
$type = substr(image_type_to_extension($info[2]),1);
if($type == 'jpg' || $type == 'jpeg'){$image = imagecreatefromjpeg($path.$name);}
elseif($type=='gif'){$image = imagecreatefromgif($path.$name);}
elseif($type=='png'){$image = imagecreatefrompng($path.$name);}
$temp = imagecreatetruecolor($target_width, $target_height);
imagealphablending($temp, false);
imagesavealpha($temp, true);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $target_width, $target_height, $width, $height);
if($type == 'jpg' || $type == 'jpeg'){imagejpeg($temp, $path.$new_name, $quality);}
elseif($type=='gif'){imagegif($temp, $path.$new_name);}
elseif($type=='png'){imagepng($temp, $path.$new_name, 0);}
}
function crop($wished_width, $wished_height, $path, $name, $new_name, $quality=100)
{
$info= getimagesize($path.$name);
$width = $info[0];
$height = $info[1];
$type = substr(image_type_to_extension($info[2]),1);
if($width > $height){
$x= ($width - $wished_width)/2;
$y= 0;
}elseif($width <= $height){
$x= 0;
$y= ($height - $wished_height) / 2;
}
if($type == 'jpg' || $type == 'jpeg'){$image = imagecreatefromjpeg($path.$name);}
elseif($type=='gif'){$image = imagecreatefromgif($path.$name);}
elseif($type=='png'){$image = imagecreatefrompng($path.$name);}
$temp = imagecreatetruecolor($wished_width, $wished_height);
imagealphablending($temp, false);// to preserve the transparent background
imagesavealpha($temp, true);// to preserve the transparent background
imagecopy($temp, $image, 0, 0, $x, $y, $width, $height);
if($type == 'jpg' || $type == 'jpeg'){imagejpeg($temp, $path.$new_name, $quality);}
elseif($type=='gif'){imagegif($temp, $path.$new_name);}
elseif($type=='png'){imagepng($temp, $path.$new_name, 0);}
}
function delete($directory)
{
if( file_exists($directory) && is_file($directory) ){
unlink($directory);
return TRUE;
}else{
return FALSE;
}
}
function full_name()
{
$name = $_SERVER['HTTP_X_FILE_NAME'];
return $name;
}
function name()
{
$name = $_SERVER['HTTP_X_FILE_NAME'];
$name = preg_replace('/\.[^.]*$/', '', $name); // remove extension from file name
return $name;
}
function size()
{
$size = $_SERVER['HTTP_X_FILE_SIZE'];
return $size;
}
function extension()
{
$extension = strtolower(substr(strrchr($_SERVER['HTTP_X_FILE_NAME'], '.'), 1));
return $extension;
}
function content()
{
return file_get_contents("php://input");
}
function save($fullpath)
{
$file = file_get_contents("php://input");
file_put_contents($fullpath, $file);
}
}
?>