-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageSaver.php
140 lines (119 loc) · 4.59 KB
/
ImageSaver.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
<?php
/**
*Copyright (c) 2012 amir hoseinian http://www.amirhoseinian.com
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*Example usage
* $imageMaker = New ImageMaker();
* $imageMaker->setImage($_FILES['image'])->setName('sample name')->setPath("\data\images\brand\\")->resize(270,'auto')->save();
*/
class ImageSaver{
//class properties
protected $image;
protected $name;
protected $path;
protected $imageResized;
public $allowFileTypes = array("jpg","jpeg","gif","png");
/**
*@param array $image posted file like $FILE['image']
*/
public function setImage($image){
$this->image = $image;
if(!in_array($this->getExtension() , $this->allowFileTypes)) //check if file is an image
throw new \Exception("FileTypeNotSupported", 1);
return $this;
}
/**
*@param string $name new name for image to save
*/
public function setName($name){
$this->name = $name;
return $this;
}
/**
*@param string $path store image place -- example \data\images\sample\
*@constant APPLICATION_PATH u can define your application path in a global variable or simply change this
* with somting like || c:\wamp\www\sample\public
*/
public function setPath($path){
if(APPLICATION_PATH)
$this->path = APPLICATION_PATH.$path;
else
$this->path = $path;
return $this;
}
/**
*@return string $ext image extension eg jpg,png
*/
public function getExtension(){
$info = pathinfo($this->image['name']);
$ext = strtolower($info['extension']); // get the extension of the file
return $ext;
}
/**
*@return resource open image file for resizing
*/
public function getImageFile(){
switch (strtolower($this->getExtension())) {
case 'jpg':
case 'jpeg':
return imagecreatefromjpeg($this->image['tmp_name']);
break;
case 'png':
return imagecreatefrompng($this->image['tmp_name']);
break;
case 'gif':
return imagecreatefromgif($this->image['tmp_name']);
break;
default:
throw new \Exception("image type not recognized");
break;
}
}
/**
*@param string OR int $width eg 300,250,'auto'
*@param string OR int $height eg 300,250,'auto'
*/
public function resize($width, $height){
list($imageWidth, $imageHeight, $type, $attr) = getimagesize($this->image['tmp_name']);
list($width, $height) = $this->getBestDimensions($width, $height, $imageWidth, $imageHeight);
$this->imageResized = imagecreatetruecolor($width, $height);
imagecopyresampled($this->imageResized, $this->getImageFile(), 0, 0, 0, 0, $width, $height, $imageWidth, $imageHeight);
return $this;
}
/**
*@param string OR int $width eg 300,250,'auto'
*@param string OR int $height eg 300,250,'auto'
*@param int width of uploaded image
*@param int height of uploaded image
*@return array ($width, $height) optimal dimensions
*/
private function getBestDimensions($width, $height, $imageWidth, $imageHeight){
if($width == 'auto' && is_numeric($height)){
$width = round(($imageWidth * $height) / $imageHeight);
}elseif($height == 'auto' && is_numeric($width)){
$height = round(($imageHeight * $width) / $imageWidth);
}
else if(!is_numeric($height) && !is_numeric($width))
throw new \Exception("error in d", 1);
return array($width,$height);
}
/**
*@param int $imageQuality range between 1-100 define quality of saved image
*/
public function save($imageQuality=100){
$newname = $this->name.".".$this->getExtension($this->image);
$target = $this->path.$newname;
if($this->imageResized){
imagejpeg($this->imageResized, $target, $imageQuality);
imagedestroy($this->imageResized);
$this->imageResized = null;
}else
move_uploaded_file($this->image['tmp_name'], $target);
}
}