tpshop二次开发(淘宝客)商品主图为外部链接解决办法

我们在用tpshop二次开发淘宝客的时候,可以将淘宝商品主图直接作为本站商品的主图(表tp_goods字段original_img的值),出现的问题我就不展示了。直接来解决办法。
一、在application/function.php中添加函数


/**
 * add:file_exists的扩展,主要是判断外部文件
 * fgets length设为50,为了尽量减少响应时间
 * 如果网络问题可能会引起延迟,适合在后台使用
 */
function file_exists_ex($file)
{
    if(preg_match('/^(http|https):\/\//',$file)){
        //远程文件
        if(ini_get('allow_url_fopen')){
            if(@fopen($file,'r')) return true;
        }
        else{
            $parseurl=parse_url($file);
            $host=$parseurl['host'];
            $path=$parseurl['path'];
            $fp=fsockopen($host,80, $errno, $errstr, 10);
            if(!$fp)return false;
            fputs($fp,"GET {$path} HTTP/1.1 \r\nhost:{$host}\r\n\r\n");
            if(preg_match('/HTTP\/1.1 200/',fgets($fp,1024))) return true;
        }
        return false;
    }
    return file_exists($file);
}

二、在application/common.php文件中找到

$original_img = '.' . $original_img; // 相对路径
if (!is_file($original_img)) {
    return '/public/images/icon_goods_thumb_empty_300.png';
}

改成

if(preg_match('/^(http|https):\/\//',$original_img)){
    if(!file_exists_ex($original_img)){
        return '/public/images/icon_goods_thumb_empty_300.png';
    }
} else {
    $original_img = '.' . $original_img; // 相对路径
    if (!is_file($original_img)) {
        return '/public/images/icon_goods_thumb_empty_300.png';
    }
}

后来又作了些改进,在显示外网图片时不生成本地文件,降低了本地服务的消耗。函数商品相册缩略图get_sub_images($sub_img, $goods_id, $width, $height)也要作同样修改。

三、因为我在调试时并没有生成缩略图,所以在浏览商品页面时会出现“文件不存在”的问题。问题在\vendor\topthink\think-image\src\Image.php的open($file)函数中把以下代码注释掉,系统会自动生成缩略图。

//if (!$file->isFile()) {
//    throw new ImageException('image file not exist');
//}

在开发过程中发现页面展示还是以生成的本地网站文件,对于低配的服务器压力颇大,亟待改进!

自己开发的网站,关键词海宁皮革城

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乡村

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值