· 4 years ago · Jun 13, 2021, 04:26 PM
1<?php
2
3
4/*
5
6Zerohedge Crawler (@zeroedtweets)
7
8Install php with memcache
9Fix all #CHANGE with your credentials
10
11*/
12
13class tweet_bot {
14 function oauth() {
15 require_once(); // #CHANGE link it to your twitteroauth.php file (based on a old version of https://github.com/abraham/twitteroauth)
16 $con = new TwitterOAuth($this->api_key, $this->api_secret, $this->access_token, $this->access_token_secret);
17 return $con;
18 }
19 function tweet($text) {
20 $con = $this->oauth();
21 $status = $con->post('statuses/update', array('status' => $text));
22 return $status;
23 }
24 function tweeturl($text, $url) {
25 $con = $this->oauth();
26 $status = $con->post('statuses/update', array('status' => $text, 'attachment_url' => $url));
27 return $status;
28 }
29 function search($query, $count = 90, $lang = "en", $resulttype = "recent") {
30 $con = $this->oauth();
31 $search = $con->get('search/tweets', array('q' => $query, 'count' => $count, 'lang' => $lang, 'result_type' => $resulttype));
32 return $search;
33 }
34 function timeline($name, $count = 200, $ex_replies = true, $in_rts = false) {
35 $con = $this->oauth();
36 $timeline = $con->get('statuses/user_timeline', array('screen_name' => $name, 'count' => $count, 'exclude_replies' => $ex_replies, 'include_rts' => $in_rts));
37 return $timeline;
38 }
39 function retweet($id, $userid = "00000") {
40 $con = $this->oauth();
41 $retweet = $con->post('statuses/retweet/'.$id);
42
43 return $retweet;
44 }
45 function follow($id) {
46 $con = $this->oauth();
47 $follow = $con->post('friendships/create', array("user_id" => $id));
48 return $follow;
49 }
50 function favorite($id) {
51 $con = $this->oauth();
52 $favorite = $con->post('favorites/create', array("id" => $id));
53 return $favorite;
54 }
55 function unfavorite($id) {
56 $con = $this->oauth();
57 $unfavorite = $con->post('favorites/destroy', array("id" => $id));
58 return $unfavorite;
59 }
60 function setKey($api_key,$api_secret,$access_token,$access_token_secret) {
61 $this->api_key = $api_key;
62 $this->api_secret = $api_secret;
63 $this->access_token = $access_token;
64 $this->access_token_secret = $access_token_secret;
65 }
66}
67
68$api_key= ''; // #CHANGE twitter api key
69$api_secret= ''; // #CHANGE twitter api secret
70$access_token = ''; // #CHANGE twitter access token
71$access_token_key= ''; // #CHANGE twitter access token key
72$twitter_handle = ""; // #CHANGE your twitter handle without @
73$url_home = "https://www.zerohedge.com";
74$url_feed = "http://feeds.feedburner.com/zerohedge/feed";
75
76$cenword_arr = array(""); // Censored words
77$ht_keywords = array(""); // Highlight keys
78$ua_strings = array("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14931", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.9200", "Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", "Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; HTC_IncredibleS_S710e Build/GRJ90) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", "Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; HTC_DesireS_S510e Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:63.0) Gecko/20100101 Firefox/63.0", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/58.0.1", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/4.0; WOW64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0; Maxthon 2.0)", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; Maxthon/3.0)", "Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16", "Mozilla/5.0 (Windows NT 6.0; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 12.14", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101213 Opera/9.80 (Windows NT 6.1; U; zh-tw) Presto/2.7.62 Version/11.01");
79
80$url_arr = array_values(array_merge(fetch_article_urls($url_home), fetch_feed_urls($url_feed)));
81$has_tweeted = false;
82
83$date_time = date("Y-m-d H:i:s");
84
85if(count($url_arr)>0) {
86 foreach($url_arr as $k => $d) {
87 if($data_arr = get_data($url_arr[$k])) {
88 $message = $data_arr[1];
89 foreach($cenword_arr as $word) {
90 if($pos = strpos(strtolower(" ".$message), $word)) {
91 $message = substr_replace(" ".$message, str_repeat("*", (strlen($word)-1)), $pos+1, (strlen($word)-1));
92 }
93 }
94 $message = str_ireplace("'", "‘", $message);
95 $can_tweet = true;
96
97 //if(stripos($data_arr[0], '/health/')) {
98 //$message = $message.' [⚠NSFT]';
99 //}
100
101 if($can_tweet==true && ($resp = post_tweet(html_entity_decode($message)." ".$data_arr[0])) && isset($resp->created_at)) {
102 $has_tweeted = true;
103 }
104
105 set_memcache(md5("tw".$url_arr[$k]), "y", 172800);
106
107 break;
108 }
109 }
110}
111
112if($has_tweeted===false) {
113 //try_rt_self();
114}
115
116die();
117
118function try_rt_self() {
119 $bot= new tweet_bot;
120 $bot->setKey($GLOBALS["api_key"], $GLOBALS["api_secret"], $GLOBALS["access_token"], $GLOBALS["access_token_key"]);
121 $result = $bot->timeline($GLOBALS["twitter_handle"], 100, true, true);
122
123 $date_time = date("Y-m-d H:i:s");
124 $min_since_last = 0;
125 foreach($result as $tweets){
126 $min_since_last = (int)(((new DateTime($date_time))->diff((new DateTime(date('Y-m-d H:i:s', strtotime($tweets->created_at))))))->format("%i"));
127 break;
128 }
129
130 $result = $bot->timeline($GLOBALS["twitter_handle"], 200);
131
132 if($min_since_last>10) {
133 $total=0;
134 foreach($result as $tweets) {
135 $total++;
136 }
137 $choice = rand(6, (($total>18) ? 18 : $total));
138 foreach($result as $key => $tweets) {
139 if($key==$choice) {
140 $tweet_id = $tweets->id_str;
141 if(!get_memcache(md5("twr".$tweet_id)) && ((int)(((new DateTime($date_time))->diff((new DateTime(date('Y-m-d H:i:s', strtotime($tweets->created_at))))))->format("%i")))<1400) {
142 $user_id = $tweets->user->id_str;
143 $result = $bot->retweet($tweet_id, $user_id);
144 set_memcache(md5("twr".$tweet_id), "y", 86400);
145 return true;
146 }
147 else {
148 $choice++;
149 }
150 }
151 }
152 }
153}
154
155function post_tweet($str) {
156 $tweet= new tweet_bot;
157 $tweet->setKey($GLOBALS["api_key"], $GLOBALS["api_secret"], $GLOBALS["access_token"] , $GLOBALS["access_token_key"]);
158 return ($tweet->tweet($str));
159}
160
161
162function get_site_html($url){
163 $ch = curl_init();
164 curl_setopt($ch, CURLOPT_URL, $url);
165 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
166 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
167 curl_setopt($ch, CURLOPT_ENCODING , "");
168 curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
169 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
170 'X-Forwarded-For: '.long2ip(mt_rand()+mt_rand()+mt_rand(0,1)),
171 ));
172 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
173 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,3);
174 curl_setopt($ch, CURLOPT_TIMEOUT, 9);
175 curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS["ua_strings"][rand(0, (count($GLOBALS["ua_strings"])-1))]);
176 $str = curl_exec($ch);
177 $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
178 curl_close ($ch);
179 return (($httpcode!=200 || $str=="") ? false : preg_replace('/(?=<!--)([\s\S]*?-->)/i', '', $str));
180}
181
182function get_data($url) {
183 if(get_memcache(md5("tw".$url))) {
184 return false;
185 }
186 if(($str = get_site_html($url))) {
187 preg_match("/<\s{0,}a\s{0,}href\s{0,}=\s{0,}(\"|')(.[^<]*?)(\"|')\s{0,}>\s{0,}(.*)authored\s{0,}by/i", $str, $matches);
188
189 if(isset($matches[2]) && strlen($matches[2])>0 && strpos(parse_url($matches[2], PHP_URL_HOST), "zerohedge")===false && ($outurl = (parse_url(($matches[2]), PHP_URL_SCHEME).'://'.parse_url(($matches[2]), PHP_URL_HOST).parse_url(($matches[2]), PHP_URL_PATH)))) {
190 if(get_memcache(md5("tw".$outurl))) {
191 return false;
192 }
193
194 $url = ((strpos(parse_url(($matches[2]), PHP_URL_HOST), "realinvestmentadvic.com")!==false) ? $outurl : $url);
195
196 if($url==$outurl) {
197 $str = get_site_html($url);
198 }
199 }
200
201 $title = "";
202 preg_match("/<\s{0,}title\s{0,}(.*?)>(.[^<]*?)<\s{0,}\/\s{0,}title\s{0,}>/i", $str, $matches);
203 if(isset($matches[2]) && strlen($matches[2])>0) {
204 $title = ((strlen(explode("|", $matches[2])[0])>10) ? explode("|", $matches[2])[0] : "");
205 }
206
207
208 preg_match("/<\s{0,}meta\s{0,}name\s{0,}=\s{0,}(\"|')title\s{0,}(\"|')\s{0,}content=\s{0,}(\"|')(.[^<]*?)(\"|')/i", $str, $matches);
209 if(isset($matches[4]) && strlen($matches[4])>0) {
210 $title = $matches[4];
211 }
212
213
214
215 preg_match("/<\s{0,}meta\s{0,}name\s{0,}=\s{0,}(\"|')og\s{0,}:\s{0,}title\s{0,}(\"|')\s{0,}content=\s{0,}(\"|')(.[^<]*?)(\"|')/i", $str, $matches);
216 if(isset($matches[4]) && strlen($matches[4])>0) {
217 $title = $matches[4];
218 }
219
220
221 if($title=="") {
222 preg_match("/<\s{0,}meta\s{0,}name\s{0,}=\s{0,}(\"|')twitter\s{0,}:\s{0,}title\s{0,}(\"|')\s{0,}content=\s{0,}(\"|')(.[^<]*?)(\"|')/i", $str, $matches);
223 if(isset($matches[4]) && strlen($matches[4])>0) {
224 $title = $matches[4];
225 }
226 }
227
228 if($title=="") {
229 return false;
230 }
231
232 $title = ((strlen($title)>200) ? substr($title, 0, 200)."..." : $title);
233
234 $description = "";
235 preg_match("/<\s{0,}meta\s{0,}name\s{0,}=\s{0,}(\"|')description\s{0,}(\"|')\s{0,}content=\s{0,}(\"|')(.[^<]*?)(\"|')/i", $str, $matches);
236 if(isset($matches[4]) && strlen($matches[4])>0) {
237 $description = $matches[4];
238 }
239
240 preg_match("/<\s{0,}meta\s{0,}name\s{0,}=\s{0,}(\"|')og\s{0,}:\s{0,}description\s{0,}(\"|')\s{0,}content=\s{0,}(\"|')(.[^<]*?)(\"|')/i", $str, $matches);
241 if(isset($matches[4]) && strlen($matches[4])>0) {
242 $description = $matches[4];
243 }
244
245 preg_match("/<\s{0,}meta\s{0,}name\s{0,}=\s{0,}(\"|')twitter\s{0,}:\s{0,}description\s{0,}(\"|')\s{0,}content=\s{0,}(\"|')(.[^<]*?)(\"|')/i", $str, $matches);
246 if(isset($matches[4]) && strlen($matches[4])>0) {
247 $description = $matches[4];
248 }
249
250 if(stripos($title, "election")!==false || stripos($description, "election")!==false) {
251 set_memcache(md5("tw".$url), "y", 172800);
252 return false;
253 }
254
255 $description = ((strlen($description)>200) ? substr($description, 0, 200)."..." : $description);
256
257 if(strlen($title)>0 || strlen($description)>0) {
258 //return (((strlen($description)>strlen($title)) && ((strlen($description))+25)<245 && (rand(1,3) == 2)) ? $description.'' : $title.' #FreeZeroHedge');
259
260 //return (((strlen($description)>strlen($title)) && ((strlen($description))+25)<245 && (rand(1,3) == 2)) ? $description.' #FreeZeroHedge' : ((strpos(strtolower($title), "china")!==false) ? str_ireplace("china", "#China", $title) : $title).'');
261
262 $author = ((strpos(parse_url($url, PHP_URL_HOST), "zerohedge")!==false) ? ' - zerohedge' : ' - ['.preg_replace("/^([a-zA-Z0-9].*\.)?([a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z.]{2,})$/", '$2', parse_url($url, PHP_URL_HOST)).']');
263
264 return array($url, (((strlen($description)>strlen($title)) && ((strlen($description))+25)<245 && (rand(1,2) == 2)) ? hashtag_string($GLOBALS["ht_keywords"], $description, true).$author : hashtag_string($GLOBALS["ht_keywords"], $title, true).$author), get_domain($url), $title, $description, $str);
265 }
266 else return false;
267 }
268 else return false;
269}
270
271function hashtag_string($key_arr, $str, $first_only=true) {
272 foreach($key_arr as $word) {
273 if(($pos = strpos(strtolower($str), $word))!==false && $str[($pos-1)]!="#") {
274 $str = substr_replace($str, "#", $pos, 0);
275 if($first_only===true) { return $str; }
276 }
277 }
278 return $str;
279}
280
281function get_domain($url) {
282 if((substr($url,0,strlen('http://')) <> 'http://') && (substr($url,0,strlen('https://')) <> 'https://')) $url = 'http://'.$url;
283 preg_match("/[a-z0-9\-]{1,63}\.[a-z\.]{2,6}$/", parse_url($url, PHP_URL_HOST), $_domain_tld);
284 return $_domain_tld[0];
285}
286
287function fetch_article_urls($url) {
288 $p_arr = array();
289 if(($str = get_site_html($url))) {
290 preg_match_all('/<\s{0,}article\s{0,}role="article"\s{0,}about=\s{0,}("|\')(.[^<]*?)("|\')/i', $str, $matches);
291 if(isset($matches[2]) && count($matches[2])>0) {
292 foreach($matches[2] as $path) {
293 if(strlen($path)>0) {
294 array_push($p_arr, $url.$path);
295 }
296 }
297 }
298 }
299 return $p_arr;
300}
301
302function fetch_feed_urls($url) {
303 $p_arr = array();
304 if(($str = get_site_html($url))) {
305 preg_match_all('/<\s{0,}feedburner:origLink\s{0,}>\s{0,}(.[^<]*?)<\s{0,}\/\s{0,}feedburner:origLink\s{0,}>/i', $str, $matches);
306 if(isset($matches[1]) && count($matches[1])>0) {
307 foreach($matches[1] as $url) {
308 if(strlen($url)>0) {
309 array_push($p_arr, $url);
310 }
311 }
312 }
313 }
314 return ((count($p_arr)>20) ? array_slice($p_arr, 0, 20) : $p_arr);
315}
316
317function set_memcache($key, $value, $expiry = false) {
318 $expiry = (is_int($expiry) ? $expiry : 300);
319 $m = new Memcached();
320 $m->addServer('localhost', 11211);
321 $m->set($key, $value, $expiry);
322 $m->quit();
323}
324function get_memcache($key) {
325 $m = new Memcached();
326 $m->addServer('localhost', 11211);
327 if(($data = $m->get($key))) {
328 $m->quit();
329 return $data;
330 }
331 else {
332 $m->quit();
333 return false;
334 }
335}
336function delete_memcache($key) {
337 $m = new Memcached();
338 $m->addServer('localhost', 11211);
339 $m->delete($key);
340 $m->quit();
341}
342
343function txt_read($source) {
344 if(str_replace('.gz', '', $source) == $source) {
345 if(file_exists($source)) {
346 $data = file_get_contents($source);
347 if(txt_to_gz($source)) {
348 unlink($source);
349 return $data;
350 } else return false;
351 }
352 else if(file_exists($source.'.gz')) {
353 $temp = gz_to_txt($source.'.gz');
354 if(file_exists($temp)) {
355 $data = file_get_contents($temp);
356 unlink($temp);
357 return $data;
358 } else return false;
359 }
360 else return false;
361 }
362 else {
363 if(file_exists($source)) {
364 $temp = gz_to_txt($source);
365 if(file_exists($temp)) {
366 $data = file_get_contents($temp);
367 unlink($temp);
368 return $data;
369 } else return false;
370 }
371 else {
372 $temp = str_replace('.gz', '', $source);
373 if(file_exists($temp)) {
374 if(txt_to_gz($temp)) {
375 $data = file_get_contents($temp);
376 unlink($temp);
377 return $data;
378 } else return false;
379 } else return false;
380 }
381 }
382}
383function txt_write($source, $data, $type = 'w') { //$type - w / a
384 if(str_replace('.gz', '', $source) == $source) {
385 if(file_exists($source)) {
386 $temp = $source;
387 $source = txt_to_gz($source);
388 unlink($temp);
389 }
390 else {
391 $source = $source.'.gz';
392 }
393 }
394 if($type!='w' && $type!='a') { return false; }
395 else if($type=='a') {
396 if(file_exists($source)) {
397 $temp = gz_to_txt($source);
398 if(file_exists($temp)) {
399 $file = fopen($temp, 'a');
400 fwrite($file, $data);
401 fclose($file);
402 if(txt_to_gz($temp)) {
403 $data = file_get_contents($temp);
404 unlink($temp);
405 return $data;
406 } else return false;
407 } else return false;
408 }
409 else {
410 $temp = str_replace('.gz', '', $source);
411 $file = fopen($temp, 'w');
412 fwrite($file, $data);
413 fclose($file);
414 if(txt_to_gz($temp)) {
415 $data = file_get_contents($temp);
416 unlink($temp);
417 return $data;
418 } else return false;
419 }
420 }
421 else {
422 $temp = str_replace('.gz', '', $source);
423 $file = fopen($temp, 'w');
424 fwrite($file, $data);
425 fclose($file);
426 if(txt_to_gz($temp)) {
427 $data = file_get_contents($temp);
428 unlink($temp);
429 return $data;
430 } else return false;
431 }
432}
433function txt_to_gz($source, $level = 9){
434 $dest = $source.'.gz';
435 $mode = 'wb'.$level;
436 $error = false;
437 if ($fp_out = gzopen($dest, $mode)) {
438 if ($fp_in = fopen($source,'rb')) {
439 while (!feof($fp_in))
440 gzwrite($fp_out, fread($fp_in, 1024 * 512));
441 fclose($fp_in);
442 } else {
443 $error = true;
444 }
445 gzclose($fp_out);
446 } else {
447 $error = true;
448 }
449 if ($error) {
450 return false;
451 } else {
452 return $dest;
453 }
454}
455function gz_to_txt($source) {
456 $buffer_size = 4096;
457 $dest = str_replace('.gz', '', $source);
458 $error = false;
459 if($fp_in = gzopen($source, 'rb')) {
460 if($fp_out = fopen($dest, 'wb')) {
461 while(!gzeof($fp_in)) {
462 fwrite($fp_out, gzread($fp_in, $buffer_size));
463 }
464 fclose($fp_out);
465 } else {
466 $error = true;
467 }
468 } else {
469 $error = true;
470 }
471 if ($error) {
472 return false;
473 } else {
474 return $dest;
475 }
476}
477
478function strip_tags_content($text, $tags = '', $invert = FALSE) {
479 preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
480 $tags = array_unique($tags[1]);
481
482 if(is_array($tags) AND count($tags) > 0) {
483 if($invert == FALSE) {
484 return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
485 }
486 else {
487 return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
488 }
489 }
490 elseif($invert == FALSE) {
491 return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
492 }
493 return $text;
494}
495function get_display_image($str) {
496 if (preg_match("/<\s{0,}meta\s{0,}property\s{0,}=\s{0,}(\"|')og:image(\"|')\s{0,}/i", $str)) {
497 $tag_xy = array();
498 preg_match("/<\s{0,}meta\s{0,}property\s{0,}=\s{0,}(\"|')og:image(\"|')\s{0,}content\s{0,}=\s{0,}(\"|')(.[^<]*?)(\"|')\s{0,}[^<]*?\>/i", $str, $tag_xy);
499 if (count($tag_xy) > 4) {
500 $image_item_extract = $tag_xy[4];
501 $file_headers = @get_headers($image_item_extract);
502 if(!$file_headers || $file_headers[0] != 'HTTP/1.1 404 Not Found') {
503 list($width, $height, $type) = @getimagesize($image_item_extract);
504 // $type - 1 = GIF; 2 = JPG; 3 = PNG
505 if($width>399 && $height>199 && ($width/$height)>1 && ($type==2||$type==3)) {
506 return $image_item_extract;
507 }
508 else {}
509 }
510 else {}
511 } else {}
512 }
513 else {}
514
515
516 $matches = array();
517 preg_match_all('/(<\s{0,}img(.[^<]*?)src\s{0,}=\s{0,}["\'](http.[^<|\"|\']*?\.gif|http.[^<|\"|\']*?\.png|http.[^<|\"|\']*?\.jpg|http.[^<|\"|\']*?\.jpeg)|<\s{0,}a(.[^<]*?)href\s{0,}=\s{0,}["\'](http.[^<|\"|\']*?\.gif|http.[^<|\"|\']*?\.png|http.[^<|\"|\']*?\.jpg|http.[^<|\"|\']*?\.jpeg))/i', $str, $matches);
518
519 $image_array_extract = array();
520 if(count($matches)>1) {
521 for($i=0; $i<count($matches); $i++) {
522 foreach($matches[$i] as $item) {
523 if(preg_match('/\.(jpeg|jpg|png|gif)$/i', $item)) {
524 array_push($image_array_extract, $item);
525 }
526 }
527 }
528 function compare_ext($item1, $item2) {
529 if(preg_match('/\.(jpeg|jpg|png)$/i', $item1) && preg_match('/\.(gif)$/i', $item2)) {
530 return 1;
531 }
532 else if(preg_match('/\.(jpeg|jpg|png)$/i', $item1) && preg_match('/\.(jpeg|jpg|png)$/i', $item2)) {
533 return 0;
534 }
535 else {
536 return -1;
537 }
538 }
539 usort($image_array_extract, 'compare_ext');
540 }
541 else {
542 return null;
543 }
544
545 if(count($image_array_extract)>1) {
546 for($i=0; $i<count($image_array_extract); $i++) {
547 $image_item_extract = $image_array_extract[$i];
548 $file_headers = @get_headers($image_item_extract);
549 if(!$file_headers || $file_headers[0] != 'HTTP/1.1 404 Not Found') {
550 list($width, $height, $type) = @getimagesize($image_item_extract);
551 // $type - 1 = GIF; 2 = JPG; 3 = PNG
552 if($width>399 && $height>199 && ($width/$height)>1 && ($type==2||$type==3)) {
553 return $image_item_extract;
554 }
555 else if(($i == count($image_array_extract)-1) || $i > 30) {
556 return null;
557 }
558 else {}
559 }
560 else if(($i == count($image_array_extract)-1) || $i > 30) {
561 return null;
562 }
563 else {}
564 }
565 }
566 else {
567 return null;
568 }
569}
570
571function get_scaled_image($file, $max = 600, $quality = 30) {
572 $source_pic = $file;
573 $max_width = $max;
574 $max_height = $max;
575
576 $file = preg_replace('/\s+/', '%20', $file);
577
578 list($width, $height, $image_type) = getimagesize($file);
579
580 switch ($image_type) {
581 case 1: $src = imagecreatefromgif($file); break;
582 case 2: $src = imagecreatefromjpeg($file); break;
583 case 3: $src = imagecreatefrompng($file); break;
584 default: return file_get_contents($source_pic); break;
585 }
586
587 $x_ratio = $max_width / $width;
588 $y_ratio = $max_height / $height;
589
590 if(($width <= $max_width) && ($height <= $max_height)) {
591 $tn_width = $width;
592 $tn_height = $height;
593 }
594 elseif (($x_ratio * $height) < $max_height){
595 $tn_height = ceil($x_ratio * $height);
596 $tn_width = $max_width;
597 }
598 else {
599 $tn_width = ceil($y_ratio * $width);
600 $tn_height = $max_height;
601 }
602
603 $tmp = imagecreatetruecolor($tn_width,$tn_height);
604
605 if(($image_type == 1) OR ($image_type==3)) {
606 imagealphablending($tmp, false);
607 imagesavealpha($tmp,true);
608 $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
609 imagefilledrectangle($tmp, 0, 0, $tn_width, $tn_height, $transparent);
610 }
611 imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
612
613 ob_start();
614
615 switch ($image_type) {
616 case 1: imagegif($tmp); break;
617 case 2: imagejpeg($tmp, NULL, $quality); break;
618 case 3: imagepng($tmp, NULL, (10-round_down(($quality/10), 0))); break;
619 default: return file_get_contents($source_pic); break;
620 }
621
622 $final_image = ob_get_contents();
623 ob_end_clean();
624
625 return $final_image; //blob
626}
627
628function get_scaled_jpeg($file, $max = 600, $quality = 30) {
629 $source_pic = $file;
630 $max_width = $max;
631 $max_height = $max;
632
633 $file = preg_replace('/\s+/', '%20', $file);
634
635 list($width, $height, $image_type) = getimagesize($file);
636
637 switch ($image_type) {
638 case 1: $src = imagecreatefromgif($file); break;
639 case 2: $src = imagecreatefromjpeg($file); break;
640 case 3: $src = imagecreatefrompng($file); break;
641 default: return file_get_contents($source_pic); break;
642 }
643
644 $x_ratio = $max_width / $width;
645 $y_ratio = $max_height / $height;
646
647 if(($width <= $max_width) && ($height <= $max_height)) {
648 $tn_width = $width;
649 $tn_height = $height;
650 }
651 elseif (($x_ratio * $height) < $max_height){
652 $tn_height = ceil($x_ratio * $height);
653 $tn_width = $max_width;
654 }
655 else {
656 $tn_width = ceil($y_ratio * $width);
657 $tn_height = $max_height;
658 }
659
660 $tmp = imagecreatetruecolor($tn_width,$tn_height);
661
662 if(($image_type == 1) OR ($image_type==3)) {
663 imagealphablending($tmp, false);
664 imagesavealpha($tmp,true);
665 $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
666 imagefilledrectangle($tmp, 0, 0, $tn_width, $tn_height, $transparent);
667 }
668 imagecopyresampled($tmp, $src, 0,0,0,0,$tn_width, $tn_height,$width,$height);
669
670 ob_start();
671
672 if($image_type<=3) {
673 imagejpeg($tmp, NULL, $quality);
674 $final_image = ob_get_contents();
675 ob_end_clean();
676 imagedestroy($src);
677 imagedestroy($tmp);
678 return $final_image; //blob
679 }
680 else {
681 ob_end_clean();
682 imagedestroy($src);
683 imagedestroy($tmp);
684 return file_get_contents($source_pic);
685 }
686}
687
688function round_up($number, $precision = 2) {
689 $fig = (int) str_pad('1', $precision, '0');
690 return (ceil($number * $fig) / $fig);
691}
692
693function round_down($number, $precision = 2) {
694 $fig = (int) str_pad('1', $precision, '0');
695 return (floor($number * $fig) / $fig);
696}
697
698?>
699