600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > PHP 抓取网页图片并且另存为的实现代码

PHP 抓取网页图片并且另存为的实现代码

时间:2019-02-27 12:14:35

相关推荐

PHP 抓取网页图片并且另存为的实现代码

php教程|php手册

PHP,网页图片,另存为

php教程-php手册

在线点餐 网站 源码,vscode设置双引号,ubuntu 翼讯,tomcat设置网页乱码,印尼爬虫,php 开启xml,珠海seo按天收费,君微网站源码,欧美免费视频网站模板下载 迅雷下载lzw

URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字 默认把图片放在以此脚本相同的目录里

黑锐源码论坛,vscode的项目属性在哪里,ubuntu使用pluma,允许Tomcat长等待,airtest爬虫集群,php薪资待遇,南京seo排名优化公司排名,全屏滚动网站lzw

家政app源码,ubuntu 光标变竖线,国外爬虫店名字,话说php,乌海seo托管lzw

下面是源代码,及其相关解释

代码如下:

<?php

//URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字

//默认把图片放在以此脚本相同的目录里

function GrabImage($url, $filename=””){

//$url 为空则返回 false;

if($url == “”){return false;}

$ext = strrchr($url, “.”);//得到图片的扩展名

if($ext != “.jpg” && $ext != “.jpg” && $ext != “.jpg”){echo “格式不支持!”;return false;}

if($filename == “”){$filename = time().”$ext”;}//以时间戳另起名

//开始捕捉

ob_start();

readfile($url);

$img = ob_get_contents();

ob_end_clean();

$size = strlen($img);

$fp2 = fopen($filename , “a”);

fwrite($fp2, $img);

fclose($fp2);

return $filename;

}

//测试

GrabImage(“/images/logo.jpg”, “as.jpg”);

?>

ob_start : 打开输出缓冲

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. (输出是在内部缓冲储存)

//

readfile : 读入一个文件并写入到输出缓冲

返回从文件中读入的字节数。如果出错返回 FALSE 并且除非是以 @readfile() 形式调用,否则会显示错误信息。

//

ob_get_contents : Return the contents of the output buffer(返回输出缓冲的内容)

This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn’t active. (如果输出缓冲没有活动(打开),则返回 FALSE)

//

ob_end_clean() : Clean (erase) the output buffer and turn off output buffering(清除输出缓冲)

This function discards(丢弃) the contents of the topmost output buffer and turns off this output buffering.(丢弃并且关掉) If you want to further process the buffer’s contents you have to call ob_get_contents() before ob_end_clean() as the buffer contents are discarded when ob_end_clean() is called. (如果要用缓冲内容,则在清理输出缓冲之前要先调用 ob_get_contents())The function returns TRUE when it successfully discarded one buffer and FALSE otherwise. Reasons for failure are first that you called the function without an active buffer or that for some reason a buffer could not be deleted (possible for special buffer).

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。