0

I'm trying to get the image from Google Books service, in the thumbnail. Reading the address in php via file_get_contents get all the web page of the book while I just want to get the cover image. I know that it can be done via the src tag of html img element but I need the image server side. Is there a way? Thanks

Here the code I've used:

$context = [
    'http' => [
        'method'=>"GET",
        'header' => "Accept:image/png\r\nAccept-Language:it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3\r\nCache-Control:max-age=0\r\nConnection:  keep-alive\r\nUser-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:33.0) Gecko/20100101 Firefox/33.0\r\n"
     ]
];
$context = stream_context_create($context);
$result = @file_get_contents($bookThumbnail, false, $context);

2 Answers2

0

You mean by scraping it? Theres probably legal issues if you do that...

But looking at their site (and only one result), the image is in this part of the html:

<div class="bookcover"><a href="http://books.google.com/books?id=btIQAAAAYAAJ&amp;printsec=frontcover&amp;source=gbs_ge_summary_r&amp;cad=0" ><img src="http://bks0.books.google.com/books?id=btIQAAAAYAAJ&printsec=frontcover&img=1&zoom=1&edge=curl&imgtk=AFLRE70zzwoUCkgdipXt2aghe0i9TUKTDTH4s71IXTEPNlUNi9dN-aiJDCtxor4tL3yqq8KrHzEiHTyysqSFu2vH2QNm3JNCjyjm3f-B_N2VKYZqrcaqHfUXDdOea2f3FBR3nFIUxtk7" alt="Front Cover" title="Front Cover" width=128 border=1 id=summary-frontcover ></a></div>

Use something like http://php.net/manual/en/class.domxpath.php to extract it. Should be quite straight forward. You will probably not want to just use file_get_contents() for the image though, use curl or something.

ashbkr
  • 1
  • 1
0

sounds like you want something like this

$dat = file_get_contents('https://www.googleapis.com/books/v1/volumes/H1w9AwAAQBAJ');

$arr = json_decode($dat,1);

$info = $arr['volumeInfo'];

$imagedata = file_get_contents($info['imageLinks']['thumbnail']);

file_put_contents('/images/thumb.jpg', $imagedata);

// just to see it 
$img = imagecreatefromstring($imagedata);
header('Content-Type: image/jpg');
imagejpeg($img);
imagedestroy($img);
myte
  • 887
  • 7
  • 10
  • That's what I've tried but $imagedate contains the entire web page of the book, not just the image – Andrea Mai Oct 25 '14 at 10:06
  • did you even bother loading https://www.googleapis.com/books/v1/volumes/H1w9AwAAQBAJ directly in the browser? it returns json encoded data, NOT a "web page" – myte Oct 25 '14 at 12:37
  • Not talking about the json. The problem is with ['imageLinks']['thumbnail']: if you open http://bks6.books.google.it/books?id=H1w9AwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&imgtk=AFLRE72u4uHUtXrp5KndKgbDCFooaoAJkag-LgCQDStIsd8HxjaT_Q5vzmloBjY2ZnOIz2lJbr2alZRmzb3kfQXjMaLGnk6Y4Gq2yRct3xiyzoqo2sz3w-M&source=gbs_api in the browser you get the cover but not via file_get_contents – Andrea Mai Oct 25 '14 at 15:22