UCH远程图片本地化添加水印

*支持远程图片本地化(包括新浪、腾讯、百度、网易等网站的图片。)
*支持生成缩略片
*支持图片做为文章附件

新浪的图片把后缀取消掉了,腾讯的图片加密过了,

<strong>Q:如何获取新浪,腾讯的图片文件?</strong>
A:可先对图片的地址做如下处理:
http://www.gonganren.com/img.jpg?src=这里填任意加密后的图片地址或防盗链的图片地址

经处理后的图片地址支持被在线采集。

<strong>以下是UCH图片本地化方法,新建两个PHP文件:</strong>

<!–more–>
/source/dedehttpdown.class.php
/source/GetRemoteImage.php

dedehttpdown.class.php
<pre>@set_time_limit(0);

class DedeHttpDown
{
var $m_url = ”;
var $m_urlpath = ”;
var $m_scheme = ‘http’;
var $m_host = ”;
var $m_port = ’80’;
var $m_user = ”;
var $m_pass = ”;
var $m_path = ‘/’;
var $m_query = ”;
var $m_fp = ”;
var $m_error = ”;
var $m_httphead = ”;
var $m_html = ”;
var $m_puthead = ”;
var $BaseUrlPath = ”;
var $HomeUrl = ”;
var $reTry = 0;
var $JumpCount = 0;

//初始化系统
function PrivateInit($url)
{
if($url==”) {
return ;
}
$urls = ”;
$urls = @parse_url($url);
$this-&gt;m_url = $url;
if(is_array($urls))
{
$this-&gt;m_host = $urls[“host”];
if(!empty($urls[“scheme”]))
{
$this-&gt;m_scheme = $urls[“scheme”];
}
if(!empty($urls[“user”]))
{
$this-&gt;m_user = $urls[“user”];
}
if(!empty($urls[“pass”]))
{
$this-&gt;m_pass = $urls[“pass”];
}
if(!empty($urls[“port”]))
{
$this-&gt;m_port = $urls[“port”];
}
if(!empty($urls[“path”]))
{
$this-&gt;m_path = $urls[“path”];
}
$this-&gt;m_urlpath = $this-&gt;m_path;
if(!empty($urls[“query”]))
{
$this-&gt;m_query = $urls[“query”];
$this-&gt;m_urlpath .= “?”.$this-&gt;m_query;
}
$this-&gt;HomeUrl = $urls[“host”];
$this-&gt;BaseUrlPath = $this-&gt;HomeUrl.$urls[“path”];
$this-&gt;BaseUrlPath = preg_replace(“/\/([^\/]*)\.(.*)$/”,”/”,$this-&gt;BaseUrlPath);
$this-&gt;BaseUrlPath = preg_replace(“/\/$/”,””,$this-&gt;BaseUrlPath);
}
}

function ResetAny()
{
//重设各参数
$this-&gt;m_url = “”;
$this-&gt;m_urlpath = “”;
$this-&gt;m_scheme = “http”;
$this-&gt;m_host = “”;
$this-&gt;m_port = “80”;
$this-&gt;m_user = “”;
$this-&gt;m_pass = “”;
$this-&gt;m_path = “/”;
$this-&gt;m_query = “”;
$this-&gt;m_error = “”;
}

//打开指定网址
function OpenUrl($url,$requestType=”GET”)
{
$this-&gt;ResetAny();
$this-&gt;JumpCount = 0;
$this-&gt;m_httphead = Array() ;
$this-&gt;m_html = ”;
$this-&gt;reTry = 0;
$this-&gt;Close();

//初始化系统
$this-&gt;PrivateInit($url);
$this-&gt;PrivateStartSession($requestType);
}

//转到303重定向网址
function JumpOpenUrl($url)
{
$this-&gt;ResetAny();
$this-&gt;JumpCount++;
$this-&gt;m_httphead = Array() ;
$this-&gt;m_html = “”;
$this-&gt;Close();

//初始化系统
$this-&gt;PrivateInit($url);
$this-&gt;PrivateStartSession(‘GET’);
}

//获得某操作错误的原因
function printError()
{
echo “错误信息:”.$this-&gt;m_error;
echo ”
具体返回头:
“;
foreach($this-&gt;m_httphead as $k=&gt;$v){ echo “$k =&gt; $v
\r\n”; }
}

//判别用Get方法发送的头的应答结果是否正确
function IsGetOK()
{
if( ereg(“^2”,$this-&gt;GetHead(“http-state”)) )
{
return true;
}
else
{
$this-&gt;m_error .= $this-&gt;GetHead(“http-state”).” – “.$this-&gt;GetHead(“http-describe”).”
“;
return false;
}
}

//看看返回的网页是否是text类型
function IsText()
{
if( ereg(“^2”,$this-&gt;GetHead(“http-state”)) &amp;&amp; eregi(“text|xml”,$this-&gt;GetHead(“content-type”)) )
{
return true;
}
else
{
$this-&gt;m_error .= “内容为非文本类型或网址重定向
“;
return false;
}
}

//判断返回的网页是否是特定的类型
function IsContentType($ctype)
{
if(ereg(“^2”,$this-&gt;GetHead(“http-state”))
&amp;&amp; $this-&gt;GetHead(“content-type”)==strtolower($ctype))
{    return true; }
else
{
$this-&gt;m_error .= “类型不对 “.$this-&gt;GetHead(“content-type”).”
“;
return false;
}
}

//用Http协议下载文件
function SaveToBin($savefilename)
{
if(!$this-&gt;IsGetOK())
{
return false;
}
if(@feof($this-&gt;m_fp))
{
$this-&gt;m_error = “连接已经关闭!”; return false;
}
$fp = fopen($savefilename,”w”);
while(!feof($this-&gt;m_fp))
{
fwrite($fp,fread($this-&gt;m_fp,1024));
}
fclose($this-&gt;m_fp);
fclose($fp);
return true;
}

//保存网页内容为Text文件
function SaveToText($savefilename)
{
if($this-&gt;IsText())
{
$this-&gt;SaveBinFile($savefilename);
}
else
{
return “”;
}
}

//用Http协议获得一个网页的内容
function GetHtml()
{
if(!$this-&gt;IsText())
{
return ”;
}
if($this-&gt;m_html!=”)
{
return $this-&gt;m_html;
}
if(!$this-&gt;m_fp||@feof($this-&gt;m_fp))
{
return ”;
}
while(!feof($this-&gt;m_fp))
{
$this-&gt;m_html .= fgets($this-&gt;m_fp,256);
}
@fclose($this-&gt;m_fp);
return $this-&gt;m_html;
}

//开始HTTP会话
function PrivateStartSession($requestType=”GET”)
{
if(!$this-&gt;PrivateOpenHost())
{
$this-&gt;m_error .= “打开远程主机出错!”;
return false;
}
$this-&gt;reTry++;
if($this-&gt;GetHead(“http-edition”)==”HTTP/1.1″)
{
$httpv = “HTTP/1.1”;
}
else
{
$httpv = “HTTP/1.0″;
}
$ps = explode(‘?’,$this-&gt;m_urlpath);

$headString = ”;

//发送固定的起始请求头GET、Host信息
if($requestType==”GET”)
{
$headString .= “GET “.$this-&gt;m_urlpath.” $httpv\r\n”;
}
else
{
$headString .= “POST “.$ps[0].” $httpv\r\n”;
}
$this-&gt;m_puthead[“Host”] = $this-&gt;m_host;

//发送用户自定义的请求头
if(!isset($this-&gt;m_puthead[“Accept”]))
{
$this-&gt;m_puthead[“Accept”] = “*/*”;
}
if(!isset($this-&gt;m_puthead[“User-Agent”]))
{
$this-&gt;m_puthead[“User-Agent”] = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)”;
}
if(!isset($this-&gt;m_puthead[“Refer”]))
{
$this-&gt;m_puthead[“Refer”] = “http://”.$this-&gt;m_puthead[“Host”];
}

foreach($this-&gt;m_puthead as $k=&gt;$v)
{
$k = trim($k);
$v = trim($v);
if($k!=””&amp;&amp;$v!=””)
{
$headString .= “$k: $v\r\n”;
}
}
fputs($this-&gt;m_fp, $headString);
if($requestType==”POST”)
{
$postdata = “”;
if(count($ps)&gt;1)
{
for($i=1;$im_fp,”Content-Type: application/x-www-form-urlencoded\r\n”);
fputs($this-&gt;m_fp,”Content-Length: $plen\r\n”);
}

//发送固定的结束请求头
//HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
if($httpv==”HTTP/1.1″)
{
fputs($this-&gt;m_fp,”Connection: Close\r\n\r\n”);
}
else
{
fputs($this-&gt;m_fp,”\r\n”);
}
if($requestType==”POST”)
{
fputs($this-&gt;m_fp,$postdata);
}

//获取应答头状态信息
$httpstas = explode(” “,fgets($this-&gt;m_fp,256));
$this-&gt;m_httphead[“http-edition”] = trim($httpstas[0]);
$this-&gt;m_httphead[“http-state”] = trim($httpstas[1]);
$this-&gt;m_httphead[“http-describe”] = “”;
for($i=2;$im_httphead[“http-describe”] .= ” “.trim($httpstas[$i]);
}

//获取详细应答头
while(!feof($this-&gt;m_fp))
{
$line = trim(fgets($this-&gt;m_fp,256));
if($line == “”)
{
break;
}
$hkey = “”;
$hvalue = “”;
$v = 0;
for($i=0;$im_httphead[strtolower($hkey)] = trim($hvalue);
}
}

//如果连接被不正常关闭,重试
if(feof($this-&gt;m_fp))
{
if($this-&gt;reTry &gt; 10)
{
return false;
}
$this-&gt;PrivateStartSession($requestType);
}

//判断是否是3xx开头的应答
if(ereg(“^3”,$this-&gt;m_httphead[“http-state”]))
{
if($this-&gt;JumpCount &gt; 3)
{
return;
}
if(isset($this-&gt;m_httphead[“location”]))
{
$newurl = $this-&gt;m_httphead[“location”];
if(eregi(“^http”,$newurl))
{
$this-&gt;JumpOpenUrl($newurl);
}
else
{
$newurl = $this-&gt;FillUrl($newurl);
$this-&gt;JumpOpenUrl($newurl);
}
}
else
{
$this-&gt;m_error = “无法识别的答复!”;
}
}
}

//获得一个Http头的值
function GetHead($headname)
{
$headname = strtolower($headname);
return isset($this-&gt;m_httphead[$headname]) ? $this-&gt;m_httphead[$headname] : ”;
}

//设置Http头的值
function SetHead($skey,$svalue)
{
$this-&gt;m_puthead[$skey] = $svalue;
}

//打开连接
function PrivateOpenHost()
{
if($this-&gt;m_host==””)
{
return false;
}
$errno = “”;
$errstr = “”;
$this-&gt;m_fp = @fsockopen($this-&gt;m_host, $this-&gt;m_port, $errno, $errstr,10);
if(!$this-&gt;m_fp)
{
$this-&gt;m_error = $errstr;
return false;
}
else
{
return true;
}
}

//关闭连接
function Close()
{
@fclose($this-&gt;m_fp);
}

//补全相对网址
function FillUrl($surl)
{
$i = 0;
$dstr = “”;
$pstr = “”;
$okurl = “”;
$pathStep = 0;
$surl = trim($surl);
if($surl==””)
{
return “”;
}
$pos = strpos($surl,”#”);
if($pos&gt;0)
{
$surl = substr($surl,0,$pos);
}
if($surl[0]==”/”)
{
$okurl = “http://”.$this-&gt;HomeUrl.$surl;
}
else if($surl[0]==”.”)
{
if(strlen($surl)&lt;=1)
{
return “”;
}
else if($surl[1]==”/”)
{
$okurl = “http://”.$this-&gt;BaseUrlPath.”/”.substr($surl,2,strlen($surl)-2);
}
else
{
$urls = explode(“/”,$surl);
foreach($urls as $u)
{
if($u==”..”)
{
$pathStep++;
}
else if($iBaseUrlPath);
if(count($urls) &lt;= $pathStep)
{
return “”;
}
else
{
$pstr = “http://”;
for($i=0;$i&lt;7)
{
$okurl = “http://”.$this-&gt;BaseUrlPath.”/”.$surl;
}
else if(strtolower(substr($surl,0,7))==”http://”)
{
$okurl = $surl;
}
else
{
$okurl = “http://”.$this-&gt;BaseUrlPath.”/”.$surl;
}
}
$okurl = eregi_replace(“^(http://)”,””,$okurl);
$okurl = eregi_replace(“/{1,}”,”/”,$okurl);
return “http://”.$okurl;
}
}
</pre>
GetRemoteImage.php
<pre>if(!defined(‘IN_UCHOME’)) {
exit(‘Access Denied’);
}

include_once(S_ROOT.’./source/dedehttpdown.class.php’); //dedehttpdown.class.php文件来源于dedecms
function MyDate($format=’Y-m-d H:i:s’,$timest=0) {
global $cli_time;
$addtime = $cli_time * 3600;
if(empty($format)) {
$format = ‘Y-m-d H:i:s’;
}
return gmdate ($format,$timest+$addtime);
}

function dd2char($ddnum) {
$ddnum = strval($ddnum);
$slen = strlen($ddnum);
$okdd = ”;
$nn = ”;
for($i=0;$i&lt;$slen;$i++) {
if(isset($ddnum[$i+1])) {
$n = $ddnum[$i].$ddnum[$i+1];
if( ($n&gt;96 &amp;&amp; $n&lt;123) || ($n&gt;64 &amp;&amp; $n&lt;91) ) {
$okdd .= chr($n);
$i++;
} else {
$okdd .= $ddnum[$i];
}
} else {
$okdd .= $ddnum[$i];
}
}
return $okdd;
}

function GetCurContent($body) {

global $_SC;
$basedir=”;
$uploaddir =$_SC[‘attachdir’];
$htd = new DedeHttpDown();
$basehost = “http://”.$_SERVER[“HTTP_HOST”];
$watermark= ‘1’; //1是0否加水印
$img_array = array();
preg_match_all(“/\<img \s+.*?src=[\’\”]*([a-z0-9\/\-_+=.~!%@?#%&;:$\\()|]+)[\’\”\s\/>]+/is”, $body, $img_array);
$img_array = array_unique($img_array[1]);
$imgUrl = $uploaddir.MyDate(“Ym”,time()).’/’.MyDate(“d”,time());
$imgPath = $basedir.$imgUrl;
if(!is_dir($imgPath.’/’)) {
@mkdir($imgPath,0777);
}
$milliSecond = MyDate(‘ymdHis’,time());
foreach($img_array as $key=&gt;$value) {
//set_time_limit(180); //每个图片最长允许下载时间,秒
if(eregi($basehost,$value)) {
continue;
}
if(!eregi(“^http://”,$value)) {
continue;
}
$htd-&gt;OpenUrl($value);
$itype = $htd-&gt;GetHead(“content-type”);
$itype = substr($value,-4,4);
if(!eregi(“\.(jpg|gif|png)”,$itype)) {
if($itype==’image/gif’) {
$itype = “.gif”;
} else if($itype==’image/png’) {
$itype = “.png”;
} else {
$itype = ‘.jpg’;
}
}
$milliSecondN = dd2char($milliSecond.mt_rand(1000,8000));
$value = trim($value);
$rndFileName = $imgPath.’/’.$milliSecondN.’-‘.$key.$itype;
$fileurl = str_replace(“./”,””,$imgUrl).’/’.$milliSecondN.’-‘.$key.$itype;
$rs = $htd-&gt;SaveToBin($rndFileName);
if($rs) {
$body = str_replace($value,$fileurl,$body);

//缩略图
include_once(S_ROOT.’./source/function_image.php’);
$thumbpath = makethumb($filepath);
$thumb = empty($thumbpath)?0:1;

// 添加水印
if(($itype=’.jpg’ || $itype=’.jpeg’ || $itype=’.png’) &amp;&amp; $watermark==’1′) {
@makewatermark($rndFileName);
}
}

}
$htd-&gt;Close();
return $body;
}
</pre>
打开function_blog.php文件:

查找:

$message = $POST[‘message’];

下面加

//增加下载外部图片的代码
include_once(S_ROOT.’./source/GetRemoteImage.php’);
$message=GetCurContent($message);

转载请注明出处:https://www.onexin.net/localization-of-uch-remote-image-watermark/

相关文章:

1、DX无后缀名图片下载,后缀名指(.jpg/.gif)
https://www.onexin.net/dx-extension-picture-download-extension-means-jpg-gif/

2、supesite远程图片本地化并作为文章附件
https://www.onexin.net/supesite-remote-image-localization-and-as-a-post-attachments/

3、远程图片本地化 For wordpress
https://www.onexin.net/remote-image-localization-for-wordpress/

4、UCH日志增加系统分类技巧
https://www.onexin.net/increase-in-uch-log-taxonomy-coup/

5、日志推送到群组方法For UCH2.0
https://www.onexin.net/log-pushed-to-group-method-for-uch2/

Leave a Reply