added cURL as first choice
Foaly* [Fri, 20 Sep 2013 21:54:25 +0000 (22:54 +0100)]
lib/class.imagecompressor_utils.php

index d34d2f2..c88d886 100644 (file)
@@ -6,7 +6,7 @@
 #
 #-------------------------------------------------------------------------
 #
-# ImageCompressir is a CMS Made Simple module that can compress user-uploaded images.
+# ImageCompressor is a CMS Made Simple module that can compress user-uploaded images.
 #
 #-------------------------------------------------------------------------
 #
@@ -29,7 +29,7 @@
 class imagecompressor_utils {
 
        const SMUSHIT_API_URL = 'http://www.smushit.com/ysmush.it/ws.php?img=%s';
-       const SMUSHIT_UA = 'CMSms Image Compressor/0.9 (http://dev.cmsmadesimple.org/projects/imagecompressor)';
+       const SMUSHIT_UA = 'CMS made simple Image Compressor/0.10.2';
 
        private function __construct() {}
 
@@ -43,10 +43,9 @@ class imagecompressor_utils {
                return round($size, 1).$units[$i];
        }
 
-       static final public function smushit_post($file_url)
+       static final protected function smushit_fopen($url)
        {
-               $uri = sprintf(self::SMUSHIT_API_URL, urlencode($file_url));
-               $f = @fopen($uri, 'r');
+               $f = @fopen($url, 'r');
                if($f === false)
                {
                        return false;
@@ -72,6 +71,31 @@ class imagecompressor_utils {
                return $response;
        }
 
+       static final protected function smushit_curl($url)
+       {
+               $smushit = curl_init();
+               curl_setopt($smushit, CURLOPT_CONNECTTIMEOUT, 10);
+               curl_setopt($smushit, CURLOPT_RETURNTRANSFER, true);
+               curl_setopt($smushit, CURLOPT_URL, $url);
+               curl_setopt($smushit, CURLOPT_USERAGENT, self::SMUSHIT_UA);
+               $request = curl_exec($smushit);
+               curl_close($smushit);
+               return json_decode($request);
+       }
+
+       static final public function smushit_post($file_url)
+       {
+               $url = sprintf(self::SMUSHIT_API_URL, urlencode($file_url));
+               if(function_exists('curl_init'))
+               {
+                       return self::smushit_curl($url);
+               }
+               else
+               {
+                       return self::smushit_fopen($url);
+               }
+       }
+
        static final public function is_image($path)
        {
                $info = @exif_imagetype($path);