ecshop 远程图片本地化

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
$smarty->assign('siteDomain','http://'.$_SERVER['HTTP_HOST'].'/');

/**
 * 得到远程图片
*
* @access  public
* @param   goods_desc      $goods_desc         要处理的内容
* @return  mix         如果成功返回缩略图的路径,失败则返回false
*/

$str = '<p>
    <img src="http://dealer0.autoimg.cn/dl/10982/newsimg/130364690780019046.jpg"/>
    <img src="http://photocdn.sohu.com/20151028/mp38321387_1446018553478_10.jpeg"/>
</p>
<p style="word-wrap: break-word; white-space: normal; word-spacing: 0px; text-transform: none; word-break: normal; color: rgb(34,34,34); outline-width: medium; padding-bottom: 17px; padding-top: 0px; font: 16px/30px tahoma, arial, 'Microsoft YaHei'; outline-style: none; padding-left: 0px; widows: 1; margin: 0px; letter-spacing: normal; outline-color: invert; padding-right: 0px; background-color: rgb(245,248,253); text-indent: 0px; -webkit-text-stroke-width: 0px">
    ddddddddddddddddddddddddddddddddd
</p>';


$test =  GetCurContent($str);

function GetCurContent($goods_desc)
{
    $body = stripslashes($goods_desc);
    $img_array = array();
    //$body = ereg_replace("HTTP://".$_SERVER['SERVER_NAME']."/","",$body);//将源文件进行替换
    preg_match_all("/src=[\"|'|\s]{0,}(http:\/\/([^>]*)\.(gif|jpg|png))/isU",$body,$img_array);
    $img_array = array_unique($img_array[1]);
    
    set_time_limit(0);
    $imgurl = IMAGE_DIR."/bdimages/upload1/".date('Ymd');
    $imgpath = ROOT_PATH.$imgurl;
    
    $millisecond = date("YmdHms");
    if (!file_exists($imgpath))
    {
        if (!make_dir($imgpath))
        {
            return false;
        }
    }
    else
    {
        
        foreach($img_array as $key =>$value)
        {
            $value = trim($value);
            $get_file = @file_get_contents($value);
            $rndfilename = $imgpath."/".$millisecond.$key.".".substr($value,-3,3);
            $fileurl = "http://".$_SERVER['SERVER_NAME']."/".$imgurl."/".$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);
            }
            //echo $rndfilename."<br/>".$fileurl."<br/>";
            $body = str_replace($value,$fileurl,$body);;
            echo $body;
        }
    }
    $body = addslashes($body);
    return $body;
}

  

posted @ 2016-05-16 14:11  Abner3721  阅读(241)  评论(0编辑  收藏  举报