diff --git a/Fastimage.php b/Fastimage.php index 0d3b9a6..f8ca3f8 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; @@ -72,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; } @@ -94,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; @@ -215,7 +228,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; }