php class picture private image_file public image public image_type pu

  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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
class picture {
private $image_file;
public $image;
public $image_type;
public $image_width;
public $image_height;
public function __construct($image_file) {
$this->image_file=$image_file;
$image_info = getimagesize($this->image_file);
$this->image_width = $image_info[0];
$this->image_height = $image_info[1];
switch($image_info[2]) {
case 1: $this->image_type = 'gif'; break;//1: IMAGETYPE_GIF
case 2: $this->image_type = 'jpeg'; break;//2: IMAGETYPE_JPEG
case 3: $this->image_type = 'png'; break;//3: IMAGETYPE_PNG
case 4: $this->image_type = 'swf'; break;//4: IMAGETYPE_SWF
case 5: $this->image_type = 'psd'; break;//5: IMAGETYPE_PSD
case 6: $this->image_type = 'bmp'; break;//6: IMAGETYPE_BMP
case 7: $this->image_type = 'tiffi'; break;//7: IMAGETYPE_TIFF_II (порядок байт intel)
case 8: $this->image_type = 'tiffm'; break;//8: IMAGETYPE_TIFF_MM (порядок байт motorola)
case 9: $this->image_type = 'jpc'; break;//9: IMAGETYPE_JPC
case 10: $this->image_type = 'jp2'; break;//10: IMAGETYPE_JP2
case 11: $this->image_type = 'jpx'; break;//11: IMAGETYPE_JPX
case 12: $this->image_type = 'jb2'; break;//12: IMAGETYPE_JB2
case 13: $this->image_type = 'swc'; break;//13: IMAGETYPE_SWC
case 14: $this->image_type = 'iff'; break;//14: IMAGETYPE_IFF
case 15: $this->image_type = 'wbmp'; break;//15: IMAGETYPE_WBMP
case 16: $this->image_type = 'xbm'; break;//16: IMAGETYPE_XBM
case 17: $this->image_type = 'ico'; break;//17: IMAGETYPE_ICO
default: $this->image_type = ''; break;
}
$this->fotoimage();
}
private function fotoimage() {
switch($this->image_type) {
case 'gif': $this->image = imagecreatefromgif($this->image_file); break;
case 'jpeg': $this->image = imagecreatefromjpeg($this->image_file); break;
case 'png': $this->image = imagecreatefrompng($this->image_file); break;
}
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
}
public function autoimageresize($new_w, $new_h) {
$difference_w = 0;
$difference_h = 0;
if($this->image_width < $new_w && $this->image_height < $new_h) {
$this->imageresize($this->image_width, $this->image_height);
}
else {
if($this->image_width > $new_w) {
$difference_w = $this->image_width - $new_w;
}
if($this->image_height > $new_h) {
$difference_h = $this->image_height - $new_h;
}
if($difference_w > $difference_h) {
$this->imageresizewidth($new_w);
}
elseif($difference_w < $difference_h) {
$this->imageresizeheight($new_h);
}
else {
$this->imageresize($new_w, $new_h);
}
}
}
public function percentimagereduce($percent) {
$new_w = $this->image_width * $percent / 100;
$new_h = $this->image_height * $percent / 100;
$this->imageresize($new_w, $new_h);
}
public function imageresizewidth($new_w) {
$new_h = $this->image_height * ($new_w / $this->image_width);
$this->imageresize($new_w, $new_h);
}
public function imageresizeheight($new_h) {
$new_w = $this->image_width * ($new_h / $this->image_height);
$this->imageresize($new_w, $new_h);
}
public function imageresize($new_w, $new_h) {
$new_image = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_w, $new_h, $this->image_width, $this->image_height);
$this->image_width = $new_w;
$this->image_height = $new_h;
$this->image = $new_image;
}
public function imagesave($image_type='jpeg', $image_file=NULL, $image_compress=100, $image_permiss='') {
if($image_file==NULL) {
switch($this->image_type) {
case 'gif': header("Content-type: image/gif"); break;
case 'jpeg': header("Content-type: image/jpeg"); break;
case 'png': header("Content-type: image/png"); break;
}
}
switch($this->image_type) {
case 'gif': imagegif($this->image, $image_file); break;
case 'jpeg': imagejpeg($this->image, $image_file, $image_compress); break;
case 'png': imagepng($this->image, $image_file); break;
}
if($image_permiss != '') {
chmod($image_file, $image_permiss);
}
}
public function imageout() {
imagedestroy($this->image);
}
public function __destruct() {
}
}
//Заюзывать довольно просто:
//
//
//пропорционально изменяем размер изображение и сохраняем на сервере не изменяя расширения файла
//полученное изображение не будет превышать по высоте 400 пикселей и по ширине 300
//т.е. автоматически впишется в нужные размеры
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->autoimageresize(300, 400);
//$new_image->imagesave($new_image->image_type, 'папка на сервере');
//$new_image->imageout();
//========================
//вывод на экран не изменяя расширения файла
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->autoimageresize(300, 400);
//$new_image->imagesave($new_image->image_type, NULL);
//$new_image->imageout();
//========================
//сохраняем на сервере и выбираем тип файла на выходе
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->autoimageresize(300, 400);
//$new_image->imagesave('png', 'папка на сервере');
//$new_image->imageout();
//========================
//ужимаем файл и выводим на экран
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->autoimageresize(300, 400);
//$new_image->imagesave('jpeg', NULL, 75);
//$new_image->imageout();
//========================
//сохраняем на сервере, ужимаем и устанавливаем права доступа
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->autoimageresize(300, 400);
//$new_image->imagesave('jpeg', 'папка на сервере', 75, 0777);
//$new_image->imageout();
//========================
//Уменьшаем изображение по ширине
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->imageresizewidth(300);
//$new_image->imagesave($new_image->image_type, 'папка на сервере');
//$new_image->imageout();
//========================
//Уменьшаем изображение по высоте
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->imageresizeheight(400);
//$new_image->imagesave($new_image->image_type, 'папка на сервере');
//$new_image->imageout();
//========================
//произвольно уменьшаем изображение не сохраняя пропорции
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->imageresize(300,400);
//$new_image->imagesave($new_image->image_type, 'папка на сервере');
//$new_image->imageout();
//========================
//уменьшаем изображение в процентном соотношении
//Code (php):
//$new_image = new picture('url или файл');
//$new_image->percentimagereduce(30);
//$new_image->imagesave($new_image->image_type, 'папка на сервере');
//$new_image->imageout();
//========================
?>