]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/imagefile.php
Merge branch '0.9.x' into pluginize-twitter-bridge
[quix0rs-gnu-social.git] / lib / imagefile.php
index db344db8f83d8500c790210df522de5ab8cf80f6..88f46148140b3d9904074e583d5dc74bc7fafc84 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Laconica, the distributed open-source microblogging tool
+ * StatusNet, the distributed open-source microblogging tool
  *
  * Abstraction for an image file
  *
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Image
- * @package   Laconica
- * @author    Evan Prodromou <evan@controlyourself.ca>
- * @author    Zach Copley <zach@controlyourself.ca>
- * @copyright 2008-2009 Control Yourself, Inc.
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @author    Zach Copley <zach@status.net>
+ * @copyright 2008-2009 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link      http://laconi.ca/
+ * @link      http://status.net/
  */
 
-if (!defined('LACONICA')) {
+if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
@@ -38,11 +38,11 @@ if (!defined('LACONICA')) {
  * Makes it slightly easier to accept an image file from upload.
  *
  * @category Image
- * @package  Laconica
- * @author   Evan Prodromou <evan@controlyourself.ca>
- * @author   Zach Copley <zach@controlyourself.ca>
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@status.net>
+ * @author   Zach Copley <zach@status.net>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link     http://laconi.ca/
+ * @link     http://status.net/
  */
 
 class ImageFile
@@ -68,17 +68,18 @@ class ImageFile
     static function fromUpload($param='upload')
     {
         switch ($_FILES[$param]['error']) {
-        case UPLOAD_ERR_OK: // success, jump out
+         case UPLOAD_ERR_OK: // success, jump out
             break;
-        case UPLOAD_ERR_INI_SIZE:
-        case UPLOAD_ERR_FORM_SIZE:
-            throw new Exception(sprintf(_('That file is too big. The maximum file size is %d.'), $this->maxFileSize()));
+         case UPLOAD_ERR_INI_SIZE:
+         case UPLOAD_ERR_FORM_SIZE:
+            throw new Exception(sprintf(_('That file is too big. The maximum file size is %d.'),
+                ImageFile::maxFileSize()));
             return;
-        case UPLOAD_ERR_PARTIAL:
+         case UPLOAD_ERR_PARTIAL:
             @unlink($_FILES[$param]['tmp_name']);
             throw new Exception(_('Partial upload.'));
             return;
-        default:
+         default:
             throw new Exception(_('System error uploading file.'));
             return;
         }
@@ -113,6 +114,23 @@ class ImageFile
             return;
         }
 
+        // Don't crop/scale if it isn't necessary
+        if ($size === $this->width
+            && $size === $this->height
+            && $x === 0
+            && $y === 0
+            && $w === $this->width
+            && $h === $this->height) {
+
+            $outname = Avatar::filename($this->id,
+                                        image_type_to_extension($this->type),
+                                        $size,
+                                        common_timestamp());
+            $outpath = Avatar::path($outname);
+            @copy($this->filepath, $outpath);
+            return $outname;
+        }
+
         switch ($this->type) {
          case IMAGETYPE_GIF:
             $image_src = imagecreatefromgif($this->filepath);
@@ -154,9 +172,9 @@ class ImageFile
         imagecopyresampled($image_dest, $image_src, 0, 0, $x, $y, $size, $size, $w, $h);
 
         $outname = Avatar::filename($this->id,
-                                          image_type_to_extension($this->type),
-                                          $size,
-                                          common_timestamp());
+                                    image_type_to_extension($this->type),
+                                    $size,
+                                    common_timestamp());
 
         $outpath = Avatar::path($outname);
 
@@ -165,7 +183,7 @@ class ImageFile
             imagegif($image_dest, $outpath);
             break;
          case IMAGETYPE_JPEG:
-            imagejpeg($image_dest, $outpath);
+            imagejpeg($image_dest, $outpath, 100);
             break;
          case IMAGETYPE_PNG:
             imagepng($image_dest, $outpath);
@@ -175,6 +193,9 @@ class ImageFile
             return;
         }
 
+        imagedestroy($image_src);
+        imagedestroy($image_dest);
+
         return $outname;
     }
 
@@ -209,12 +230,12 @@ class ImageFile
         $num = substr($str, 0, -1);
 
         switch(strtoupper($unit)){
-            case 'G':
-                $num *= 1024;
-            case 'M':
-                $num *= 1024;
-            case 'K':
-                $num *= 1024;
+         case 'G':
+            $num *= 1024;
+         case 'M':
+            $num *= 1024;
+         case 'K':
+            $num *= 1024;
         }
 
         return $num;