Home Backend Development PHP Tutorial PHP implementation method of grabbing remote images and saving them as_PHP Tutorial

PHP implementation method of grabbing remote images and saving them as_PHP Tutorial

Jul 21, 2016 pm 02:54 PM
php url under picture whole accomplish crawl method yes source code of Related explain Remotely

The following is the source code and its related explanation

<?php
//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.66xing.com/UploadFile/200609082320515027.bmp", "as.gif");
?>

Related description:

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. Stored in an internal buffer)
//
readfile: Read a file and write it to the 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/364552.htmlTechArticleThe following is the source code and its related explanation?php //URL is the remote complete image address and cannot be empty , $filename is the name of the picture saved as //By default, the picture is placed in the same directory as this script...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles