<?php $pdf_file = './pdf/demo.pdf'; $save_to = './jpg/demo.jpg'; //make sure that apache has permissions to write in this folder! (common problem) //execute ImageMagick command 'convert' and convert PDF to JPG with applied settings exec('convert "'.$pdf_file.'" -colorspace RGB -resize 800 "'.$save_to.'"', $output, $return_var); if($return_var == 0) { //if exec successfuly converted pdf to jpg print "Conversion OK"; } else print "Conversion failed.".$output; ?>
44. 使用 tinyurl 生成短网址
function get_tiny_url($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch); curl_close($ch); return $data; }
语法:
<?php $url = "http://blog.koonk.com/2015/07/Hello-World"; $tinyurl = get_tiny_url($url); echo $tinyurl; ?>
45. youtube 下载链接生成器
使用下面的 PHP 片段可以让你的用户下载 Youtube 视频
function str_between($string, $start, $end) { $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } function get_youtube_download_link(){ $youtube_link = $_GET['youtube']; $youtube_page = file_get_contents($youtube_link); $v_id = str_between($youtube_page, "&video_id=", "&"); $t_id = str_between($youtube_page, "&t=", "&"); $flv_link = "http://www.youtube.com/get_video?video_id=$v_id&t=$t_id"; $hq_flv_link = "http://www.youtube.com/get_video?video_id=$v_id&t=$t_id&fmt=6"; $mp4_link = "http://www.youtube.com/get_video?video_id=$v_id&t=$t_id&fmt=18"; $threegp_link = "http://www.youtube.com/get_video?video_id=$v_id&t=$t_id&fmt=17"; echo "\t\tDownload (right-click > save as):\n\t\t"; echo "<a href=https://www.jb51.net/article/\"$flv_link\">FLV</a>\n\t\t"; echo "<a href=https://www.jb51.net/article/\"$hq_flv_link\">HQ FLV (if available)</a>\n\t\t"; echo "<a href=https://www.jb51.net/article/\"$mp4_link\">MP4</a>\n\t\t"; echo "<a href=https://www.jb51.net/article/\"$threegp_link\">3GP</a>\n"; }
46. Facebook 样式的时间戳
Facebook (x mins age, y hours ago etc) function nicetime($date) { if(empty($date)) { return "No date provided"; } $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array("60","60","24","7","4.35","12","10"); $now = time(); $unix_date = strtotime($date); // check validity of date if(empty($unix_date)) { return "Bad date"; } // is it future date or past date if($now > $unix_date) { $difference = $now - $unix_date; $tense = "ago"; } else { $difference = $unix_date - $now; $tense = "from now"; } for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference /= $lengths[$j]; } $difference = round($difference); if($difference != 1) { $periods[$j].= "s"; } return "$difference $periods[$j] {$tense}"; }
语法:
<?php $date = "2015-07-05 03:45"; $result = nicetime($date); // 2 days ago ?>