发布文章时希望把远程的图片保存到本地,这样的问题相信很多人都遇到过,思考了下并做了初步的尝试,方法还不够完美,但已经实现功能,优化那是以后的事情了。
说说星际尘埃网站上的解决方案。
大体思路是,提交文章前,获取html代码,提取其中的远程图片地址,通过ajax调用php程序对它们进行保存,并返回地址,用新图片地址替换远程地址,提交文章。
用的技术有:javascript的带正则的replace函数,ajax同步保存图片,php的保存远程文件,php返回json数据等
下面给出代码,先是前端代码,后是php代码,配合注释看,希望能理解吸收,还有我用的编辑器是kindeditor,简单又好用的一个编辑器:
前端JS代码:
function checkForm() {//如果勾选了需要保存远程图片则进入保存远程图片函数
if ($("#cboxSaveRomote")[0].checked) {
saveRomoteImgs();
}
KE.util.setData("content1");
return true;
}
function saveRomoteImgs() {
var imgcount = 0;
var content = KE.html("content1");//得到html代码
content = content.replace(/src=['"][^'"]+(.bmp|.jpg|.jpeg|.gif|.png)['"]/ig,function(matchword){
imgcount++;
showInfoWindow("图片"+imgcount+"保存开始");//显示提示框
matchArray = /src=['"]([^'"]+)['"]/i.exec(matchword);
var newimgpath = 'src="'+matchArray[1]+'"';
$.ajax({
async:false,//这个很重要,要同步ajax,设为true的话是不等返回的
url:"upload/save_remote_image.php",
type:"POST",
data:{imgurl:matchArray[1],referbase:$("#txtReferbase").val()},
dataType:"json",
timeout:10000,
cache:false,
success:function(result){
if (result.error) {
showInfoWindow("图片"+imgcount+"保存失败1("+result.message+")");
//newimgpath = 'src="'+matchArray[1]+'"';
} else {
showInfoWindow("图片"+imgcount+"保存成功");
newimgpath = 'src="'+result.url+'"';
}
},
error:function(request,status,error){
showInfoWindow("图片"+imgcount+"保存失败2("+status+";"+error+")");
//newimgpath = 'src="'+matchArray[1]+'"';
}
});
return newimgpath;
});
KE.html("content1",content);//把修改好的html内容再保存
clearInfoWindow();//关闭提示框
}
upload/save_remote_image.php代码:
<?php
require_once 'JSON.php';
session_name("lscj_session");
session_start();
if (!isset($_SESSION['username']))
{
session_destroy();
alert("用户信息丢失,请重新登录!");
}
if (empty($_POST['imgurl'])) {
alert("imgurl为空!");
}
$phpbb_root_path = "../../../";
//图片显示路径
$imgShowPath = "http://www.iseexn.com/attachments/".date('Y').'/'.date('m').'/';
//图片保存路径
$imgStorePath = $phpbb_root_path.'attachments/'.date('Y').'/';
if (!is_dir($imgStorePath)) {
mkdir($imgStorePath,0777);
}
$imgStorePath .= date('m').'/';
if (!is_dir($imgStorePath)) {
mkdir($imgStorePath,0777);
}
$imgurl = $_POST['imgurl'];
$referbase = $_POST['referbase'];
if (0 === stripos($imgurl,"http") || 0 === stripos($imgurl,"//")) {
} else {
if (empty($referbase)) {
alert("referbase为空!");
} else {
$imgurl = $referbase.$imgurl;
}
}
if (false !== stripos($imgurl,"//www.iseexn.com")) {
header('Content-type: text/html; charset=utf-8');
$json = new Services_JSON();
echo $json->encode(array('error' => 0,'url' => $imgurl));
}
$filetype = getFiletype($imgurl);
if (empty($filetype)) {
alert("图片类型为空!");
}
$newimgname = time().'_'.rand(1000,9999).".".$filetype;
$newimgpath = $imgStorePath.$newimgname;
set_time_limit(0);
$get_file = @file_get_contents($imgurl);
if ($get_file) {
$fp = @fopen ( $newimgpath,'w');
@fwrite ( $fp,$get_file);
@fclose ( $fp);
header('Content-type: text/html; charset=utf-8');
$json = new Services_JSON();
echo $json->encode(array('error' => 0,'url' => $imgShowPath.$newimgname));
exit;
} else {
alert("获取图片失败!");
}
function getFiletype($filename) {
$tempArray = explode(".",$filename);//分割字符串
if (count($tempArray)>1) {
$fileType = $tempArray[count($tempArray)-1];//得到文件扩展名
return $fileType;
}
return "";
}
function alert($msg) {
header('Content-type: text/html; charset=utf-8');
$json = new Services_JSON();
echo $json->encode(array('error' => 1,'message' => $msg));
exit;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。