【问题标题】:Laravel iIntervention Image. Image source not readableLaravel iIntervention 图像。图片来源不可读
【发布时间】:2015-09-15 10:13:45
【问题描述】:

在我的应用程序中,用户创建文章并向其添加图像,如果用户不添加图像,该应用程序必须在谷歌图像中搜索它并将该图像添加到用户文章中。但是当我试图从谷歌获取图像时。我得到这个错误:

     AbstractDecoder.php line 302:
Image source not readable

控制器方法:

 public function store(ArticleRequest $request)
    {
        if ($request->hasFile('file')) {

            $file = Input::file('file');
            $imgTitle = $request->title;
            $imagePath = 'uploads/' . $imgTitle . '.jpg';
            $request->image_path = $imagePath;

            Article::create(array('title' => $request->title,
                'body' => $request->body,
                'image_path' => $imagePath));

            Image::make($file)->resize(300, 200)->save($imagePath);
        } else {
//            $file = Input::file('file');
            $imgTitle = $request->title;

            $query = $imgTitle;

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" . urlencode($query));

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

            $output = json_decode(curl_exec($ch));

//            $file = file_get_contents($output);
            curl_close($ch);

            $imagePath = 'uploads/' . $imgTitle . '.jpg';

            $request->image_path = $imagePath;
            Article::create(array('title' => $request->title,
                'body' => $request->body,
                'image_path' => $imagePath));

            Image::make($output)->resize(300, 200)->save($imagePath);

        }

    }

【问题讨论】:

    标签: php image laravel google-api laravel-5.1


    【解决方案1】:
    $output = json_decode(curl_exec($ch));
    

    返回一个带有结果的 json 对象。如果要获取第一张图片的url添加

    $output = json_decode(curl_exec($ch));
    if(!empty($responseData->results))
        $output = $output->responseData->results[0]->url;
    else {
        die('no image found');
    }
    

    【讨论】:

    • 现在我得到未定义的变量:输出
    • $output = json_decode(curl_exec($ch));下添加新行
    • 如果得到正确 ` $output = json_decode(curl_exec($ch)); $output = $output->responseData->results[0]->url;` Bur now Im get: Trying to get property of non-object
    • 给出错误:试图在$output = $output->responseData->results[0]->url;线上获取非对象的属性
    • 你必须检查是否有任何结果。你不能凭空获得信息;)
    【解决方案2】:

    我找到了解决办法

    else{
            $url = 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=1&q=' . $request->title;
            $url = file_get_contents($url);
            $file = json_decode($url);
            $file = $file->responseData->results[0]->url;
            $imgTitle = $request->title;
            $imagePath = 'uploads/' . $imgTitle . '.jpg';
            $request->image_path = $imagePath;
    
            Article::create(array('title' => $request->title,
                'body' => $request->body,
                'image_path' => $imagePath));
    
            Image::make($file)->resize(300, 200)->save($imagePath);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 2019-04-22
      • 1970-01-01
      • 2016-12-19
      • 2018-05-05
      相关资源
      最近更新 更多