kindeditor php自动获取远程图片php

/ / 2016-10-11   阅读:2487
关键文件一、remoteimg.php <?php /**  * Function: 获取远程图片并把它保存到本地  *   * 本PHP程序是演示程序,建议不要直接在实际项目中使用。  * 如果您确定直接使用本程序,...
关键文件一、remoteimg.php
<?php
/**
 * Function: 获取远程图片并把它保存到本地
 * 
 * 本PHP程序是演示程序,建议不要直接在实际项目中使用。
 * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
 * 确定您有把文件写入本地服务器的权限
 */
// 权限认证
/*
session_start();
if(!isset($_SESSION['username']))
{
echo 'Access is not allowed.';
session_destroy();
die();
}
*/

//文件保存目录路径
$imgPath = '../../../../Uploads/'.date('Ym').'/';
//文件保存目录URL
$imgUrl = str_replace('Public/Js/kindeditor/php/','',dirname($_SERVER['PHP_SELF']) . '/Uploads/'.date('Ym').'/');
//alert(str_replace('Public/Js/kindeditor/php/','',dirname($_SERVER['PHP_SELF']) . '/Uploads/'));
if((isset($_POST['str']))&&(!empty($_POST['str'])))
{
//$body = stripslashes($saveremoteimg);
$body=$_POST['str'];
$img_array = array();
preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array);
$img_array = array_unique($img_array[2]);
set_time_limit(0);
$milliSecond = date("dHis") . '_';
if(!is_dir($imgPath)) @mkdir($imgPath,0777);
foreach($img_array as $key =>$value)
{
$value = trim($value);
$get_file = @file_get_contents($value);
$rndFileName = $imgPath.$milliSecond.$key.'.'.substr($value,-3,3);
$fileurl = $imgUrl.$milliSecond.$key.'.'.substr($value,-3,3);
if($get_file)
{
$fp = @fopen($rndFileName,'w');
@fwrite($fp,$get_file);
@fclose($fp);
}
$body = ereg_replace($value,$fileurl,$body);
}
//$body = addslashes($body);
echo $body;
}
else
{
//echo 'no post';
}

?>

关键文件二、 注意,还要你的页面中包含了Jquery。
KE.plugin['remoteimg'] = {
click : function(id) {
$.post(KE.scriptPath+'php/remoteimg.php',{str: KE.html(id)},function(data) {
 alert('获取图片完成!');
 KE.html(id, '');
 KE.util.insertHtml(id, data);
},'JSON');
}
};

我要评论

昵称:
验证码:

最新评论

共0条 共0页 10条/页 首页 上一页 下一页 尾页
意见反馈