-
- if($in = fopen('php://input', "rb"))
- while($buff = fread($in, 4096))
- fwrite('e:\1.jpg' , $buff);
-
Copy the code
To get the image information, the only way is getimagesize($filename) and open the file you just closed again.
There is a getimagesizefromstring in php, but it requires >=php5.4.
Is there a way to directly manipulate the data stream?
The answer is yes, there is "Supported Protocols and Encapsulation Protocols" in the official PHP manual, and the data:// in it should be familiar to everyone.
You can use it to directly manipulate images (watermarks, thumbnails, etc.) in the data stream.
For convenience, the following code is directly file_get_contents().
-
- $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:WEBuploadstest.jpg', 100);
-
Copy code The format was successfully converted.
This method is good, but base64 doesn't look so cool.
You can consider a simpler method, stream_register_wrapper — register a URL wrapper protocol implemented with a PHP class.
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");
- < p> print_r(getimagesize('image://stream'));
- $new_img = imagecreatefromgif('image://stream');//or $new_img = imagecreatefromstring($stream);
- print_r($new_img);
- imagejpeg ($new_img, 'E:WEBuploadstest.jpg', 100);
-
Copy the code
No surprises, because this function supports PHP 4 >= 4.3.0, PHP 5.
After testing, the same local picture (300x800), the average method 1 is 43ms, the average method 2 is 39ms
Correction:
If the stream reported by getimagesize does not support seeking, you may need to add the seek operation to the wrapper and add 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
|