发布文章的时候Kindeditor自动保存远程图片(php)
发布时间:2023-03-13 14:17:09 所属栏目:教程 来源:
导读:发布文章时希望把远程的图片保存到本地,这样的问题相信很多人都遇到过,思考了下并做了初步的尝试,方法还不够完美,但已经实现功能,优化那是以后的事情了。
说说星际尘埃网站上的解决方案。
大体思路是,提
说说星际尘埃网站上的解决方案。
大体思路是,提
发布文章时希望把远程的图片保存到本地,这样的问题相信很多人都遇到过,思考了下并做了初步的尝试,方法还不够完美,但已经实现功能,优化那是以后的事情了。 说说星际尘埃网站上的解决方案。 大体思路是,提交文章前,获取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 = "/".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,"//")) { 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; } (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