PHP Gets the remote picture and saves it to the local

Source: Internet
Author: User
Tags file size file url readfile save file strlen time and date

Example 1

The code is as follows Copy Code

/*
* Function: PHP a variety of ways to achieve the perfect download remote pictures saved to local
* Parameters: File URL, save file name, use the download method
* Use the original name of the remote file when the save file name is empty
*/
function GetImage ($url, $filename = ', $type =0) {
if ($url = = ") {return false;}
if ($filename = = ") {
$ext =strrchr ($url, '. ');
if ($ext!= '. gif ' && $ext!= '. jpg ') {return false;}
$filename =time (). $ext;
}
File Save path
if ($type) {
$ch =curl_init ();
$timeout = 5;
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer,1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$img =curl_exec ($ch);
Curl_close ($ch);
}else{
Ob_start ();
ReadFile ($url);
$img =ob_get_contents ();
Ob_end_clean ();
}
$size =strlen ($IMG);
File size
$FP 2= @fopen ($filename, ' a ');
Fwrite ($fp 2, $img);
Fclose ($fp 2);
return $filename;
}

Example 2

The code is as follows Copy Code

<?php

//
Function: Gets the remote picture and saves it to the local
//
//
Determine if you have permission to write files to the local server
//
//
Variable Description:
$url is the full URL address of the remote picture and cannot be empty.
$filename is an optional variable: If NULL, local file names are based on time and date
Automatically generated.

function Grabimage ($url, $filename = "") {
if ($url = = ""): return false;endif;

if ($filename = = "") {
$ext =STRRCHR ($url, ".");
if ($ext!= ". gif" && $ext!= ". jpg"): return false;endif;
$filename =date ("Dmyhis"). $ext;
}

Ob_start ();
ReadFile ($url);
$img = Ob_get_contents ();
Ob_end_clean ();
$size = strlen ($img);

$FP 2= @fopen ($filename, "a");
Fwrite ($fp 2, $img);
Fclose ($fp 2);

return $filename;
}


$img =grabimage ("Http://www.111cn.net", "");
if ($img): Echo ' <pre></pre> Else:echo "false"; endif;

?>


In the DEDECMS:

The code is as follows Copy Code
if (!empty ($saveremoteimg))
{
$body = Stripslashes ($body);
$img _array = Array ();
Preg_match_all ("/(src| SRC) =["| ' |] {0,} (http://(. *). (gif|jpg|jpeg|bmp|png)) /isu ", $body, $img _array);
$img _array = Array_unique ($img _array[2]);
Set_time_limit (0);
$IMGURL = $img _dir. " /". Strftime ("%y%m%d ", Time ());
$imgPath = $base _dir. $imgUrl;
$milliSecond = strftime ("%h%m%s", Time ());
if (!is_dir ($imgPath)) @mkdir ($imgPath, 0777);
foreach ($img _array as $key => $value)
{
$value = Trim ($value);
$get _file = @file_get_contents ($value);
$rndFileName = $imgPath. " /". $milliSecond. $key.". substr ($value, -3,3);
$fileurl = $imgUrl. " /". $milliSecond. $key.". substr ($value, -3,3);
if ($get _file)
{
$fp = @fopen ($rndFileName, "w");
@fwrite ($fp, $get _file);
@fclose ($FP);
}
$body = Ereg_replace ($value, $fileurl, $body);
}
$body = Addslashes ($body);
}
?>

Example 4

The code is as follows Copy Code

<?php
//
/Function: Get the remote picture and save it to the local
//
//
//Make sure you have permission to write the file to the local server
//
//
//Variable Description:
//$url remote Map The full URL address of the slice, cannot be empty. The
//$filename is an optional variable: If NULL, the local file name is automatically generated based on time and date//.
Function Grabimage ($url, $filename = ') {
if ($url = = '): Return false;endif
if ($filename = =) {
$ext = STRRCHR ($url, '. ');
if ($ext!= '. gif ' && $ext!= '. jpg '): Return false;endif $filename =date (' Dmyhis '). $ext;
}
Ob_start ();
ReadFile ($url);
$img = Ob_get_contents ();
Ob_end_clean ();
$size = strlen ($img); br> $fp 2= @fopen ($filename, ' a ');
Fwrite ($fp 2, $img);
Fclose ($fp 2);
return $filename;
}
$img =grabimage (' yun_qi_img/logo.png ', ');
if ($img) {echo ' <pre></pre> ';} Else{echo ' false ';}
?

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.