- <?
- function WaterMark($target){
- $watermark_file = './images/common/watermark.gif';
- $attachinfo = getimagesize($target);
- $watermark_logo = imageCreateFromGIF($watermark_file);
- $logo_w = imageSX($watermark_logo);
- $logo_h = imageSY($watermark_logo);
- $img_w = $attachinfo[0];
- $img_h = $attachinfo[1];
- $wmwidth = $img_w - $logo_w;
- $wmheight = $img_h - $logo_h;
- if($attachinfo['mime'] == 'image/gif') {
- return true;
- }else{
- if(is_readable($watermark_file) && $wmwidth > 100 && $wmheight > 100) {
- switch ($attachinfo['mime']) {
- case 'image/jpeg':
- $dst_photo = imageCreateFromJPEG($target);
- break;
- case 'image/png':
- $dst_photo = imageCreateFromPNG($target);
- break;
- default:
- return true;
- }
- $x = 10;//水印位置
- $y = 10;
-
- imageAlphaBlending($watermark_logo, true);
- imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, 100);
- switch($attachinfo['mime']) {
- case 'image/jpeg':
- imageJPEG($dst_photo, $target);
- break;
- case 'image/png':
- imagePNG($dst_photo, $target);
- break;
- }
- return true;
-
- }
-
-
- }
- }
- function Copyimg($nurl){
-
- $imghost='http://www.hekeren.com/discuz/';
- $Copydir='./attachments/getimg/';
- $watermark=1;//是否加水印
- $imgdate=Gmdate("y-m",time()+3600*8);
- $ext=strtolower(trim(substr(strrchr($nurl, '.'), 1)));
-
- if(!in_array($ext,array('jpg','gif','jpeg','bmp','png')))return $nurl;
- $flag=true;
- $host=array(
- 1=>'hekeren.com',
- 2=>'zcever.com',
- 3=>'www.hekeren.com'
- );
- //以上主机下的图片不copy
- foreach($host as $key =>$value){
- if(strstr ($nurl, $value))$flag=false;
-
- }
- if($flag){
- @CK_mkdir($Copydir.$imgdate);
- $filename=$Copydir.$imgdate.'/'.md5($nurl).'.'.$ext;
- if(@doWriteToimg($filename,$nurl)){
- $nurl=$imghost.$filename;
- if(in_array($ext,array('jpg','jpeg','png'))&&$watermark)WaterMark($filename);
- }
- return $nurl;
- }else{
- return $nurl;
- }
-
-
-
- }
- function doWriteToimg ($fileName,$imgurl,$method="w") {
- //echo $nurl;
- @chmod($fileName,0777);
- if (file_exists($fileName)) {
- return true;
- }
- $data=ReadImg($imgurl);
- if(!$data)return false;
- if(!$f=@fopen($fileName,$method))return false;
- @flock($f,LOCK_EX);
- $fileData=@fwrite($f,$data);
- @fclose($f);
- return $fileData;
- }
- function ReadImg($url){
- //echo $url;
- if(!$str=file_get_contents($url)){
- $temp= parse_url($url);
- $host=$temp[host];
- $port=$temp[port]?$temp[port]:80;
- $referer=$host;
- $fp = fsockopen($host,$port, $errno, $errstr, 30);
- if (!$fp) {
- echo "$errstr ($errno)<br />\n";
- } else {
- $out = "GET ".$url." HTTP/1.1\r\n";
- $out .= "Host: $host\r\n";
- $out .= "Referer: $referer\r\n";
- $out .= "Connection: Close\r\n\r\n";
- fputs($fp, $out);
- while(!feof($fp))
- {
- $temp=fgets($fp,1024);
- $str=$str.$temp;
- }
- fclose($fp);
- }
- }
- return $str;
- }
- function Checkimg($str){
- $exp1='[img]';
- $exp2='[/img]';
- //echo $str;
- $r = explode ($exp1, $str);
- $text=$r[0];
- for ($i = 1; $i < count ($r); ++$i)
- {
- $r1 = explode ($exp2, $r[$i]);
- if ((strstr ($r1[0], 'http://') OR strstr ($r1[0], 'https://')))
- {
- $dourl = $r1[0];
- $nurl=Copyimg($dourl);
- $text.=$exp1.$nurl.$exp2.$r1[1];
- }else{
- $text.=$exp1.$r1[0].$exp2.$r1[1];
- }
- }
- return $text;
- }
- function CK_mkdir($htmldir){
- if (file_exists($htmldir)) {
- return;
- } else {
- @mkdir($htmldir,0777);
- @fclose(fopen($htmldir.'/index.htm', 'w'));
- }
- return;
-
- }
- ?>
复制代码 |