-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-gd.php
60 lines (52 loc) · 2.24 KB
/
index-gd.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
<?php
defined('ROOT_PATH') || define('ROOT_PATH', realpath(dirname(__FILE__)));
defined('LIBRARIES_PATH') || define('LIBRARIES_PATH', realpath(ROOT_PATH . '/libraries')); // 后面不带 /
include_once LIBRARIES_PATH . '/Common/Image/Abstract.php';
include_once LIBRARIES_PATH . '/Common/Image.php';
include_once LIBRARIES_PATH . '/Common/Imagick.php';
// 分析路径
$filePath = $_SERVER['REQUEST_URI']; // /user_pic/15_10_15/fBjmVpy4151015101330_100x100_w_s.jpeg?csc86=861
$filePath = preg_replace('/\?.*/', '', $filePath);
$fileName = pathinfo($filePath, PATHINFO_FILENAME); // 文件名,不包括后缀
$extension = pathinfo($filePath, PATHINFO_EXTENSION); // 后缀,不包括点
$dirname = pathinfo($filePath, PATHINFO_DIRNAME); // 文件目录,不包括斜杠/
$path = trim($filePath, '/');
$position = strpos($path, '/');
$module = substr($path, 0, $position);
$arrayFileName = explode('_', $fileName);
// 查找对应的模块是否有该缩略图尺寸的配置
// 查看源文件是否存在
$originPath = ROOT_PATH . $dirname . '/origin/' . $arrayFileName[0] . '.' . $extension;
if (!is_file($originPath)) {
// die('源文件不存在!!!!');
exit();
}
$widthPlace = array('c'=>'center', 'w'=>'west', 'e'=>'east');
$heightPlace = array('c'=>'center', 'n'=>'north', 's'=>'south');
// 生成缩略图
$size = count($arrayFileName);
$boolean = false;
if ($size < 2) { // 原图的缩略图(不符合规范)
// exit('不符合规范!!!!');
die();
} else {
$image = new Common_Image($originPath);
$arrayWidthHeight = explode('x', $arrayFileName[1]);
if ($size < 3) { // 仅缩略
$image->thumbnail($arrayWidthHeight[0], $arrayWidthHeight[1]);
} else { // 缩略后进行裁剪
if (!array_key_exists($arrayFileName[2], $widthPlace)) {
// die('不符合规范!');
exit;
}
if ((!isset($arrayFileName[3])) || (!array_key_exists($arrayFileName[3], $heightPlace))) {
$arrayFileName[3] = 'c';
}
$image->resize($arrayWidthHeight[0], $arrayWidthHeight[1], $widthPlace[$arrayFileName[2]], $heightPlace[$arrayFileName[3]]);
}
$boolean = $image->write(ROOT_PATH . $filePath);
}
// 输出缩略图
if ($boolean) {
$image->show();
}