1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: mi
5+ * Date: 2014/10/30
6+ * Time: 14:56
7+ */
8+
9+ namespace Plugin ;
10+
11+ /**
12+ * 动态获取图片尺寸
13+ *
14+ * 原始图片: http://img.xx.com/chat/M00/00/51/cHxqplZWoMKANLnyAAC290JKUck397.jpg
15+ * 按固定宽和高: http://img.xx.com/chat/M00/00/51/cHxqplZWoMKANLnyAAC290JKUck397.jpg_100x200.jpg
16+ * 按固定宽: http://img.xx.com/chat/M00/00/51/cHxqplZWoMKANLnyAAC290JKUck397.jpg_200-.jpg
17+ * 按固定高: http://img.xx.com/chat/M00/00/51/cHxqplZWoMKANLnyAAC290JKUck397.jpg_-200.jpg
18+ *
19+ * Class ImgSize
20+ * @package Plugin
21+ */
22+ class ImgSize
23+ {
24+ private static $ trust_hosts = array (
25+ null , // 保留null
26+ STATIC_DOMAIN ,
27+ MAIN_DOMAIN ,
28+ "fdfs.estt.com.cn "
29+ );
30+
31+
32+ /**
33+ * 获取原始图片
34+ *
35+ * @param $img_url
36+ * @return string
37+ */
38+ public static function getOriginal ($ img_url )
39+ {
40+ $ img_ext = pathinfo ($ img_url , PATHINFO_EXTENSION );
41+ $ img_path = self ::getOriginalWithoutExt ($ img_url );
42+ if ($ img_url ) {
43+ return $ img_path . '. ' . $ img_ext ;
44+ }
45+ return $ img_url ;
46+ }
47+
48+ /**
49+ * 替换内容图片为自动高
50+ *
51+ * @param $detail
52+ * @return mixed
53+ */
54+ public static function getWapDetailPicReplace ($ detail )
55+ {
56+ preg_match_all ('/<[img|IMG].*?src=[ \'|\"](.*?(?:))[ \'|\"].*?[\/]?>/ ' , $ detail , $ matches );
57+ if (isset ($ matches [1 ])) {
58+ $ matches [1 ] = array_unique ($ matches [1 ]);
59+ foreach ($ matches [1 ] as $ item ) {
60+ $ detail = str_replace ($ item , self ::getAutoHeight ($ item ), $ detail );
61+ }
62+ }
63+ return $ detail ;
64+
65+ }
66+
67+ /**
68+ * 获取自动高
69+ *
70+ * @param $img_url
71+ * @param int $width
72+ * @return string
73+ */
74+ public static function getAutoHeight ($ img_url , $ width = 800 )
75+ {
76+ $ width = intval ($ width * 1.5 );
77+
78+ if ($ img_url ) {
79+ // 允许的域名
80+ $ img_host = parse_url ($ img_url , PHP_URL_HOST );
81+ if (!($ img_host && in_array ($ img_host , self ::$ trust_hosts ))) {
82+ return $ img_url ;
83+ }
84+
85+ // 获取原始图片未带后缀
86+ $ originalImgPath = self ::getOriginalWithoutExt ($ img_url );
87+ if (!$ originalImgPath ) {
88+ return $ img_url ;
89+ }
90+ $ img_ext = pathinfo ($ img_url , PATHINFO_EXTENSION );
91+
92+ if ($ width ) {
93+ return $ originalImgPath . '. ' . $ img_ext . '_ ' . $ width . '-. ' . $ img_ext ;
94+ }
95+ }
96+
97+ return $ img_url ;
98+ }
99+
100+ /**
101+ * 获取自动宽
102+ *
103+ * @param $img_url
104+ * @param int $height
105+ * @return string
106+ */
107+ public static function getAutoWidth ($ img_url , $ height = 200 )
108+ {
109+ $ height = intval ($ height * 1.5 );
110+
111+ if ($ img_url ) {
112+
113+ // 允许的域名
114+ $ img_host = parse_url ($ img_url , PHP_URL_HOST );
115+ if (!($ img_host && in_array ($ img_host , self ::$ trust_hosts ))) {
116+ return $ img_url ;
117+ }
118+
119+ // 获取原始图片未带后缀
120+ $ originalImgPath = self ::getOriginalWithoutExt ($ img_url );
121+ if (!$ originalImgPath ) {
122+ return $ img_url ;
123+ }
124+ $ img_ext = pathinfo ($ img_url , PATHINFO_EXTENSION );
125+
126+ if ($ height ) {
127+ return $ originalImgPath . '. ' . $ img_ext . '_ ' . '- ' . $ height . '. ' . $ img_ext ;
128+ }
129+ }
130+
131+ return $ img_url ;
132+ }
133+
134+ /**
135+ * 按固定尺寸获取图片
136+ *
137+ * @param $img_url
138+ * @param int $w
139+ * @param null $h
140+ * @return string
141+ */
142+ public static function getSize ($ img_url , $ w = 200 , $ h = null )
143+ {
144+ if ($ w < 800 ) {
145+ $ w = intval ($ w * 1.5 );
146+ $ h = intval ($ h * 1.5 );
147+ }
148+
149+ if ($ img_url ) {
150+ // 允许的域名
151+
152+ $ img_host = parse_url ($ img_url , PHP_URL_HOST );
153+ if (!(in_array ($ img_host , self ::$ trust_hosts ))) {
154+ return $ img_url ;
155+ }
156+ // 获取原始图片未带后缀
157+ $ originalImgPath = self ::getOriginalWithoutExt ($ img_url );
158+ if (!$ originalImgPath ) {
159+ return $ img_url ;
160+ }
161+ $ img_ext = pathinfo ($ img_url , PATHINFO_EXTENSION );
162+ // 长宽检测
163+ if ($ w && $ h ) {
164+ return $ originalImgPath . '. ' . $ img_ext . '_ ' . $ w . 'x ' . $ h . '. ' . $ img_ext ;
165+ }
166+
167+ if ($ w && !$ h ) {
168+ return $ originalImgPath . '. ' . $ img_ext . '_ ' . $ w . 'x ' . $ w . '. ' . $ img_ext ;
169+ }
170+
171+ if (!$ w && $ h ) {
172+ return $ originalImgPath . '. ' . $ img_ext . '_ ' . $ h . 'x ' . $ h . '. ' . $ img_ext ;
173+ }
174+
175+ if (!$ w && !$ h ) {
176+ return $ originalImgPath . '. ' . $ img_ext ;
177+ }
178+ }
179+
180+ return $ img_url ;
181+ }
182+
183+ /**
184+ * 获取原始图片未带后缀
185+ *
186+ * @param $img_url
187+ * @return string
188+ */
189+ private static function getOriginalWithoutExt ($ img_url )
190+ {
191+ if ($ img_url ) {
192+ $ img_ext = pathinfo ($ img_url , PATHINFO_EXTENSION );
193+ // like /avatar/M00/00/01/wKgBqFJyG2iAfHD0AAAxCAGuAII166.jpg_430x430.jpg
194+ if (preg_match ('/^(.*)\. ' . $ img_ext . '(_([0-9]+)x([0-9]+)\. ' . $ img_ext . ')?$/is ' , $ img_url , $ match )) {
195+ return $ match [1 ];
196+ }
197+ }
198+ return "" ;
199+ }
200+ }
0 commit comments