之前做微信登录开发时候,发现微信头像图片没有后缀名,传统的图片抓取方式不奏效,需要特殊的抓取处理。所以,后来将各种情况结合起来,封装成一个类,分享出来。
创建项目
作为演示,我们在www根目录创建项目grabimg,创建一个类GrabImage.php和一个index.php。
编写类代码
我们定义一个和文件名相同的类:GrabImage
属性
接下来定义几个需要使用的属性。
1、首先定义一个需要抓取的图片地址:$img_url
2、再定义一个$file_name用来存储文件的名称,但是不携带拓展名,因为可能涉及到拓展名更换,所以这里拆开定义
3、紧接着就是拓展名$extension
4、然后我们定义一个$file_dir,该属性的作用是,远程图片抓取到本地后所存储的目录,一般相对于PHP入口文件所在的位置作为起始。但是该路径一般不保存到数据库。
5、最后我们定义一个$save_dir,顾名思义,该路径是用来直接保存的数据库的目录。这里说明下,我们不直接存储文件保存路径到数据库,一般是为了之后如果系统迁移,方便更换路径做准备。我们这里的$save_dir一般为日期 + 文件名,如果需要使用时候取出,在前方拼上所需要的路径。
方法
属性弄完了,接下来我们正式开始抓取工作。
首先我们定义一个对外开放的方法getInstances用来获取一些数据,比如抓取图片地址,和本地保存路径。同时将其放入属性中。
1
2
3
4
5
6
|
public function getInstances( $img_url , $base_dir )
{
$this ->img_url = $img_url ;
$this ->save_dir = date ( "Ym" ). '/' . date ( "d" ). '/' ;
$this ->file_dir = $base_dir . '/' . $this ->save_dir. '/' ;
}
|
图片保存路径拼接完成,下面我们要注意一个问题,目录是否存在。日期在一天天走,但是目录并不会自动创建。所以,在保存图片之前,首先需要检查一下,如果当前目录不存在我们需要即时创建。
我们创建设置目录方法setDir。属性我们设置了private,安全
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
private function setDir()
{
if (! file_exists ( $this ->file_dir))
{
mkdir ( $this ->file_dir,0777,TRUE);
}
$this ->file_name = uniqid().rand(10000,99999);
return true;
}
|
接下来就是抓取核心代码
第一步,解决一个问题,我们需要抓取的图片可能没有后缀名。按照传统的抓取方法,先抓取图片,然后截取后缀名的方案不可行。
我们必须通过其它方法来获得图片类型。办法就是从文件流信息中获取文件头信息,从而判断文件mime信息,就可以知道文件后缀名。
为了方便,先定义一个mime和文件拓展名映射。
1
2
3
4
5
6
7
|
$mimes = array (
'image/bmp' => 'bmp' ,
'image/gif' => 'gif' ,
'image/jpeg' => 'jpg' ,
'image/png' => 'png' ,
'image/x-icon' => 'ico'
);
|
这样,当我获取了类型是image/gif的时候,我就可以知道是.gif图片了。
利用php函数get_headers,获取文件流头信息。当其值不为false时候我们将其赋值给变量$headers
取出Content-Type的值即为mime的值。
1
2
3
4
|
if (( $headers =get_headers( $this ->img_url, 1))!==false){
$type = $headers [ 'Content-Type' ];
}
|
使用上面我们定义的映射表,我们可以很轻松的获取后缀名。
1
|
$this ->extension= $mimes [ $type ];
|
当然上面获取的$type,可能不存在我们的映射表中,说明这种类型文件并不是我们想要的,直接抛弃就好了,不用管它。
下面的步骤就和传统抓取文件一样。
1
2
3
4
5
6
7
8
|
$file_path = $this ->file_dir. $this ->file_name. "." . $this ->extension;
$contents = file_get_contents ( $this ->img_url);
if ( file_put_contents ( $file_path , $contents ))
{
return $this ->save_dir. $this ->file_name. "." . $this ->extension;
}
|
首先获取本地保图片存完整路径$file_path,接下来使用file_get_contents抓取数据,然后使用file_put_contents保存到刚刚的文件路径。
最后我们返回一个可以直接保存到数据库中的路径,而不是文件存储路径。
该抓取方法完整版是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
private function getRemoteImg()
{
$mimes = array (
'image/bmp' => 'bmp' ,
'image/gif' => 'gif' ,
'image/jpeg' => 'jpg' ,
'image/png' => 'png' ,
'image/x-icon' => 'ico'
);
if (( $headers =get_headers( $this ->img_url, 1))!==false)
{
$type = $headers [ 'Content-Type' ];
if (isset( $mimes [ $type ]))
{
$this ->extension= $mimes [ $type ];
$file_path = $this ->file_dir. $this ->file_name. "." . $this ->extension;
$contents = file_get_contents ( $this ->img_url);
if ( file_put_contents ( $file_path , $contents ))
{
return $this ->save_dir. $this ->file_name. "." . $this ->extension;
}
}
}
return false;
}
|
最后,为了简单,我们想在其他地方只要调用其中一个方法就可以完成抓取。所以,我们将抓取动作直接放入到getInstances中,在配置完路径后,直接抓取,所以,在初始化配置方法getInstances里新增代码。
1
2
3
4
5
6
7
8
|
if ( $this ->setDir())
{
return $this ->getRemoteImg();
}
else
{
return false;
}
|
测试
我们去刚刚创建的index.php文件内试试。
1
2
3
4
5
6
7
|
<?php
require_once 'GrabImage.php' ;
$object = new GrabImage();
$img_url = "http://www.bidianer.com/img/icon_mugs.jpg" ; // 需要抓取的远程图片
$base_dir = "./uploads/image" ;
echo $object ->getInstances( $img_url , $base_dir );
?>
|
惹,的确抓取过来了
完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php
class GrabImage{
private $img_url ;
private $file_name ;
private $extension ;
private $file_dir ;
private $save_dir ;
public function getInstances( $img_url , $base_dir )
{
$this ->img_url = $img_url ;
$this ->save_dir = date ( "Ym" ). '/' . date ( "d" ). '/' ;
$this ->file_dir = $base_dir . '/' . $this ->save_dir. '/' ;
return $this ->start();
}
private function start()
{
if ( $this ->setDir())
{
return $this ->getRemoteImg();
}
else
{
return false;
}
}
private function setDir()
{
if (! file_exists ( $this ->file_dir))
{
mkdir ( $this ->file_dir,0777,TRUE);
}
$this ->file_name = uniqid().rand(10000,99999);
return true;
}
private function getRemoteImg()
{
$mimes = array (
'image/bmp' => 'bmp' ,
'image/gif' => 'gif' ,
'image/jpeg' => 'jpg' ,
'image/png' => 'png' ,
'image/x-icon' => 'ico'
);
if (( $headers =get_headers( $this ->img_url, 1))!==false)
{
$type = $headers [ 'Content-Type' ];
if (isset( $mimes [ $type ]))
{
$this ->extension= $mimes [ $type ];
$file_path = $this ->file_dir. $this ->file_name. "." . $this ->extension;
$contents = file_get_contents ( $this ->img_url);
if ( file_put_contents ( $file_path , $contents ))
{
return $this ->save_dir. $this ->file_name. "." . $this ->extension;
}
}
}
return false;
}
}
|
![](https://img2018.cnblogs.com/common/1859876/201911/1859876-20191108150008170-1378238020.png)
明确的学习思路能更高效的学习
![](https://img2018.cnblogs.com/common/1859876/201911/1859876-20191108150206752-581043463.jpg)
点此加入该群学习