因为批量上传了一些商品,其中很多图片是引用别人网站的。然后写了一个程序 ,就是把引用地址,下载到本地。然后更新数据库的  goods_desc 字段-----------------
可是还需要生成一张标题图片。-----------------
以下是代码。<?phpdefine('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
//require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');/* 模板赋值 */
$smarty->assign('ur_here', $_LANG['goods_pic2local']);
$smarty->assign('thisfile', 'goods_pic2local.php');
if ($_REQUEST['act'] == 'manage')
{
    /* 显示模板 */
    $smarty->display('goods_pic2local_manage.htm');
}/*------------------------------------------------------ */
//-- 读取库图片 ==> 图片下载 ==> 更新入库
/*------------------------------------------------------ */
if  ($_REQUEST['act'] == 'update'){ set_time_limit(0); ob_start(); //打开输出缓冲区 
ob_end_flush(); 
ob_implicit_flush(1); //立即输出for($i=0;$i<1000;$i++){
echo str_repeat(" ",4096); //确保足够的字符,立即输出,Linux服务器可以去掉这个语句 echo("wait please,checking image and aim to download now<br/><br/>"); /*查询所有商品信息*/
$sql = "select good.goods_id,good.goods_name,good.goods_desc from ".$GLOBALS['ecs']->table('goods')." as good where is_delete=0 order by good.goods_id asc LIMIT 0,10000"; //$res = $GLOBALS['db']->getAll($sql);
$res = $db->query($sql);
//$res = goods_list($_REQUEST['act'] == 'list' ? 0 : 1, ($_REQUEST['act'] == 'list') ? (($code == '') ? 1 : 0) : -1);// /* 模板赋值 */
// $smarty->assign('Goods',$res);
//    /* 显示模板 */
// $smarty->display('goods_pic2local_update.htm'); while ($row = $db->fetchRow($res))
    {
$goods_id = $row['goods_id'];
$goods_name = $row['goods_name'];
$goods_desc = $row['goods_desc'];
$filename = array();
$img_array = array(); echo("<a href='../goods.php?id=$goods_id' target='_blank'>$goods_name</a>");

//正则
$reg = "/<img[^>]*src=\"(http:\/\/(.+)\/(.+)\.(jpg|gif|bmp|png))\"/isU";
//把抠出来的 img 地址存放到 $img_array 变量中
preg_match_all($reg, $goods_desc, $img_array, PREG_PATTERN_ORDER);
if(is_array($img_array)) {
//过滤重复的图片
$img_array = array_unique($img_array[1]); $dealpic = 0;//修改点
//
//取第一张为标题图片
// foreach ($img_array as $img){
//判断是否是自己网站上的 图片
if('angels-dress.com' != get_domain($img)){// 如果这个图片不是自己服务器上的
//读取图片文件
$Gimg = new GetImage();
$Gimg->source = $img;
$Gimg->save_to = '../images/remote/'.$goods_id.'/'; //$filename = $Gimg->download(); //图片下载到本地 默认curl 还可以使用 gd,ob
$filename = $Gimg->download($method = 'gd');
//$filename = pic_save($filename,0,''); //保存到相册 得到图片保存的位置
$goods_desc = str_replace($img, str_replace("../","/",$filename['tmp_name']), $goods_desc);

$dealpic++;
}
}
if($dealpic > 0) {
echo(" √ 已下载并替换图片".$dealpic."张");

//文本路径替换
$goods_desc = addslashes_deep($goods_desc);
$sql= "update ".$GLOBALS['ecs']->table('goods')." set goods_desc='".$goods_desc."' where `goods_id`='".$goods_id."'";
//echo($sql."<hr/>");
//$res1 = $db->query($sql);
$db->query($sql);
}
}
echo(" <br/>");
ob_flush();
flush(); //../goods.php?id=42
} echo ("<script language='javascript'>alert('".$_LANG['goods_pic2local'].$_LANG['success']."');//window.location='goods_pic2local.php?act=manage';</script>");
}
//从url中获得域名
function get_domain($url){
    $pattern = "/[\w-]+\.(com|net|org|gov|cc|biz|info|cn)(\.(cn|hk))*/";
    preg_match($pattern, $url, $matches);
    if(count($matches) > 0) {
        return $matches[0];
    }else{
        $rs = parse_url($url);
        $main_url = $rs["host"];
        if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
            return $main_url;
        }else{
            $arr = explode(".",$main_url);
            $count=count($arr);
            $endArr = array("com","net","org","3322");//com.cn  net.cn 等情况
            if (in_array($arr[$count-2],$endArr)){
                $domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
            }else{
                $domain =  $arr[$count-2].".".$arr[$count-1];
            }
            return $domain;
        }// end if(!strcmp...)
    }// end if(count...)
}// end function
// 从远程把图片载到服务器本地 的 类
class GetImage { var $source;
var $save_to;
var $quality; function download($method = 'curl') {
$info = @GetImageSize($this->source);
$mime = $info['mime']; // What sort of image?
$type = substr(strrchr($mime, '/'), 1);
switch ($type){
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';

// Best Quality: 100
$quality = isSet($this->quality) ? $this->quality : 100;
break; case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png'; // Compression Level: from 0  (no compression) to 9
$quality = isSet($this->quality) ? $this->quality : 0;
break; case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
break; case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
break; case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
break; case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
break; default:
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
} if(isSet($this->set_extension)){
$ext = strrchr($this->source, ".");
$strlen = strlen($ext);
$new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext;
}else{
$new_name = basename($this->source);
}

if(!is_dir($this->save_to)) {@mkdir($this->save_to,0777);}
$save_to = $this->save_to.date("y_m_d_",time()).time().mt_rand(1000,9999).".".$new_image_ext; //输出对象 组成跟$_FILE变量一样 得到后自己和平常图片上传处理一样了
$img_info['name'] = basename($this->source);
$img_info['type'] = $mime;
$img_info['size'] = 1000;
$img_info['tmp_name'] = $save_to;
$img_info['error'] = 0;

if($method == 'curl'){
$save_image = $this->LoadImageCURL($save_to);
}elseif($method == 'gd'){
$img = $image_create_func($this->source); if(isSet($quality)){
   $save_image = $image_save_func($img, $save_to, $quality);
}else{
   $save_image = $image_save_func($img, $save_to);
}
}elseif($method == 'ob'){
$save_image = $this->GrabImage($save_to);
}
return $img_info;
} function LoadImageCURL($save_to){
$ch = curl_init($this->source);
$fp = fopen($save_to, "wb");

// set URL and other appropriate options
$options = array(CURLOPT_FILE => $fp,
 CURLOPT_HEADER => 0,
 CURLOPT_FOLLOWLOCATION => 1,
 CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough) curl_setopt_array($ch, $options);

curl_exec($ch);
curl_close($ch);
fclose($fp);
} function GrabImage($filename="",$url="") {
if($filename=="") {
$ext=strrchr($url,".");
$filename=date("y_m_d_",time()).time().mt_rand(1000,9999).$ext;
}
if($url==""){
$url = $this->source;
}
if($url==""):return false;endif;
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
}
?>//在修改点做以下工作,把默认第一张图片保存到标题文件夹地址:/images/201105/goods_img/37_G_*.jpg例:
http://www.angels-dress.com:9002/images/201105/goods_img/39_G_13055576419325.jpg
http://www.angels-dress.com:9002/images/201105/goods_img/37_G_13055593660002.jpg,然后更新一下数据库的(goods_thumb ,goods_img ,original_img)三个字段,让他生效goods_thumb-----------------------------,goods_img--------------------------,original_img
images/201105/thumb_img/39_thumb_G_*.jpg|images/201105/goods_img/39_G_*.jpg|images/201105/source_img/39_G_*.jpg

解决方案 »

