auto-resize photos that are really toooo big, instead of dropping them
authorPierre Habouzit (MadCoder <pierre.habouzit@m4x.org>
Fri, 7 Jan 2005 07:51:16 +0000 (07:51 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 26 Jun 2008 21:27:12 +0000 (23:27 +0200)
git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-224

include/validations/photos.inc.php
include/xorg.varstream.inc.php

index e1b894b..3d0a91e 100644 (file)
@@ -50,28 +50,44 @@ class PhotoReq extends Validate
             return ($this = null);
         }
         list($this->x, $this->y, $this->mimetype) = $image_infos;
-        // récupération du type de l'image
+
         switch ($this->mimetype) {
-            case 1:
-                $this->mimetype = "gif";
-                break;
-                
-            case 2:
-                $this->mimetype = "jpeg";
-                break;
-                
-            case 3:
-                $this->mimetype = "png";
-                break;
-                
+            case 1: $this->mimetype = "gif";    break;
+            case 2: $this->mimetype = "jpeg";   break;
+            case 3: $this->mimetype = "png";    break;
             default:
                 $page->trig("Type d'image invalide");
                 return ($this = null);
         }
 
         if (strlen($_data) > SIZE_MAX)  {
-            $page->trig("Image trop grande (max 30ko)");
-            return ($this = null);
+            $img = imagecreatefromstring($_data);
+            if (!$img) {
+                $page->trig("image trop grande et impossible à retailler automatiquement");
+                return ($this = null);
+            }
+
+            $nx = $x = imagesx($img);
+            $ny = $y = imagesy($img);
+
+            if ($nx > 240) { $ny = intval($ny*240/$nx); $nx = 240; }
+            if ($ny > 300) { $ny = intval($nx*300/$nx); $ny = 300; }
+            if ($nx < 160) { $ny = intval($ny*160/$nx); $nx = 160; }
+
+            $comp = '90';
+            $file = tempnam('/tmp', 'photo');
+
+            while (strlen($_data) > SIZE_MAX) {
+                $img2  = imagecreatetruecolor($nx, $ny);
+                imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $x, $y);
+                imagejpeg($img2, $file, $comp);
+                $_data = file_get_contents($file);
+                $this->mimetype = 'jpeg';
+
+                $comp --;
+            }
+
+            unlink($file);
         }
         $this->data = $_data;
     }
index ef0909d..435082d 100644 (file)
@@ -34,18 +34,25 @@ class VarStream
 
     function stream_open($path, $mode, $options, &$opened_path)
     {
-        $url = parse_url($path);
-        $this->varname = $url['host'];
-        if(!isset($GLOBALS[$this->varname]))
+        $url            = parse_url($path);
+        $this->varname  = $url['host'];
+        $this->position = 0;
+        if (!isset($GLOBALS[$this->varname]))
         {
             trigger_error('Global variable '.$this->varname.' does not exist', E_USER_WARNING);
             return false;
         }
-        $this->position = 0;
         return true;
     }
 
     // }}}
+    // {{{ stream_close
+
+    function stream_close()
+    {
+    }
+    
+    // }}}
     // {{{ stream_read
 
     function stream_read($count)
@@ -56,6 +63,20 @@ class VarStream
     }
 
     // }}}
+    // {{{ stream_write
+
+    function stream_write($data)
+    {
+        $len = strlen($data);
+        if ($len > $this->position + strlen($GLOBALS[$this->varname])) {
+            str_pad($GLOBALS[$this->varname], $len);
+        }
+
+        $GLOBALS[$this->varname] = substr_replace($GLOBALS[$this->varname], $data, $this->position, $len);
+        $this->position += $len;
+    }
+    
+    // }}}
     // {{{ stream_eof
 
     function stream_eof()
@@ -64,6 +85,47 @@ class VarStream
     }
 
     // }}}
+    // {{{ stream_tell
+
+    function stream_tell()
+    {
+        return $this->position;
+    }
+
+    // }}}
+    // {{{ stream_seek
+
+    function stream_seek($offs, $whence)
+    {
+        switch ($whence) {
+            case SEEK_SET:
+                $final = $offs;
+                break;
+
+            case SEEK_CUR:
+                $final += $offs;
+                break;
+
+            case SEEK_END:
+                $final = strlen($GLOBALS[$this->varname]) + $offs;
+                break;
+        }
+
+        if ($final < 0) {
+            return -1;
+        }
+        $this->position = $final;
+        return 0;
+    }
+
+    // }}}
+    // {{{ stream_flush
+
+    function stream_flush()
+    {
+    }
+    
+    // }}}
 }
 
 // }}}