本文实例讲述了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
|
class App_image_helper {
protected $imgFileName;
protected $imgWidth;
protected $imgHeight;
protected $imgMime;
protected $imgResource;
static $imgMineList
= array(
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'wbmp' => 'image/wbmp',
);
/**
* 根据文件名,初始化图片,
* 计算出给定图片的宽、高、图片类型,并获取图片的资源保存到内存,便于下次使用
* App_image_helper constructor.
*
* @param $fileName
*/
public function __construct($fileName) {
$this->imgFileName = $fileName;
list($this->imgWidth, $this->imgHeight, $this->imgMime) = $this->getImageInfo($this->imgFileName);
$this->imgResource = $this->getImageResource($this->imgFileName);
}
/**
* 根据图片路径获取相关宽、高、MIME类型信息
*
* @param $fileName
*
* @return array|null
*/
protected function getImageInfo($fileName) {
$result = null;
if ( is_file($fileName) ) {
$tmpImageInfo = getimagesize($fileName);
if ( $tmpImageInfo ) {
$result = array($tmpImageInfo[0], $tmpImageInfo[1], $tmpImageInfo['mime']);
}
}
return $result;
}
/**
* 将图片文件转为资源类类型
*
* @param $fileName
*
* @return null|resource
*/
protected function getImageResource($fileName) {
$image = null;
if ( is_file($fileName) ) {
switch ($this->imgMime) {
case self::$imgMineList['jpeg']:
$image = imagecreatefromjpeg($fileName);
break;
case self::$imgMineList['gif']:
$image = imagecreatefromgif($fileName);
break;
case self::$imgMineList['png']:
$image = imagecreatefrompng($fileName);
break;
case self::$imgMineList['wbmp']:
$image = imagecreatefromwbmp($fileName);
break;
default:
break;
}
}
return $image;
}
/**
* 可根据固定宽,等比缩放图片;或根据百分比,等比缩放图片
*
* @param int $width
* @param int $percent
*
* @return array|null
*/
protected function getSizeByScale($width = 360, $percent = 1) {
$result = null;
if ( $this->imgWidth && $this->imgHeight ) {
if ( $width ) {
$result = array($width, intval($width * $this->imgHeight / $this->imgWidth));
} elseif ( $percent ) {
$result = array(intval($this->imgWidth * $percent), intval($this->imgHeight * $percent));
}
}
return $result;
}
/**
* 外调
*
* @param int $percentOrWidth int整数表示图片缩放为固定宽度,0.0~0.99999表示缩放百分比
* @param null $fileName
* @param int $quality
* @param bool $reSample 重新采样图片,默认是
*
* @return bool
*/
public function createImage($percentOrWidth = 1, $fileName = null, $quality = 75, $reSample = true) {
$result = false;
$fileName ? header('Content-Type: ' . $this->imgMime) : false;
$size = $this->getSizeByScale(($percentOrWidth <= 1) ? null : $percentOrWidth, $percentOrWidth);
if ( $size ) {
$thumb = imagecreatetruecolor($size[0], $size[1]);
if ( $reSample ) {
imagecopyresampled($thumb, $this->imgResource, 0, 0, 0, 0, $size[0], $size[1], $this->imgWidth, $this->imgHeight);
} else {
imagecopyresized($thumb, $this->imgResource, 0, 0, 0, 0, $size[0], $size[1], $this->imgWidth, $this->imgHeight);
}
$result = imagejpeg($thumb, $fileName, $quality);
}
return $result;
}
}
|
希望本文所述对大家PHP程序设计有所帮助。
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 86
-
springboot整合ehcache 实现支付超时限制的方法
2025-05-27 68 -
2025-05-25 85
-
2025-05-29 48
-
2025-05-27 58
热门评论

