-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadForm.php
93 lines (86 loc) · 3.04 KB
/
UploadForm.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
<?php
class UploadForm
{
public $name;
public $tempName;
public $type;
public $size;
public $image;
public $image_type;
public function load_img($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
public function save_img($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
public function output_img($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
public function getWidth() {
return imagesx($this->image);
}
public function getHeight() {
return imagesy($this->image);
}
public function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize_img($width,$height);
}
public function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize_img($width,$height);
}
public function resize_img($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
$new_image = imagecreatetruecolor($width, $height);//create the background 130x130
$whiteBackground = imagecolorallocate($new_image, 255, 255, 255);
imagefill($new_image,0,0,$whiteBackground); // fill the background with white
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
public function upload($path)
{
return move_uploaded_file($this->tempName,$path);
}
public function createFolder($path){
if (!is_dir($path)){
return mkdir($path,0775, true);
}else{return false;}
}
public function loadFile($file){
$this->name = $file['name'];
$this->tempName = $file['tmp_name'];
$this->size = $file['size'];
$this->type = $file['type'];
}
public function unlink_file($path){
if(is_file($path)){
return unlink($path);
}
}
}