Blame view

framework/class/image.class.php 6.74 KB
4d84a934   曹明   初始代码提交
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */
defined('IN_IA') or exit('Access Denied');


class Image {
	private $src;
	private $actions = array(); 		private $resize_width = 0;
	private $resize_height = 0;

	private $image = null;
	private $imageinfo = array();
			private $crop_width = 0;
		private $crop_height = 0;
		private $crop_position = 1;

	private $ext = '';

	public function __construct($src) {
		$this->src = $src;
		$this->ext = pathinfo($src, PATHINFO_EXTENSION);
	}

	public static function create($src) {
		return new self($src);
	}

	public function resize($width = 0, $height = 0) {
		if ($width > 0 || $height > 0) {
			$this->actions[] = 'resize';
		}
		if ($width > 0 && $height == 0) {
			$height = $width;
		}
		if ($height > 0 && $width == 0) {
			$width = $height;
		}
		$this->resize_width = $width;
		$this->resize_height = $height;

		return $this;
	}

	public function crop($width = 400, $height = 300, $position = 1) {
		if ($width > 0 || $height > 0) {
			$this->actions[] = 'crop';
		}
		if ($width > 0 && $height == 0) {
			$height = $width;
		}
		if ($height > 0 && $width == 0) {
			$width = $height;
		}
		$this->crop_width = $width;
		$this->crop_height = $height;
				$this->crop_position = min(intval($position), 9);

		return $this;
	}

	public function getExt() {
		return in_array($this->ext, array('jpg', 'jpeg', 'png', 'gif')) ? $this->ext : 'jpeg';
	}

	public function isPng() {
		return file_is_image($this->src) && $this->getExt() == 'png';
	}

	public function isJPEG() {
		return file_is_image($this->src) && in_array($this->getExt(), array('jpg', 'jpeg'));
	}

	public function isGif() {
		return file_is_image($this->src) && $this->getExt() == 'gif';
	}

	
	public function saveTo($path, $quality = null) {
		$path = safe_gpc_path($path);
		if (empty($path)) {
			return false;
		}
		$result = $this->handle();
		if (!$result) {
			return false;
		}
		$ext = $this->getExt();
		if ($ext == 'jpg') {
			$ext = 'jpeg';
		}
		$func = 'image' . $ext;
		$real_quality = $this->realQuality($quality);
		$saved = false;
		$image = $this->image();
		imagealphablending($image, false);
		imagesavealpha($image, true);
		if (is_null($real_quality)) {
			$saved = $func($image, $path);
		} else {
			if (!$this->isGif()) {
				$saved = $func($image, $path, $real_quality);
			}
		}
		$this->destroy();

		return $saved ? $path : $saved;
	}

	private function realQuality($quality = null) {
		if (is_null($quality)) {
			return null;
		}
				$quality = min($quality, 100);
		if ($this->isJPEG()) {
			return $quality * 0.75;
		}
		if ($this->isPng()) {
			return round(abs((100 - $quality) / 11.111111));
		}

		return null;
	}

	protected function handle() {
				if (!function_exists('gd_info')) {
			return false;
		}
		$this->image = $this->createResource();
		if (!$this->image) {
			return false;
		}
		$this->imageinfo = getimagesize($this->src);
		$actions = array_unique($this->actions);
		$src_image = $this->image;
		foreach ($actions as $action) {
			$method = 'do' . ucfirst($action);
			$src_image = $this->{$method}($src_image);
		}
		$this->image = $src_image;

		return true;
	}

	
	protected function doCrop($src_image) {
		list($dst_x, $dst_y) = $this->getCropDestPoint();
		if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
			$new_image = imagecrop($src_image, array('x' => $dst_x, 'y' => $dst_y, 'width' => $this->crop_width, 'height' => $this->crop_height));
			imagedestroy($src_image);
		} else {
			$new_image = $this->modify($src_image, $this->crop_width,
				$this->crop_height, $this->crop_width, $this->crop_height, 0, 0, $dst_x, $dst_y);
		}
		$this->imageinfo[0] = $this->crop_width;
		$this->imageinfo[1] = $this->crop_height;

		return $new_image;
	}

	
	protected function doResize($src_image) {
		$newimage = $this->modify($src_image, $this->resize_width, $this->resize_height,
			$this->imageinfo[0], $this->imageinfo[1]);
		$this->imageinfo[0] = $this->resize_width;
		$this->imageinfo[1] = $this->resize_height;

		return $newimage;
	}

	
	protected function modify($src_image, $width, $height, $src_width,
							  $src_height, $dst_x = 0, $dst_y = 0, $src_x = 0, $src_y = 0) {
		$image = imagecreatetruecolor($width, $height);
		imagealphablending($image, false);
		imagesavealpha($image, true);
		imagecopyresampled($image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $width, $height,
			$src_width, $src_height);
		imagedestroy($src_image);

		return $image;
	}

	private function image() {
		return $this->image;
	}

	private function destroy() {
		if ($this->image) {
			imagedestroy($this->image);
		}
	}

	private function createResource() {
		if (file_exists($this->src) && !is_readable($this->src)) {
			return null;
		}
		if ($this->isPng()) {
			return imagecreatefrompng($this->src);
		}
		if ($this->isJPEG()) {
			return imagecreatefromjpeg($this->src);
		}
		if ($this->isGif()) {
			return imagecreatefromgif($this->src);
		}

		return null;
	}

	
	public function toBase64($prefix = 'data:image/%s;base64,') {
		$filename = tempnam('tmp', 'base64');
		$prefix = sprintf($prefix, $this->getExt());
		$result = $this->saveTo($filename);
		if (!$result) {
			return false;
		}
		$content = file_get_contents($filename);
		$base64 = base64_encode($content);
		unlink($filename);

		return $prefix . $base64;
	}

	
	private function getCropDestPoint() {
				$s_width = $this->imageinfo[0];
		$s_height = $this->imageinfo[1];
		$dst_x = $dst_y = 0;
				if ($this->crop_width == '0' || $this->crop_width > $s_width) {
			$this->crop_width = $s_width;
		}
		if ($this->crop_height == '0' || $this->crop_height > $s_height) {
			$this->crop_height = $s_height;
		}
		switch ($this->crop_position) {
			case 0:
			case 1:
				$dst_x = 0;
				$dst_y = 0;
				break;
			case 2:
				$dst_x = ($s_width - $this->crop_width) / 2;
				$dst_y = 0;
				break;
			case 3:
				$dst_x = $s_width - $this->crop_width;
				$dst_y = 0;
				break;
			case 4:
				$dst_x = 0;
				$dst_y = ($s_height - $this->crop_height) / 2;
				break;
			case 5:
				$dst_x = ($s_width - $this->crop_width) / 2;
				$dst_y = ($s_height - $this->crop_height) / 2;
				break;
			case 6:
				$dst_x = $s_width - $this->crop_width;
				$dst_y = ($s_height - $this->crop_height) / 2;
				break;
			case 7:
				$dst_x = 0;
				$dst_y = $s_height - $this->crop_height;
				break;
			case 8:
				$dst_x = ($s_width - $this->crop_width) / 2;
				$dst_y = $s_height - $this->crop_height;
				break;
			case 9:
				$dst_x = $s_width - $this->crop_width;
				$dst_y = $s_height - $this->crop_height;
				break;
			default:
				$dst_x = 0;
				$dst_y = 0;
		}
		if ($this->crop_width == $s_width) {
			$dst_x = 0;
		}
		if ($this->crop_height == $s_height) {
			$dst_y = 0;
		}

		return array(intval($dst_x), intval($dst_y));
	}
}