- if ($in = fopen (' Php://input ', "RB"))
- while ($buff = Fread ($in, 4096))
- Fwrite (' e:\\1.jpg ', $buff);
Copy CodeTo get the picture information, only getimagesize ($filename), and then hit the file just closed. PHP has a getimagesizefromstring, but requires >=php5.4. Is there a way to manipulate the data flow directly? The answer is yes, there is a "supported Protocol and encapsulation Protocol" in the official PHP manual, and the data://should be familiar to everyone. It can be used to manipulate images (watermarks, thumbnails, and the like) directly in the data stream. The following code is directly file_get_contents () for convenience.
- $file _path = ' http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif ';
- $stream = file_get_contents ($file _path);
- Print_r (getimagesize ("Data://text/plain;base64,". Base64_encode ($stream)));
- $new _img = Imagecreatefromgif ("Data://text/plain;base64,". Base64_encode ($stream));//or $new_img = Imagecreatefromstring ($stream);
- Print_r ($new _img);
- Imagejpeg ($new _img, ' E:\WEB\uploads\test.jpg ', 100);
Copy CodeThe format was converted successfully. That's a good idea, but a base64 doesn't seem so cool at once. Consider a simpler approach, stream_register_wrapper-registering a URL wrapper protocol implemented with PHP classes. Code:
Class getimgstream{
- Private $imgStream;
- Private $position;
- function Stream_open ($path, $mode, $options, & $opened _path) {
- $url = Parse_url ($path);
- $this->imgstream = $GLOBALS [$url ["host]];
- $this->position = 0;
- return true;
- }
function Stream_read ($count) {
- $ret = substr ($this->imgstream, $this->position, $count);
- $this->position + = strlen ($ret);
- return $ret;
- }
- function Stream_stat () {
- MAXMEMORY:5 * 1024 * 1024;
- $fp = fopen ("php://temp/maxmemory:5242880", ' r+ ');
- Fwrite ($fp, $this->imgstream);
- $fstat = Fstat ($FP);
- Fclose ($FP);
- return $fstat;
- }
function stream_eof () {
- return $this->position >= strlen ($this->imgstream);
- }
function Stream_tell () {
- return $this->position;
- }
- function Stream_close () {
- Unset ($this->imgstream, $this->position);
- }
- }
$file _path = ' http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif ';
- $stream = file_get_contents ($file _path);
Stream_wrapper_register ("image", "Getimgstream");
Print_r (getimagesize (' Image://stream '));
- $new _img = imagecreatefromgif (' Image://stream ');//or $new_img = Imagecreatefromstring ($stream);
- Print_r ($new _img);
- Imagejpeg ($new _img, ' E:\WEB\uploads\test.jpg ', 100);
Copy CodeNo surprises, because this function supports PHP 4 >= 4.3.0, PHP 5. Tested, the same local picture (300x800), method one average 43ms, method two average 39ms Correction: If the getimagesize stream does not the support seeking, you may want to add the seek operation to the wrapper by adding the code:
- function Stream_seek ($offset, $whence) {
- $l = strlen ($this->imgstream);
- $p = & $this->position;
- Switch ($whence) {
- Case Seek_set: $newPos = $offset;
- Break
- Case Seek_cur: $newPos = $p + $offset;
- Break
- Case Seek_end: $newPos = $l + $offset;
- Break
- Default:return false;
- }
- $ret = ($newPos >= 0 && $newPos <= $l);
- if ($ret)
- $p = $newPos;
- return $ret;
- }
Copy Code |