I want to create a script that can get a thumbnail from an URL. Here’s what I have so far:
[code=php]// Gets the HTML for the URL
$string = FetchPage($link);
if($string){
preg_match_all(‘/<img[^>]*src=[“|’](.*(jpg|jpeg|gif|png))[“|’]/Ui’, $string, $out, PREG_PATTERN_ORDER);
$i=1;
while(1){
// Choose a random image
$num = rand(0,count($out[1]));
$source_image_path = $out[1][$num];
// If the URL is relative
if (stripos($source_image_path,’http’)!=0){
// remove the first slash
if(stripos($source_image_path,’/’)==0)
$source_image_path = substr($source_image_path,1);
// Get directory of the link; creates an absolute URL
$source_image_path = preg_replace(‘//([^/]*)?$/’,”,$link).$source_image_path;
}
if(!url_exists($source_image_path))
continue;
list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );
// I don’t want icons
if($source_image_width>30 && $source_image_height>30)
break;
$i++;
if($i>20){
// Taking too long
$source_image_path = false;
break;
}
}
preg_match(‘/([^/]+.w+)(?.+)?$/’,$source_image_path,$matches);
$file_name = $matches[1];
}
I need $source_image_path and $file_name for later use. How do I prevent this script from taking so long?