edusoho企培版 外部资讯抓取 有时无法获取图片问题/无法获取微信公众号图片

 

有的网站图片无法被外站引用,这时候最好的是图片本地化。那就动动手改一版

修改src/AppBundle/Controller/Admin/ArticleController.php 代码

修改createFromUrlAction方法。添加两个新方法saveImage,processImages


头部添加引用 use Topxia\Service\Common\ServiceKernel;

public function createFromUrlAction(Request $request)
    {
        //如果携带url参数,尝试识别url对应页面的资讯
        $url = $request->query->get('url');
        $isSafeDoman = $this->isSafeDoman($url);
        $article = ['title' => '', 'body' => ''];
        if ($url) {
            $url = urldecode($url);
            $readability = new Readability(new Configuration());
            $html = file_get_contents($url);
            try {
                $readability->parse($html);
                $article['title'] = $readability->getTitle();
                $article['body'] = $this->processImages($readability->getContent());
                $article['sourceUrl'] = $url;
                $article['thumb'] = $this->saveImage($readability->getImage());
                $article['originalThumb'] = $this->saveImage($readability->getImage());
            } catch (ParseException $e) {
                // echo sprintf('Error processing text: %s', $e->getMessage());
            }

        }

        if ('POST' == $request->getMethod()) {
            $formData = $request->request->all();

            $article['tags'] = array_filter(explode(',', $formData['tags']));

            $article = $this->getArticleService()->createArticle($formData);

            $attachment = $request->request->get('attachment');
            $this->getUploadFileService()->createUseFiles($attachment['fileIds'], $article['id'], $attachment['targetType'], $attachment['type']);

            return $this->redirect($this->generateUrl('admin_article'));
        }

        $categoryTree = $this->getCategoryService()->getCategoryTree();

        return $this->render('admin/article/article-from-url-modal.html.twig', [
            'url' => $url,
            'article' => $article,
            'categoryTree' => $categoryTree,
            'category' => ['id' => 0, 'parentId' => 0],
            'isSafeDoman' => $isSafeDoman,
        ]);
    }

    public function saveImage($image){
        $smallPath = ServiceKernel::instance()->getParameter('topxia.upload.public_directory').'/tmp/';

        $imageContent =  file_get_contents($image);
        $name = time().rand(0,time()).".png";
        file_put_contents($smallPath.$name,$imageContent);
        return "tmp/".$name;
    }

    public function processImages($content){
        $matched = preg_match_all('/<img (.*?) src="(.*?)"/', $content, $matches);
        $images = $matches[2];
        foreach ($images as $image){
            $content = str_replace($image, "/files/".$this->saveImage($image), $content);
        }
        $matched = preg_match_all('/<img (.*?) data-src="(.*?)"/', $content, $matches2);
        foreach ($matches2[2] as $image){
            $content = str_replace($image, "/files/".$this->saveImage($image), $content);
        }
        $content = str_replace('data-src', "src", $content);
        
        return $content;
    }

这样就搞定了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值