From 47fe1dd9ee8224f9af11bc0e4aafa7fdcd73cd72 Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Mon, 26 Oct 2015 21:35:53 +0800 Subject: [PATCH 1/5] opetimize the fopen add timeout and user_agent and referer to avoid banned by server --- Fastimage.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Fastimage.php b/Fastimage.php index 0d3b9a6..140c262 100644 --- a/Fastimage.php +++ b/Fastimage.php @@ -28,13 +28,22 @@ public function load($uri) { if ($this->handle) $this->close(); - $this->handle = fopen($uri, 'r'); + $host = parse_url($uri, PHP_URL_HOST); + $stream_context = @stream_context_create(array( + 'http' => array( + 'timeout' => 1, + 'method' => 'GET', + 'user_agent' => (isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', + 'header' => "Referer: http://$host\r\n" + ) + )); + $this->handle = fopen($uri, 'rb', null, $stream_context); } public function close() { - if ($this->handle) + if ($this->handle && is_resource($this->handle)) { fclose($this->handle); $this->handle = null; From 5bd7a7692c15e1c84d73f119b3b7773588a9919f Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Mon, 26 Oct 2015 21:39:18 +0800 Subject: [PATCH 2/5] fix encoding problem when parsing jpeg with php 5.6+ --- Fastimage.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Fastimage.php b/Fastimage.php index 140c262..b5eaf47 100644 --- a/Fastimage.php +++ b/Fastimage.php @@ -224,7 +224,10 @@ private function getChars($n) $result = substr($this->str, $this->strpos, $n); $this->strpos += $n; - return $result; + if (function_exists("mb_convert_encoding")) + return mb_convert_encoding($result, "8BIT", "7BIT"); + else + return $result; } From a274f245a9c7bfff4b3243456a1a187c89e996b3 Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Mon, 26 Oct 2015 21:40:28 +0800 Subject: [PATCH 3/5] add webp support --- Fastimage.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Fastimage.php b/Fastimage.php index b5eaf47..f49bac0 100644 --- a/Fastimage.php +++ b/Fastimage.php @@ -81,6 +81,8 @@ public function getType() return $this->type = 'jpeg'; case chr(0x89).'P': return $this->type = 'png'; + case "RI": + return $this->type = 'webp'; default: return false; } @@ -103,7 +105,9 @@ private function parseSize() case 'bmp': return $this->parseSizeForBMP(); case 'jpeg': - return $this->parseSizeForJPEG(); + return $this->parseSizeForJPEG(); + case 'webp': + return $this->parseSizeForWEBP(); } return null; @@ -195,6 +199,13 @@ private function parseSizeForJPEG() } } + private function parseSizeForWEBP() { + $chars = $this->getChars(30); + $result = unpack("C12/S9", $chars); + + return array($result['8'], $result['9']); + } + private function getChars($n) { From 58cb83ffded75b2fc2ebb39fb04afb3ebfb6c71e Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Mon, 26 Oct 2015 21:41:36 +0800 Subject: [PATCH 4/5] Revert "add webp support" This reverts commit a274f245a9c7bfff4b3243456a1a187c89e996b3. --- Fastimage.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/Fastimage.php b/Fastimage.php index f49bac0..b5eaf47 100644 --- a/Fastimage.php +++ b/Fastimage.php @@ -81,8 +81,6 @@ public function getType() return $this->type = 'jpeg'; case chr(0x89).'P': return $this->type = 'png'; - case "RI": - return $this->type = 'webp'; default: return false; } @@ -105,9 +103,7 @@ private function parseSize() case 'bmp': return $this->parseSizeForBMP(); case 'jpeg': - return $this->parseSizeForJPEG(); - case 'webp': - return $this->parseSizeForWEBP(); + return $this->parseSizeForJPEG(); } return null; @@ -199,13 +195,6 @@ private function parseSizeForJPEG() } } - private function parseSizeForWEBP() { - $chars = $this->getChars(30); - $result = unpack("C12/S9", $chars); - - return array($result['8'], $result['9']); - } - private function getChars($n) { From a2814cc66d397966d5cee31feca79f049ddbb086 Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Mon, 26 Oct 2015 21:42:42 +0800 Subject: [PATCH 5/5] add webp support --- Fastimage.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Fastimage.php b/Fastimage.php index b5eaf47..f8ca3f8 100644 --- a/Fastimage.php +++ b/Fastimage.php @@ -81,6 +81,8 @@ public function getType() return $this->type = 'jpeg'; case chr(0x89).'P': return $this->type = 'png'; + case "RI": + return $this->type = 'webp'; default: return false; } @@ -103,7 +105,9 @@ private function parseSize() case 'bmp': return $this->parseSizeForBMP(); case 'jpeg': - return $this->parseSizeForJPEG(); + return $this->parseSizeForJPEG(); + case 'webp': + return $this->parseSizeForWEBP(); } return null;