Drupal automatically retrieves external images and saves the function-PHP source code

Source: Internet
Author: User
Tags drupal
Sometimes, when publishing content, We need to extract external images and store them on our servers. This can avoid affecting our website if the external images cannot be opened, now let's share the functions that Drupal implements this function. Sometimes, when publishing content, We need to extract external images and store them on our servers. This can avoid affecting our website if the external images cannot be opened, now let's share the functions that Drupal implements this function.

Script ec (2); script

In Drupal, an external link image is automatically obtained, and then extracted and uploaded to your website for function sharing.

Function get_external_image ($ url ){
$ External_image = file_get_contents ($ url );
$ Parsed_url = parse_url ($ url );
$ Name_dest = rand (1000,9999). "_". basename ($ parsed_url ["path"]);
$ File = file_save_data ($ external_image, 'public: // '. $ name_dest, FILE_EXISTS_REPLACE );
If (is_object ($ file) & file_exists ($ file-> uri )){
$ File-> status = 1;
$ File = file_save ($ file );
Drupal_write_record ('file _ usage', $ file );
Return (array) $ file;
}
Return null;
}

Get_external_image ("http://www.drupalla.com/logo.gif ");

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.