在我的应用程序中,如果用户不添加图像,用户创建文章并向其添加图像,该应用程序必须在谷歌图像中搜索它。我正在谷歌上搜索很长时间但仍无法找到实现这一目标所需的工具。
修改 我尝试了Mimos方法,但现在出了问题,现在我得到了:
NotReadableException in AbstractDecoder.php line 302:
Image source not readable
当我尝试从网址保存图片时
ArticlesController商店方法:
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);
}
}
答案 0 :(得分:1)
<?php
$query = 'Foobar';
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=".urlencode($query));
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string as json
$output = json_deocde(curl_exec($ch));
// close curl resource to free up system resources
curl_close($ch);
但我不建议让程序决定它应该采取什么样的形象。您可能会遇到版权问题。最好设置默认图片。