Simple Image Collection

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.