Home > Article > Backend Development > An example of PHP image collection function
An example of PHP image collection function
<p><? /** * 采集图片函数 * func: getimg * params: $url 网址 $filepath 图片文件的路径 * by bbs.it-home.org */ function getimg($url, $filepath) { </p> <p> if ($url == '') { return false; } $ext = strrchr($url, '.'); </p> <p> if ($ext != '.gif' && $ext != '.jpg') { return false; } </p> <p> //判断路经是否存在 !is_dir($filepath)?mkdir($filepath):null; </p> <p> //获得随机的图片名,并加上后辍名 $filetime = time(); $filename = date("YmdHis",$filetime).rand(100,999).'.'.substr($url,-3,3); </p> <p> //读取图片 $img = fetch_urlpage_contents($url); //指定打开的文件 $fp = @ fopen($filepath.'/'.$filename, 'a'); //写入图片到指定的文本 fwrite($fp, $img); fclose($fp); return '/'.$filepath.'/'.$filename; } function fetch_urlpage_contents($url){ $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_TIMEOUT, 1000); $file_contents = curl_exec($ch); curl_close($ch); return $file_contents; } echo getimg(") //图片地址 //保存路径 //返回保存后路径 ?></p>
The above is the content of an example of PHP image collection function. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!