-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_converter.php
111 lines (87 loc) · 3.07 KB
/
image_converter.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
<?php
class Image_converter{
function convert_image($convert_type, $target_dir, $image_name, $image_quality=100){
$target_dir = "$target_dir/";
$image = $target_dir.$image_name;
$img_name = $this->remove_extension_from_image($image);
if($convert_type == 'png'){
$binary = imagecreatefromstring(file_get_contents($image));
$image_quality = floor(10 - ($image_quality / 10));
ImagePNG($binary, $target_dir.$img_name.'.'.$convert_type, $image_quality);
return $img_name.'.'.$convert_type;
}
if($convert_type == 'jpg'){
$binary = imagecreatefromstring(file_get_contents($image));
imageJpeg($binary, $target_dir.$img_name.'.'.$convert_type, $image_quality);
return $img_name.'.'.$convert_type;
}
if($convert_type == 'gif'){
$binary = imagecreatefromstring(file_get_contents($image));
imageGif($binary, $target_dir.$img_name.'.'.$convert_type, $image_quality);
return $img_name.'.'.$convert_type;
}
return false;
}
public function upload_image($files, $target_dir, $input_name){
$target_dir = "$target_dir/";
$base_name = basename($files[$input_name]["name"]);
$imageFileType = $this->get_image_type($base_name);
$new_name = $this->get_dynamic_name($base_name, $imageFileType);
$target_file = $target_dir . $new_name;
$validate = $this->validate_image($files[$input_name]["tmp_name"]);
if(!$validate){
echo "Doesn't seem like an image file :(";
return false;
}
$file_size = $this->check_file_size($files[$input_name]["size"], 1000000);
if(!$file_size){
echo "You cannot upload more than 1MB file";
return false;
}
$file_type = $this->check_only_allowed_image_types($imageFileType);
if(!$file_type){
echo "You cannot upload other than JPG, JPEG, GIF and PNG";
return false;
}
if (move_uploaded_file($files[$input_name]["tmp_name"], $target_file)) {
return array($new_name, $imageFileType);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
protected function get_image_type($target_file){
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
return $imageFileType;
}
protected function validate_image($file){
$check = getimagesize($file);
if($check !== false) {
return true;
}
return false;
}
protected function check_file_size($file, $size_limit){
if ($file > $size_limit) {
return false;
}
return true;
}
protected function check_only_allowed_image_types($imagetype){
if($imagetype != "jpg" && $imagetype != "png" && $imagetype != "jpeg" && $imagetype != "gif" ) {
return false;
}
return true;
}
protected function get_dynamic_name($basename, $imagetype){
$only_name = basename($basename, '.'.$imagetype);
$combine_time = $only_name.'_'.time();
$new_name = $combine_time.'.'.$imagetype;
return $new_name;
}
protected function remove_extension_from_image($image){
$extension = $this->get_image_type($image);
$only_name = basename($image, '.'.$extension);
return $only_name;
}
}
?>