代码非常简单,所以不要有心理压力。我以PHP为例(这两年一直在玩nodejs,php放一边快凉了,昨天要处理一些复杂图片,发现这方面还是PHP顺手一点)。
A 先定义一个页面抓取函数。有了它,可以get和post网页,当然等会抓图片要用到它。我在原来的基础上优化了一下:
<?php function urlOpen($url, $data = null, $ua = 'MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $ua); if ($data) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch); curl_close($ch); return $content; } ?>
这样的话,有data提交,就自动post,没data提交就get。
这个函数写好后,先放php文件的最后,等会直接调用它就行了。
B 写主要部分,处理一些错误的情况。
<?php header("Content-Type:image/png");//设置本文档将出输出的格式为png图片 $string = $_SERVER["QUERY_STRING"]; //为什么要这样,而不用$_GET呢?因为可能图片地址中有&,那样就会被分割为多个参数,就会出错了。 /*换一张空白图片,如果遇到错误,需要用上*/ $im = imagecreate(600, 300); $black = imagecolorallocate($im, 100, 100, 100); //图片背景 $white = imagecolorallocate($im, 255, 255, 255); /*获取图片的真实地址*/ $url = strstr($string, "http"); if (!$url) { imagettftext($im, 18, 0, 200, 100, $white, "./fonts/hwxh.ttf", "Error 001"); imagettftext($im, 14, 0, 150, 150, $white, "./fonts/hwxh.ttf", "请在参数中输入图片的绝对地址。"); imagepng($im); exit(); } @$imgString = urlOpen($url); if ($imgString == "") { imagettftext($im, 18, 0, 200, 100, $white, "./fonts/hwxh.ttf", "Error 002"); imagettftext($im, 14, 0, 70, 150, $white, "./fonts/hwxh.ttf", "加载远程图片失败,请确认图片的地址能正常访问。"); imagepng($im); exit(); } ?>
上面一部分主要是对引用图片时出现错误的情况的处理,一个是没有告诉服务器图片的真实URL,另一个是图片的真实地址无法访问。出现这两种情况,就用图片的方式输出错误提示。
./fonts/hwxh.ttf 这是字体,放在相对路径下的对应位置,也可以不用这个,直接去系统字体里面复制字体的名称也可以。
C 做了那么多准备工作,正事这才开始,开始抓图片并输出:
<?php /*如果没有错误*/ $im = imagecreatefromstring($imgString);//上面已经把图片的内容拿到了,直接用它生成一张图片 $white = imagecolorallocate($im, 255, 255, 255); /*加上水印*/ //imagettftext($im, 12, 0, 20, 20, $white, "./fonts/hwxh.ttf", "水印的文字1"); imagepng($im); ?>
前面一堆东西那么多,真正的东西就3行??
是的,你没看错,就这么点。当然,你可以给图片加上水印什么的,让它更有特色。
最终代码
最后整理后,代码如下:
<?php header("Content-Type:image/png");//设置本文档将出输出的格式为png图片 $string = $_SERVER["QUERY_STRING"]; //为什么要这样,而不用$_GET呢?因为可能图片地址中有&,那样就会被分割为多个参数,就会出错了。 /*换一张空白图片,如果遇到错误,需要用上*/ $im = imagecreate(600, 300); $black = imagecolorallocate($im, 100, 100, 100); //图片背景 $white = imagecolorallocate($im, 255, 255, 255); /*获取图片的真实地址*/ $url = strstr($string, "http"); if (!$url) { imagettftext($im, 18, 0, 200, 100, $white, "./fonts/hwxh.ttf", "Error 001"); imagettftext($im, 14, 0, 150, 150, $white, "./fonts/hwxh.ttf", "请在参数中输入图片的绝对地址。"); imagepng($im); exit(); } @$imgString = urlOpen($url); if ($imgString == "") { imagettftext($im, 18, 0, 200, 100, $white, "./fonts/hwxh.ttf", "Error 002"); imagettftext($im, 14, 0, 70, 150, $white, "./fonts/hwxh.ttf", "加载远程图片失败,请确认图片的地址能正常访问。"); imagepng($im); exit(); } /*如果没有错误*/ $im = imagecreatefromstring($imgString);//上面已经把图片的内容拿到了,直接用它生成一张图片 $white = imagecolorallocate($im, 255, 255, 255); /*加上水印*/ //imagettftext($im, 12, 0, 20, 20, $white, "./fonts/hwxh.ttf", "水印的文字1"); imagepng($im); /*通用远程GET POST函数*/ function urlOpen($url, $data = null, $ua = 'MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $ua); if ($data) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch); curl_close($ch); return $content; } ?>
最后检测
在测试中,成功中转了pixabay上的图片(这网站有防盗链的)。
使用方法
不管是什么语言,都这么访问 http://域名/imgBridge2019.php?url=图片地址(含http)
一些提示
用自己的服务器做中转,如果目标图片太大,那么小水管的服务器肯定是吃不消的,pixabay这种最大尺寸有照片有几M甚至几十M,可以加载速度会很慢很慢,所以可以把php的执行时间设置长一点。
同时,如果服务器带宽很小,建议不要把自己的接口开放给别人用,免费影响自己网站。
源码共享
文章转自:不坑老师
发表评论