Home  >  Article  >  Backend Development  >  PHP implementation code for grabbing web page images and saving them as_PHP tutorial

PHP implementation code for grabbing web page images and saving them as_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:39:591069browse

The following is the source code and its related explanations

Copy the code The code is as follows:

// URL is the complete remote image address and cannot be empty. $filename is the name of the image saved as
//By default, the image is placed in the same directory as this script
function GrabImage($url, $filename=" "){
//If $url is empty, return false;
if($url == ""){return false;}
$ext = strrchr($url, ".");/ /Get the extension of the image
if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp"){echo "Format not supported!";return false; }
if($filename == ""){$filename = time()."$ext";}//Rename with timestamp
//Start capturing
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;
}
//Test
GrabImage("http:// www.jb51.net/images/logo.gif", "as.gif");
?>

ob_start : Turn on output buffering
This function will turn output buffering on . While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. (Output is stored in an internal buffer)
//
readfile : Read in an File and write to output buffer
Returns the number of bytes read from the file. Returns FALSE on error and displays an error message unless called as @readfile().
//

ob_get_contents : Return the contents of the output buffer (return the contents of the output buffer)
This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn' t active. (Returns FALSE if the output buffer is not active (open))
//
ob_end_clean() : Clean (erase) the output buffer and turn off output buffering (clear the output buffer)
This function discards(discard) the contents of the topmost output buffer and turns off this output buffering.(discard and turn off) If you want to further process the buffer's contents you have to call ob_get_contents() before ob_end_clean() as the buffer contents are discarded when ob_end_clean() is called. The function returns TRUE when it successfully discarded one buffer and FALSE otherwise. Reasons for failure are first that you called the function without an active buffer or that for some reason a buffer could not be deleted (possible for special buffer).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321447.htmlTechArticleThe following is the source code and its related explanation. Copy the code as follows: ?php //URL is the complete remote image Address, cannot be empty, $filename is the name of the picture saved as //By default, the picture is placed...
Statement:
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