jebego Posted June 13, 2010 Share Posted June 13, 2010 I'm writing some script that gets a few thousand images from google. I have all the URL's stored in an array. Here's my code that gets them all and puts them into a "mosaic": function get_image($url) { $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0); $fileContents = curl_exec($ch); curl_close($ch); return imagecreatefromstring($fileContents); } $image_number = 0; for($row = 0; $row < $rows; $row ++){ for($col = 0; $col < $columns; $col ++){ $copy_image = get_image($url_array[$image_number]); if(!$copy_image){ $copy_image = get_image($url_array[$image_number - 1]); } $copy_image_width = imagesx($copy_image); $copy_image_height = imagesy($copy_image); if($copy_image_width > $copy_image_height){ $copy_image_width = $copy_image_height; }else{ $copy_image_height = $copy_image_width; } imagecopyresampled($final_image, $copy_image, $size * $col, $size * $row, 0, 0, $size, $size, $copy_image_width, $copy_image_height); $image_number ++; } } Basically what this code does is goes through my URL array, and referring to the function "get_image()" which uses cURL to get the image from google, then crops/resizes the images to match the others, and pastes it onto my "canvas", $copy_image. Getting a few thousand images takes quite a while, and most of the script's time is used during the "get_image()" function calls. Is there a way I can speed this process up? Link to comment https://forums.phpfreaks.com/topic/204618-is-it-possible-to-make-this-get-image-code-any-faster/ Share on other sites More sharing options...
Mchl Posted June 13, 2010 Share Posted June 13, 2010 Get better connection to Google servers? The thing that's slow is not your code, but connecting to remote servers. Link to comment https://forums.phpfreaks.com/topic/204618-is-it-possible-to-make-this-get-image-code-any-faster/#findComment-1071362 Share on other sites More sharing options...
Daniel0 Posted June 13, 2010 Share Posted June 13, 2010 Use the curl_multi_*() functions to download in parallel. Link to comment https://forums.phpfreaks.com/topic/204618-is-it-possible-to-make-this-get-image-code-any-faster/#findComment-1071387 Share on other sites More sharing options...
jebego Posted June 13, 2010 Author Share Posted June 13, 2010 Thanks for the replys! Looking up curl_multi_* functions now! Link to comment https://forums.phpfreaks.com/topic/204618-is-it-possible-to-make-this-get-image-code-any-faster/#findComment-1071502 Share on other sites More sharing options...
jd307 Posted June 13, 2010 Share Posted June 13, 2010 I actually really like the sound of this, I'd be quite interested to see the end product when it is working. The only advise that I have to offer here is in warning to be careful with obtaining images through Google: 1. Creating automated scripts to access any of Googles services are actually against their Terms of Service, section 5.3. and 2. As you are probably already aware, many images are governed by copyright laws, etc. Not to put a downer on your hard work, but thought I'd mention it. Link to comment https://forums.phpfreaks.com/topic/204618-is-it-possible-to-make-this-get-image-code-any-faster/#findComment-1071505 Share on other sites More sharing options...
jebego Posted June 14, 2010 Author Share Posted June 14, 2010 Well, the project works much faster now Thanks for the warning about google, gonna have to look that up. Link to comment https://forums.phpfreaks.com/topic/204618-is-it-possible-to-make-this-get-image-code-any-faster/#findComment-1071816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.