First, create the project
As a demo, we create a project grabimg in the WWW root, creating a class grabimage.php and a index.php.
Second, write class code
We define a class with the same file name: Grabimage
Third, the property
Next, define several properties that you want to use.
1, first define a need to crawl the picture address:$img_url
2, and then define a $file_name
name to store the file, but do not carry the extension name, because it may involve the extension of name replacement, so here to open the definition
3, followed by the expansion of the name$extension
4, and then we define a $file_dir
, the role of this property is that the remote image crawled to the local store after the directory, generally relative to the PHP entry file location as the starting point. However, the path is typically not saved to the database.
5, finally we define a $save_dir
, as the name suggests, the path is used to directly save the directory of the database. Here, we do not directly store the file save path to the database, generally for later if the system migration, easy to replace the path to prepare. Our general here $save_dir
is the date + filename, if you need to use the time to remove, in front of the need to spell the path.
Iv. methods
property is finished, and then we begin the crawl work.
First we define a way to open the door to getInstances
get some data, such as grabbing the image address, and saving the path locally. Put it in the property at the same time.
Public Function getinstances ($img _url, $base _dir)
{
$this->img_url = $img _url;
$this->save_dir = Date ("Ym"). ' /'. Date ("D"). ' /'; For example: 201610/19/
$this->file_dir = $base _dir. '/' $this->save_dir. '/';//For example:./uploads/image/2016/10/19/
}
Picture save path Stitching complete, below we should pay attention to a question, whether the directory exists. The date is gone by day, but the directory is not created automatically. So before you save the picture, you first need to check that if the current directory does not exist we need to create it immediately.
We create a setup directory method setDir
. Property we set the private, secure
/**
* Check that the picture needs to keep the directory exists
* If not, create a directory now
* @return BOOL
*
/Private Function Setdir ()
{
if (!file_exists ($this->file_dir))
{
mkdir ($this->file_dir,0777,true);
}
$this->file_name = Uniqid (). rand (10000,99999);//filename, this is just demo, please use your own unique file name generation method to return true in the actual project
;
}
The next step is to crawl the core code
The first step, to solve a problem, we need to crawl the picture may not have a suffix name. According to the traditional method of grasping, it is not feasible to grab the image first and then intercept the suffix name.
We must use other methods to get the picture type. The solution is to get the file header information from the file flow information, so as to determine the file MIME information, you can know the file suffix name.
For convenience, first define a mime and file extension mapping.
$mimes =array (
' image/bmp ' => ' bmp ',
' image/gif ' => ' gif ',
' image/jpeg ' => ' jpg ',
' image /png ' => ' png ',
' Image/x-icon ' => ' ico '
);
So, when I get the type image/gif
, I can see that it is. gif pictures.
Use the PHP function get_headers
to get the file flow header information. When the value is not the false
time we assign it to the variable$headers
The value that takes out the Content-type is the MIME value.
if (($headers =get_headers ($this->img_url, 1)) {
//Get the type of response
$type = $headers [' Content-type '];
}
Using the mapping table we defined above, we can easily get the suffix name.
$this->extension= $mimes [$type];
Of course, the above acquisition $type
, may not exist in our mapping table, that this type of file is not what we want, directly discard the good, do not care about it.
The following steps are the same as the traditional crawl files.
$file _path = $this->file_dir. $this->file_name. "." $this->extension;
Obtain data and save
$contents =file_get_contents ($this->img_url);
if (file_put_contents ($file _path, $contents))
{
//The value returned here is directly saved to the database path + filename, in the form of: 201610/19/ 57feefd7e2a7ay5p7lspqai-ly1bf.jpg return
$this->save_dir $this->file_name. "." $this->extension;
}
First get the local guaranteed picture to save the full path $file_path
, then use the file_get_contents
crawl data, and then use the file_put_contents
file path saved to just that.
Finally we return to a path that can be saved directly to the database instead of the file store path.
The full version of the Crawl method is:
Private Function getremoteimg ()
{
//MIME and extension mappings
$mimes =array (
' image/bmp ' => ' bmp ',
' image/ GIF ' => ' gif ',
' image/jpeg ' => ' jpg ',
' image/png ' => ' png ',
' Image/x-icon ' => ' ico '
);
Gets the response head
if (($headers =get_headers ($this->img_url, 1))
{
//Gets the type $type of the response
= $headers [' Content-type '];
If it conforms to the type if we want
(Isset ($mimes [$type]))
{
$this->extension= $mimes [$type];
$file _path = $this->file_dir. $this->file_name. "." $this->extension;
Obtain data and save
$contents =file_get_contents ($this->img_url);
if (file_put_contents ($file _path, $contents))
{
//The value returned here is directly saved to the database path + filename, in the form of: 201610/19/ 57feefd7e2a7ay5p7lspqai-ly1bf.jpg return
$this->save_dir $this->file_name. "." $this
->extension;
}}} return false;
}
Finally, in order to be simple, we would like to call one of the methods in other places to complete the crawl. So, we put the crawl action directly into the, getInstances
after configuring the path, directly crawl, so, in the initialization of the configuration method getinstances new code.
if ($this->setdir ())
{return
$this->getremoteimg ();
}
else
{return
false;
}
Test
Let's try the index.php file we just created.
<?php
require_once ' grabimage.php ';
$object = new Grabimage ();
$img _url = "Http://www.bidianer.com/img/icon_mugs.jpg"; The remote picture to crawl
$base _dir = "./uploads/image";//locally saved path
echo $object->getinstances ($img _url, $base _dir); C16/>?>
Yes, it's crawling over.
Complete code
<?php/** * Crawl remote picture to local, can grab picture without suffix * @author yanying <yanyinghq@163.com> * @link bidianer.com/class Grabi mage{/** * @var string The address of a remote picture that needs to be crawled * For example: http://www.bidianer.com/img/icon_mugs.jpg * Some remote file paths may have no extension name * form: http
://www.xxx.com/img/icon_mugs/q/0 * * Private $img _url; /** * @var String The name of the file to be saved * Crawl to the local filename to regenerate the name * but without the extension name * For example: 57FEEFD7E2A7AY5P7LSPQAI-LY1BF * * Private $file _n
Ame
/** * @var string file extension * Here Direct use of remote image extension * For remote pictures that do not have a name extension, * For example:. jpg */private $extension; /** * @var string file saved in local directory * The path here is the path to the PHP save file * Generally relative to the entry file save path * For example:./uploads/image/201610/19/* But the path is not typically stored directly into the
Database * * Private $file _dir; /** * @var string database saved files directory * This path is directly saved to the database picture path * General Direct Save date + filename, need to use when spell on the previous path * This is to facilitate the migration of the system when the path * For example: 20
1610/19/* Private $save _dir; /** * @param string $img _url The image address you want to crawl * @param string $base _dir a locally saved path, such as:./uploads/image, with the last Slash "/" * @return bool |int * * Public functiOn GetInstances ($img _url, $base _dir) {$this->img_url = $img _url; $this->save_dir = Date ("Ym"). ' /'. Date ("D"). ' /'; For example: 201610/19/$this->file_dir = $base _dir. '/' $this->save_dir. '/';
For example:./uploads/image/2016/10/19/return $this->start ();
/** * Start fetching picture/Private function start () {if ($this->setdir ()) {return $this->getremoteimg ();
else {return false; }/** * Check that the picture needs to be persisted * if it does not exist, create a directory now * @return BOOL */Private Function Setdir () {if (!file_exists $
This->file_dir)) {mkdir ($this->file_dir,0777,true);
$this->file_name = Uniqid (). rand (10000,99999);//file name, this is just a demo, please use your own unique file name generation method to return true in the actual project; /** * Crawl The remote image core method, you can grab both the picture with the suffix name and the picture without the suffix name * * @return Bool|int */Private Function getremoteimg () {//MIME and extension Mappings $mimes =array (' image/bmp ' => ' bmp ', ' image/gif ' => ' gif ', ' image/jpeg ' => ' jpg ', ' image/png ' =&G t; '
PNG ', ' Image/x-icon ' => ' ico '); Gets the response head if (($headers =get_headers ($this->img_url, 1)) {//Gets the type of response $type = $headers [' Content-type '];
If it conforms to the type if we want (Isset ($mimes [$type])) {$this->extension= $mimes [$type]; $file _path = $this->file_dir. $this->file_name. "."
$this->extension;
Obtain data and save $contents =file_get_contents ($this->img_url); if (file_put_contents ($file _path, $contents)) {//The value returned here is directly saved to the database path + filename, in the form of: 201610/19/57FEEFD7E2A7AY5P7LSPQA I-ly1bf.jpg return $this->save_dir $this->file_name. "."
$this->extension;
}} return false; }
}
Summarize
The above is the entire content of this article, I hope to learn from you or use PHP can help, if you have questions you can message exchange.