  1.   


       // 是否上传商品缩略图
        if (isset($_FILES['goods_thumb']) && $_FILES['goods_thumb']['tmp_name'] != '' &&
            isset($_FILES['goods_thumb']['tmp_name']) &&$_FILES['goods_thumb']['tmp_name'] != 'none')
        {
            // 上传了,直接使用,原始大小
            $goods_thumb = $image->upload_image($_FILES['goods_thumb']);
            if ($goods_thumb === false)
            {
                sys_msg($image->error_msg(), 1, array(), false);
            }
        }
        else
        {
            // 未上传,如果自动选择生成,且上传了商品图片,生成所略图
            if ($proc_thumb && isset($_POST['auto_thumb']) && !empty($original_img))
            {
                // 如果设置缩略图大小不为0,生成缩略图
                if ($_CFG['thumb_width'] != 0 || $_CFG['thumb_height'] != 0)
                {
                    $goods_thumb = $image->make_thumb('../' . $original_img, $GLOBALS['_CFG']['thumb_width'],  $GLOBALS['_CFG']['thumb_height']);
                    if ($goods_thumb === false)
                    {
                        sys_msg($image->error_msg(), 1, array(), false);
                    }
                }
                else
                {
                    $goods_thumb = $original_img;
                }
            }
        }
    这是需要添加压缩的代码(从ecshop后台添加商品文件中摘取)。拼装进那里