-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageCrop.php
126 lines (93 loc) · 3.05 KB
/
imageCrop.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
<?php
function uploadImage($fileName,$fileSize,$fileType,$fileTemp,$imageDirectory,$thumbnailDirectory){
list($fileType,$fileExtension)= explode("/",$fileType);
if(!(isImageExtension($fileExtension))){
$error[]="Extension is not png or jpeg";
}
if(!(isFileType($fileType))){
$error[]="File Type is not image";
}
if(!(isImageSize($fileSize,5000000))){
$error[]="Size is too large";
}
if(empty($error)){
$randomFileName=rand()*Time().$fileName;
$uploadFile=move_uploaded_file($fileTemp,$imageDirectory.$randomFileName);
if($uploadFile){
$imageLinks[0]=true;
$imageLinks[1]="recipts/".$randomFileName;//// image link
$imageLinks[2]=generateThumbnail($imageDirectory.$randomFileName,$thumbnailDirectory,$randomFileName,100,$fileExtension);///thumbnail Link
return $imageLinks;
}
}
else{
print_r($error);
}
}
function generateThumbnail($source,$destination,$thumbnailName,$newWidth,$fileExtension){
list($widthOri,$heightOri)=getImageSize($source);
$ratioOri=$widthOri/$heightOri;
$newHeight=$newWidth*$ratioOri;
$imageCreateFrom=generateThumbnailFrom($source,$fileExtension);
$imageCreateTo=imagecreatetruecolor($newWidth,$newHeight);
$imageCreated=imagecopyresampled($imageCreateTo,$imageCreateFrom,0,0,0,0,$newWidth,$newHeight,$widthOri,$heightOri);
if($imageCreated){
$generatedThumbnail=generateThumbnailTo($imageCreateTo,$destination,$thumbnailName,$fileExtension);
if($generatedThumbnail){
return $destination.$thumbnailName;
}
}
}
function generateThumbnailTo($imageCreateTo,$destination,$thumbnailName,$fileExtension){
if($fileExtension=="jpeg"){
imagejpeg($imageCreateTo,$destination.$thumbnailName,100);
return true;
}
else if($fileExtension=="png"){
imagepng($imageCreateTo,$destination.$thumbnailName,9);
return true;
}
else if($fileExtension=="gif"){
imagegif($imageCreateTo,$destination.$thumbnailName,100);
return true;
}
return false;
}
function generateThumbnailFrom($source,$imageExtension){
$imageCreateFrom="";
if($imageExtension=="jpeg"){
$imageCreateFrom=imagecreatefromjpeg($source);
}
else if($imageExtension=="png"){
$imageCreateFrom=imagecreatefrompng($source);
}
else if($imageExtension=="gif"){
$imageCreateFrom=imagecreatefromgif($source);
}
return $imageCreateFrom;
}
function isFileType($fileType){
if($fileType=="image"){
return true;
}
else{
return false;
}
}
function isImageExtension($fileExtension){
if($fileExtension=="jpeg" || $fileExtension=="png" ){
return true;
}
else{
return false;
}
}
function isImageSize($fileSize,$limit){
if($fileSize<=$limit){
return true;
}
else{
return false;
}
}
?>