Exemple #1
0
 protected function upload_file($tmp, $name)
 {
     //mydie($this->_media_dir,$this->_media);
     $ext = @strtolower(end(explode('.', $name)));
     $user = Bootstrap::$main->user;
     if (isset($this->data['flowIdentifier'])) {
         $lp = $this->data['flowIdentifier'];
     } else {
         $lp = 1 + $this->image()->getUsersCount($user['id']);
     }
     $name = $this->_prefix . '/' . $user['md5hash'] . '/' . md5($lp . '-' . $name) . '.' . $ext;
     $chunks = false;
     $original_name = $name;
     if (isset($this->data['flowTotalChunks']) && $this->data['flowTotalChunks'] > 1 && isset($this->data['flowChunkNumber'])) {
         $chunks = true;
         $name .= '.part' . $this->data['flowChunkNumber'];
     }
     if ($this->_appengine) {
         $file = 'gs://' . CloudStorageTools::getDefaultGoogleStorageBucketName() . '/' . $name;
         move_uploaded_file($tmp, $file);
     } else {
         $file = $this->_media_dir . '/' . $name;
         @mkdir(dirname($file), 0755, true);
         move_uploaded_file($tmp, $file);
         //mydie(exif_read_data($tmp));
     }
     if ($chunks) {
         if (!$this->checkChunks($file)) {
             return false;
         }
         $name = $original_name;
         $file = preg_replace('/\\.part[0-9]+$/', '', $file);
     }
     if (!file_exists($file) || !filesize($file)) {
         $this->error(18);
     }
     $model = new imageModel();
     $model->user = $user['id'];
     $model->src = $name;
     $model->d_uploaded = Bootstrap::$main->now;
     $exif = [];
     $imagesize = @getimagesize($file, $exif);
     if (!is_array($imagesize) || !$imagesize[0]) {
         $imagesize = [5000, 5000];
     }
     if (is_array($exif)) {
         foreach ($exif as $k => $a) {
             if (substr($a, 0, 4) == 'Exif') {
                 $matches = [];
                 preg_match_all('/[0-9]{4}:[0-9]{2}:[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $a, $matches);
                 $d = '';
                 if (isset($matches[0][1])) {
                     $d = $matches[0][1];
                 } elseif (isset($matches[0][0])) {
                     $d = $matches[0][0];
                 }
                 if ($d) {
                     $d = preg_replace('/([0-9]{4}):([0-9]{2}):([0-9]{2})/', '\\1-\\2-\\3', $d);
                     $model->d_taken = $this->strtotime($d);
                 }
             }
         }
     }
     if ($this->_appengine) {
         $model->url = CloudStorageTools::getImageServingUrl($file, ['size' => 0 + Bootstrap::$main->getConfig('image_size'), 'secure_url' => Bootstrap::$main->getConfig('protocol') == 'https']);
         $full = CloudStorageTools::getImageServingUrl($file, ['size' => 1234, 'secure_url' => Bootstrap::$main->getConfig('protocol') == 'https']);
         $model->full = str_replace('=s1234', '=s' . Bootstrap::$main->getConfig('full_size'), $full);
         $model->thumbnail = CloudStorageTools::getImageServingUrl($file, ['size' => 0 + Bootstrap::$main->getConfig('thumbnail_size'), 'secure_url' => Bootstrap::$main->getConfig('protocol') == 'https']);
         $model->square = CloudStorageTools::getImageServingUrl($file, ['size' => 0 + Bootstrap::$main->getConfig('square_size'), 'secure_url' => Bootstrap::$main->getConfig('protocol') == 'https', 'crop' => true]);
     } else {
         $image = new Image($file);
         $w = $h = 0;
         if ($imagesize[0] > Bootstrap::$main->getConfig('image_size')) {
             $w = Bootstrap::$main->getConfig('image_size');
             $img = preg_replace("/\\.{$ext}\$/", '-i.' . $ext, $file);
             $image->min($img, $w, $h, true);
             $model->url = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . preg_replace("/\\.{$ext}\$/", '-i.' . $ext, $name);
         } else {
             $model->url = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . $name;
         }
         $w = $h = 0;
         if ($imagesize[0] > Bootstrap::$main->getConfig('full_size')) {
             $w = Bootstrap::$main->getConfig('full_size');
             $img = preg_replace("/\\.{$ext}\$/", '-f.' . $ext, $file);
             $image->min($img, $w, $h, true);
             $model->full = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . preg_replace("/\\.{$ext}\$/", '-f.' . $ext, $name);
         } else {
             $model->full = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . $name;
         }
         $w = $h = 0;
         if ($image->w() > $image->h()) {
             $w = Bootstrap::$main->getConfig('thumbnail_size');
         } else {
             $h = Bootstrap::$main->getConfig('thumbnail_size');
         }
         $thmb = preg_replace("/\\.{$ext}\$/", '-t.' . $ext, $file);
         $image->min($thmb, $w, $h, true);
         $model->thumbnail = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . preg_replace("/\\.{$ext}\$/", '-t.' . $ext, $name);
         $w = $h = Bootstrap::$main->getConfig('square_size');
         $square = preg_replace("/\\.{$ext}\$/", '-s.' . $ext, $file);
         $image->min($square, $w, $h, false, true);
         $model->square = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . preg_replace("/\\.{$ext}\$/", '-s.' . $ext, $name);
     }
     $model->save();
     $ret = $model->data();
     if ($ctx = Bootstrap::$main->session('image_ctx')) {
         $model->setLabels($ctx);
         $ret['labels'] = $model->getLabels();
         if (is_array($ctx)) {
             foreach ($ctx as $k => $e) {
                 if ($k == 'event') {
                     $event = new eventModel($e);
                     if ($event->user == Bootstrap::$main->user['id'] && !$event->img) {
                         $event->img = $model->id;
                         $event->save();
                     }
                     $model->title = $event->name;
                     $model->save();
                 }
             }
         }
     }
     return $this->status($ret);
 }