开发者

Crawling custom URL's for images and resizing them via AJAX

开发者 https://www.devze.com 2023-03-27 16:38 出处:网络
I\'m building a web app where users can store links, with 200x200 pictures associated. By default, I\'d like to crawl the link for images and then return thumbnails of the biggest ones (from which the

I'm building a web app where users can store links, with 200x200 pictures associated. By default, I'd like to crawl the link for images and then return thumbnails of the biggest ones (from which the user can select the "official" thumbnail). I want this all to happen via AJAX. My 开发者_StackOverflow中文版question is: what is the best way to do this?

Currently, I'm using the PHP Simple HTTP Parser to scan a URL. I then find the src attribute of all the <img> tags, use getimagesize to store the image size located at that URL, sort the array from biggest to smallest and return the top 5 biggest image URL's via AJAX to the client. Then the client sends a different AJAX request for each one which makes a server-side ImageMagick script download and cut the image to a thumbnail, save it in a temporary folder and then return the URL of this thumbnail, which the client finally loads on his browser.

Needless to say, this is a little complicated and probably really inefficient. Running this process on http://en.wikipedia.org takes about 10-15 seconds from start to finish. I'm not certain there are any more efficient ways, however.


I'd do it in one AJAX request, with the script automatically resizing the biggest 5 images on the first pass, saving them, and returning a JSON array with the resized image URLs for the client.


You should probably use PHP's DOMDocument class to grab/parse the html page.

getimagesize() means you have to download each image and process them. Perhaps you should consider simply showing the user ALL images, by simply placing img tags that link back to the original HTML page. You could size these however you like using the tags. This way you do not have to download/process a single image until the user has actually selected one for the thumbnail.


Interested if / how you solved this?

In the end I looped through the images doing getimagesize() until both height and width were over a certain size, and then broke the loop.

This way its slightly more efficient as it only downloads as many images as it needs

0

精彩评论

暂无评论...
验证码 换一张
取 消