typo
[Pman.Core] / DataObjects / Images.php
index 03d9713..d866e48 100644 (file)
@@ -38,6 +38,12 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
                 $tn.filename LIKE '%{$this->escape($q['search']['filename'])}%' OR $tn.title LIKE '%{$this->escape($q['search']['filename'])}%'
             ");
         }
+
+        if(!empty($q['_to_base64']) && !empty($q['image_id'])) {
+            $i = DB_DataObject::factory("Images");
+            $i->get($q['image_id']);
+            $roo->jok($i->toBase64());
+        }
         
 
     }
@@ -53,7 +59,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         
         $o = $this->object();
         //print_r($o);
-        if (method_exists($o, 'checkPerm')) {
+        if ($o &&  method_exists($o, 'checkPerm')) {
             // edit permissions on related object needed...
             return $o->checkPerm( $lvl == 'S' ? 'S' : 'E' , $au);
             
@@ -195,15 +201,79 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
      * @return - target file name
      */
     function getStoreName() 
+    {
+        return self::staticGetStoreName($this);
+          
+    }
+
+    static function staticGetStoreName($o)
     {
         $opts = HTML_FlexyFramework::get()->Pman;
-        $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
+        $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $o->filename);
         return implode( '/', array(
-            $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
+            $opts['storedir'], '_images_', date('Y/m', strtotime($o->created)), $o->id . '-'. $fn
         ));
-          
     }
-     
+    
+    /**
+     * does the files exist?
+     */
+    function exists()
+    {
+        return self::staticExists($this);
+    }
+
+    static function staticExists($o)
+    {
+        clearstatcache();
+        $ret =  file_exists(self::staticGetStoreName($o));
+        if (!$ret) {
+            return self::staticCanFix($o);
+        }
+        return $ret;
+    }
+
+    /**
+     * the getStorename code got changed, and some old files may not end up with the correct name anymore.
+     * this tries to fix it.
+     *
+     */
+    function canFix() 
+    {
+        return self::staticCanFix($this);
+    }
+
+    static function staticCanFix($o)
+    {
+        // look for the image in the folder, with matching id.
+        // this is problematic..
+        $fn = self::staticGetStoreName($o);
+        if (file_exists($fn . '-really-missing')) {
+            return false;
+        }
+        if (!file_exists(dirname($fn))) {
+            return false;
+        }
+        foreach( scandir(dirname($fn)) as $n) {
+            if (empty($n) || $n[0] == '.') {
+                continue;
+            }
+            $bits = explode('-', $n);
+            if ($bits[0] != $o->id) {
+                continue;
+            }
+            if (preg_match('/\.[0-9]+x[0-9]]+\.jpeg$/', $n)) {
+                continue;
+            }
+            copy(dirname($fn). '/'.  $n, $fn);
+            clearstatcache();
+            return true;
+        }
+        // fixme - flag it as bad
+        touch($fn . '-really-missing');
+    }
+    
+    
     /**
      * deletes all the image instances of it...
      * 
@@ -212,14 +282,20 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     function beforeDelete($dependants_array, $roo)
     {
         
+        if (!empty($dependants_array)) {
+            return;
+        }
+        
         $opts = HTML_FlexyFramework::get()->Pman;
         $deldir = $opts['storedir']. '/_deleted_images_';
+        clearstatcache();
         if (!file_exists( $deldir )) {
-            mkdir($deldir, 0755);
+            @mkdir($deldir, 0755); // not sure why we are erroring here.. after checking - maybe permissions?
         }
             
         $fn = $this->getStoreName();
         $b = basename($fn);
+        clearstatcache();
         if (file_exists($fn)) {
             
             if (file_exists($deldir . '/'. $b)) {
@@ -238,12 +314,14 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
             $dh = opendir($d);
             while (false !== ($fn = readdir($dh))) {
                 if (substr($fn, 0, strlen($b)) == $b) {
-                    
+                    clearstatcache();
                     if (file_exists($deldir . '/'. $fn)) {
                         unlink($d. '/'. $fn);
                         continue;
                     }
-                    rename($d. '/'. $fn, $deldir .'/'. $fn);
+                    if (file_exists($d. '/'. $fn)) {
+                        rename($d. '/'. $fn, $deldir .'/'. $fn);
+                    }
                     
                 }
             }
@@ -304,12 +382,22 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     
     // direct via roo...
     /// ctrl not used??
-    function onUpload($roo)
+    function onUpload($roo, $table = false, $file = false)
     {
+        
+        if ($table !== false) {
+            $this->ontable = $table->tableName();
+            $this->onid = $table->pid();
+        }
+        
+        if ($file === false) {
+            $file = isset($_FILES['imageUpload']) ? $_FILES['imageUpload'] : array();
+        }
+        
         //print_r($_FILES); echo $_FILES['imageUpload']['type'];exit;
-        if (empty($_FILES['imageUpload']['tmp_name']) || 
-            empty($_FILES['imageUpload']['name']) || 
-            empty($_FILES['imageUpload']['type'])
+        if (empty($file['tmp_name']) || 
+            empty($file['name']) || 
+            empty($file['type'])
         ) {
             
             $emap = array( 
@@ -320,7 +408,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
                 4=>"No file was uploaded",
                 6=>"Missing a temporary folder" 
             ); 
-            $estr = (empty($_FILES['imageUpload']['error']) ? '?': $emap[$_FILES['imageUpload']['error']]);
+            $estr = (empty($file['error']) ? '?': $emap[$file['error']]);
             $this->err = "Missing file details : Error=". $estr;
             return false;
         }
@@ -356,7 +444,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         
         require_once 'File/MimeType.php';
         $y = new File_MimeType();
-        $this->mimetype = $_FILES['imageUpload']['type'];
+        $this->mimetype = $file['type'];
         if (in_array($this->mimetype, array(
                         'text/application',
                         'application/octet-stream',
@@ -366,7 +454,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
                         'application/vnd.ms-excel',   /// sometimes windows reports csv as excel???
                         'application/csv-tab-delimited-table', // windows again!!?
                 ))) { // weird tyeps..
-            $inf = pathinfo($_FILES['imageUpload']['name']);
+            $inf = pathinfo($file['name']);
             $this->mimetype  = $y->fromExt($inf['extension']);
         }
         
@@ -374,11 +462,11 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         $ext = $y->toExt(trim((string) $this->mimetype ));
         
         $this->filename = empty($this->filename) ? 
-            $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext); 
+            $file['name'] : ($this->filename .'.'. $ext); 
         
         
         
-        if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
+        if (!$this->createFrom($file['tmp_name'])) {
             $this->err  =  isset($this->err)  ?  $this->err  : "createFrom Image failed";
             return false;
         }
@@ -446,14 +534,14 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     }
     
      
-    function toRooArray($req) {
+    function toRooArray($req)
+    {
         
         $ret= $this->toArray();
       
-        static $ff = false;
-        if (!$ff) {
-            $ff = HTML_FlexyFramework::get();
-        }
+         
+        $ff = HTML_FlexyFramework::get();
+        
         
         $ret['public_baseURL'] = isset($ff->Pman_Images['public_baseURL']) ?
                     $ff->Pman_Images['public_baseURL'] : $ff->baseURL;
@@ -470,10 +558,11 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
             if (!empty($req['query']['imagesize'])) {
                 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
             }
+            
+            
         }
+        $ret['shorten_name']   = $this->shorten_name();
         
-         
-         
         return $ret;
     }
     
@@ -484,13 +573,17 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
      * 
      * 
      */
-    function URL($size , $provider = '/Images/Thumb', $baseURL=false)
+    function URL($size , $provider = '/Images/Thumb', $baseURL=false, $to_type=false)
     {
         if (!$this->id) {
             return 'about:blank';
-            
         }
-
+        if (!$this->exists()) {
+            return 'about:missing';
+        }
+        
+        $shorten_name = $this->shorten_name();
+        
         $ff = HTML_FlexyFramework::get();
         $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
         if (preg_match('#^http[s]*://#', $provider)) {
@@ -500,36 +593,88 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         if ($size < 0) {
             $provider = preg_replace('#/Thumb$#', '', $provider);
             
-            return $baseURL . $provider . "/{$this->id}/{$this->filename}";
+            return $baseURL . $provider . "/{$this->id}/{$shorten_name}"; // -- this breaks the rss feed #image-{$this->id}";
         }
         //-- max?
         //$size = max(100, (int) $size);
         //$size = min(1024, (int) $size);
         // the size should 200x150 to convert
         $sizear = preg_split('/(x|c)/', $size);
-        if(empty($sizear[1])){
-            $sizear[1] = 0;
+        if(!isset($sizear[1])){
+            $sizear[1] =   0; // 0x with '0' is a box? why
         }
+        
         $size = implode(strpos($size,'c') > -1 ? 'c' : 'x', $sizear);
 //        print_r($size);
         $fc = $this->toFileConvert();
 //        print_r($size);
 //        exit;
-        $mt = $this->mimetype;
+        $mt = $to_type === false ? $this->mimetype : $to_type;
         if (!preg_match('#^image/#i',$mt)) {
             $mt = 'image/jpeg';
         }
         
-        $fc->convert($mt, $size);
+        $cn = $fc->convert($mt, $size);
+        $shorten_name = $this->shorten_name(basename($cn));
         
-        return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
+        return $baseURL . $provider . "/$size/{$this->id}/{$shorten_name}"; // -- this breaks the rss feed #image-{$this->id}";
+    }
+    /**
+     *
+     * tries to get an image from then URL - not always has based... - also from the normal url
+     */
+    function getFromHashURL($url)
+    {
+        $id = false;
+        if (preg_match('/#image-([0-9]+)$/', $url, $matches)) {
+            $id = $matches[1];
+        } else if (preg_match('#Images/Thumb/[^/]+/([0-9]+)/#', $url, $matches)) {
+            $id = $matches[1];
+        } else if (preg_match('#Images/([0-9]+)/#', $url, $matches)) {
+            $id = $matches[1];
+        } else if (preg_match('#images[^/]+/([0-9]+)/#i', $url, $matches)) {
+            // supports images.xxxxx.com/{number}/name...
+            $id = $matches[1];
+        } else if (preg_match('#Thumb/[^/]+/([0-9]+)/#', $url, $matches)) {
+            $id = $matches[1];
+        }
+        if ($id === false ||  $id < 1) {
+            return false;
+        }
+        
+        $img = DB_DAtaObject::Factory('images');
+        if ($img->get($id)) {
+            return $img;
+        }
+        return false;
+    }
+    
+    
+    function shorten_name($fn = false)
+    {
+        if(empty($this->filename)) {
+            return;
+        }
+        $fn = $fn === false ? $this->filename : $fn;
+        
+        $filename = explode('.', $fn);
+        $ext = array_pop($filename);
+        $name = preg_replace("/[^A-Z0-9.]+/i", '-', implode('-', $filename)) ;
+        
+        if(strlen($name) > 32) {
+            $name = substr($name, 0, 32);
+        }
+        
+        $shorten_name = "{$name}.{$ext}";
+        
+        return $shorten_name;
     }
     /**
      * size could be 123x345
      * 
      * 
      */
-    function toHTML($size, $provider = '/Images/Thumb') 
+    function toHTML($size, $provider = '/Images/Thumb', $extra = ''
     {
         
         
@@ -544,12 +689,12 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         }
         if (empty($sz[1])) {
             $ratio =  empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
-            $sy = $ratio * $sx;
+            $sy = intval($ratio * $sx);
         } else {
             $sy = $sz[1];
         }
         // create it?
-        $extra = '';
+       
         if (strlen($this->title)) {
             $extra = ' title="'. htmlspecialchars($this->title) . '"';
         }
@@ -588,6 +733,8 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
      */
     function toFileConvert()
     {
+        $fn = $this->getStoreName();
+        
         require_once 'File/Convert.php';
         $fc = new File_Convert($this->getStoreName(), $this->mimetype);
         return $fc;
@@ -640,11 +787,12 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
             $roo->addEvent("ADD", $this, $this->toEventString());
             
             $r = DB_DataObject::factory($this->tableName());
+            
             $r->id = $this->id;
             $roo->loadMap($r);
             $r->limit(1);
             $r->find(true);
-            $roo->jok($r->toArray());
+            $roo->jok($r->toRooArray($ar));
             
             
         }
@@ -659,8 +807,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser))  {
             $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
         }
-        
-        
+         
         
         if (!isset($_FILES['imageUpload'])) {
             return; // standard update...
@@ -680,7 +827,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         $roo->loadMap($r);
         $r->limit(1);
         $r->find(true);
-        $roo->jok($r->toArray());
+        $roo->jok($r->toRooArray($ar));
          
     }
     
@@ -771,13 +918,31 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     function createFromData($data)
     {   
         
-        $this->mimetype= strtolower($this->mimetype);
+        if (0 === strpos($data, "data:")) {
+            // data:image/png;base64, 
+            $data = substr($data,5);
+            $bits = explode(";", $data);
+            $this->mimetype = $bits[0];
+        }
+        static $imgid = 1;
+        if (empty($this->filename)) {
+            require_once 'File/MimeType.php';
+            $y = new File_MimeType();
+            $this->filename = 'image-'.$imgid++.'.'.$y->toExt($this->mimetype);
+        }
+        
+        
+        $this->mimetype = strtolower($this->mimetype);
+        if ($this->mimetype == 'image/jpg') {
+            $this->mimetype = 'image/jpeg';
+        }
+        
         
         $explode_mimetype = explode('/', $this->mimetype);
         
         if (array_shift($explode_mimetype) == 'image') { 
         
-            $imgs = @getimagesize($data);
+            $imgs = @getimagesize('data://'. $data);
             
             if (!empty($imgs) && !empty($imgs[0]) && !empty($imgs[1])) {
                 list($this->width , $this->height)  = $imgs;
@@ -801,7 +966,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         }
         
         file_put_contents($f, file_get_contents("data://" . $data));
-        
+        //var_dump($f);exit;
         $o = clone($this);
         
         $this->filesize = filesize($f);
@@ -823,7 +988,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         }
         
         $file = $this->getStoreName();
-        
+
         if(!file_exists($file)){
             return false;
         }
@@ -831,43 +996,11 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         $data = file_get_contents($file);
         
         if(!empty($scaleWidth) || !empty($scaleHeight)){
-            
-            $width = $this->width;
-            $height = $this->height;
-            
-            if(!empty($scaleWidth)){
-                $width = $scaleWidth;
-
-                if(empty($scaleHeight)){
-                    $height = $this->height * $scaleWidth / $this->width;
-                }
-            }
-            
-            
-
-            if(!empty($scaleHeight)){
-                $height = $scaleHeight;
-
-                if(empty($scaleWidth)){
-                    $width = $this->width * $scaleHeight / $this->height;
-                }
-            }
-            
-            $im = imagecreatefromstring($data);
-            
-            if (($scaled = imagescale($im, $width, $height)) != false) {
-                ob_start();
-                imagejpeg($scaled);
-                $data = ob_get_contents();
-                ob_end_clean();
-                imagedestroy($im);
-                imagedestroy($scaled);
-            }
-            
+            $data = $this->scale(false, $scaleWidth, $scaleHeight);
         }
         
         if($rotate){
-            $data = $this->rotate();
+            $data = $this->rotate($data);
         }
         
         $base64 = 'data:' . $this->mimetype . ';base64,' . base64_encode($data);
@@ -905,12 +1038,17 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         return $page;
     }
     
-    function rotate()
+    function rotate($imageBlob = false)
     {
-        $imagick = new Imagick($this->getStoreName());
+        if(empty($imageBlob)){
+            $imagick = new Imagick($this->getStoreName());
+        } else {
+            $imagick = new Imagick();
+            $imagick->readImageBlob($imageBlob);
+        }
         
         $orientation = $imagick->getImageOrientation(); 
-
+        
         switch($orientation) { 
             case Imagick::ORIENTATION_BOTTOMRIGHT: 
                 $imagick->rotateimage(new ImagickPixel('#00000000'), 180); // rotate 180 degrees 
@@ -928,4 +1066,19 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         return $imagick->getImageBlob();
     }
     
+    function scale($imageBlob = false, $width = 0, $height = 0)
+    {
+        if(empty($imageBlob)){
+            $imagick = new Imagick($this->getStoreName());
+        } else {
+            $imagick = new Imagick();
+            $imagick->readImageBlob($imageBlob);
+        }
+        
+        $imagick->resizeimage($width, $height, Imagick::FILTER_LANCZOS, true, true);
+        
+        return $imagick->getImageBlob();
+        
+    }
+    
  }