Provides various official and user-released code examples. For code reference, you are welcome to learn about the basic image storage class. For GIF images, you can perform static processing.
/**
* Author
* @ Param $ url: the url for capturing an image
* @ Param $ filepath: path for saving the image
* @ Param $ prefix of the name saved in the pre image. You can set it to an increment or a random number to reduce the chance of duplicate names */
Class getImage {
Public function getImage ($ url, $ filepath, $ pre ){
If ($ url = ""){
Return false;
}
$ Arr = explode ('.', $ url );
$ Ext = end ($ arr );
$ Uniq = uniqid (); // you can specify a unique id.
/* Static GIF images for thumbnails */
If ($ ext = "gif") {// determines whether the image is a gif. If yes, perform static processing.
$ Gif_flag = 1; // set the image to 1 as gif
$ New_ext = 'jpg ';
If (! Is_file ($ filepath) mkdir ($ filepath, 0777); // create a folder
$ Name = $ filepath. '/'. $ pre. $ uniq; // file storage path name (no suffix)
$ Name_jpg = $ name. '.'. $ new_ext; // save path name of jpg
$ Name_gif = $name.'.gif '; // save path name of gif
$ Img = file_get_contents ($ url); // capture content
$ This-> mkFile ($ name_jpg, $ img); // create a jpg file
$ This-> mkFile ($ name_gif, $ img); // create a gif File
Return array ($ name_gif, $ name_jpg, $ gif_flag); // return image path + name + flag
} Else {// Save the local image directly if it is not a GIF Image
$ Gif_flag = 0; // set the gif flag to 0.
If (! Is_file ($ filepath) mkdir ($ filepath, 0777 );
$ Name = $ filepath. '/'. $ pre. $ uniq. $ ext; // name and path of Image Storage
$ Img = file_get_contents ($ url); // capture content
$ This-> mkFile ($ name, $ img );
Return array ($ name, '', $ gif_flag); // return image path + name
}
}
Private function mkFile ($ name, $ content ){
If (! Is_file ($ name) mkdir ($ name, 0777); // create a file
File_put_contents ($ name, $ content) // fill the file
}
}
?>
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB