Home > Backend Development > PHP Tutorial > Code to obtain images from web pages and save them locally using PHP_PHP Tutorial

Code to obtain images from web pages and save them locally using PHP_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:42:00
Original
1091 people have browsed it

Copy code The code is as follows:

header("Content-type:image/jpeg");
function read_url($str)
{
$file=fopen($str,"r");
while(!feof($file))
{
$result. =fgets($file,9999);
}
fclose($file);
return $result;

}

function save_img($str)
{
$result=read_url($str);
$result=str_replace(""","",$result);
$result=str_replace("'","",$result );

preg_match_all('/|>)/i',$result,$matches);

foreach($matches[1] as $value)
{
echo $value."
n";
//GrabImage($value,$filename="" );
}
}

// $url is the complete URL address of the remote image and cannot be empty
// $filename is an optional variable: if it is empty, the local file. The name will be automatically generated based on time and date
//.

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

$path="download/"; //Specify the storage folder

//If the file does not exist, create it;
if(!file_exists($path)){
mkdir($path);
}

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

ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);

$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);

return $filename;
}
save_img("http://www.jb51.net");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321021.htmlTechArticleCopy the code as follows: ?php header("Content-type:image/jpeg"); function read_url($ str) { $file=fopen($str,"r"); while(!feof($file)) { $result.=fgets($file,9999); } fclose($file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template