Ejemplo n.º 1
0
 /**
  * Save image by AJAX, but do not save data into database.
  * If the image is fetched by upload, try to receive image by Upload class,
  * if it comes from media, try to copy the image from media to feature path.
  * Finally the image data will be saved into session.
  * 
  */
 public function saveImageAction()
 {
     Pi::service('log')->mute();
     $module = $this->getModule();
     $return = array('status' => false);
     $mediaId = $this->params('media_id', 0);
     $id = $this->params('id', 0);
     $fakeId = $this->params('fake_id', 0);
     // Checking is id valid
     if (empty($id) && empty($fakeId)) {
         $return['message'] = __('Invalid ID!');
         echo json_encode($return);
         exit;
     }
     $rename = $id ?: $fakeId;
     $extensions = array_filter(explode(',', $this->config('image_extension')));
     foreach ($extensions as &$ext) {
         $ext = strtolower(trim($ext));
     }
     // Get distination path
     $destination = Media::getTargetDir('feature', $module, true);
     $rowMedia = $this->getModel('media')->find($mediaId);
     // Checking is media exists
     if (!$rowMedia->id or !$rowMedia->url) {
         $return['message'] = __('Media is not exists!');
         echo json_encode($return);
         exit;
     }
     // Checking is media an image
     if (!in_array(strtolower($rowMedia->type), $extensions)) {
         $return['message'] = __('Invalid file extension!');
         echo json_encode($return);
         exit;
     }
     $ext = strtolower(pathinfo($rowMedia->url, PATHINFO_EXTENSION));
     $rename .= '.' . $ext;
     $fileName = rtrim($destination, '/') . '/' . $rename;
     if (!copy(Pi::path($rowMedia->url), Pi::path($fileName))) {
         $return['message'] = __('Can not create image file!');
         echo json_encode($return);
         exit;
     }
     // Scale image
     $uploadInfo['tmp_name'] = $fileName;
     $uploadInfo['w'] = $this->config('feature_width');
     $uploadInfo['h'] = $this->config('feature_height');
     $uploadInfo['thumb_w'] = $this->config('feature_thumb_width');
     $uploadInfo['thumb_h'] = $this->config('feature_thumb_height');
     Media::saveImage($uploadInfo);
     // Save image to draft
     $rowDraft = $this->getModel('draft')->find($id);
     if ($rowDraft) {
         $rowDraft->image = $fileName;
         $rowDraft->save();
     } else {
         // Or save info to session
         $session = Media::getUploadSession($module, 'feature');
         $session->{$fakeId} = $uploadInfo;
     }
     $imageSize = getimagesize(Pi::path($fileName));
     $originalName = isset($rawInfo['name']) ? $rawInfo['name'] : $rename;
     // Prepare return data
     $return['data'] = array('originalName' => $originalName, 'size' => filesize(Pi::path($fileName)), 'w' => $imageSize['0'], 'h' => $imageSize['1'], 'preview_url' => Pi::url(Media::getThumbFromOriginal($fileName)));
     $return['status'] = true;
     echo json_encode($return);
     exit;
 }
Ejemplo n.º 2
0
 /**
  * Saving image by AJAX, but do not save data into database.
  * If the image is fetched by upload, try to receive image by Upload class,
  * if the image is from media, try to copy it from media to author path.
  * Finally the image data will be saved into session.
  * 
  */
 public function saveImageAction()
 {
     Pi::service('log')->mute();
     $return = array('status' => false);
     $id = $this->params('id', 0);
     if (empty($id)) {
         $id = $this->params('fake_id', 0);
     }
     if (empty($id)) {
         $return['message'] = _a('Invalid ID!');
         echo json_encode($return);
         exit;
     }
     $uploadFakeId = $this->params('upload_id', 0);
     if (empty($uploadFakeId)) {
         $return['message'] = _a('Invalid image fake ID!');
         echo json_encode($return);
         exit;
     }
     $module = $this->getModule();
     $session = Media::getUploadSession($module, 'author');
     $image = $session->{$uploadFakeId};
     if (empty($image['tmp_name']) or !file_exists(Pi::path($image['tmp_name']))) {
         $return['message'] = _a('Image is not exists!');
         echo json_encode($return);
         exit;
     }
     $sourceName = $image['tmp_name'];
     $ext = strtolower(pathinfo($sourceName, PATHINFO_EXTENSION));
     $fileName = dirname($sourceName) . '/' . $id . '.' . $ext;
     $width = $this->params('w', 0);
     $height = $this->params('h', 0);
     $x = $this->params('x', 0);
     $y = $this->params('y', 0);
     if (empty($width) or empty($height)) {
         $return['message'] = _a('Image width or height is needed');
         echo json_encode($return);
         exit;
     }
     // Crop and resize avatar
     Pi::image()->crop(Pi::path($sourceName), array($x, $y), array($width, $height), Pi::path($fileName));
     Pi::image()->resize(Pi::path($fileName), array($this->config('author_size'), $this->config('author_size')));
     // Scale image
     $uploadInfo = array();
     $uploadInfo['tmp_name'] = $fileName;
     $uploadInfo['w'] = $this->config('author_size');
     $uploadInfo['h'] = $this->config('author_size');
     Media::saveImage($uploadInfo);
     $rowAuthor = $this->getModel('author')->find($id);
     if ($rowAuthor) {
         if ($rowAuthor->photo && $rowAuthor->photo != $fileName) {
             @unlink(Pi::path($rowAuthor->photo));
         }
         $rowAuthor->photo = $fileName;
         $rowAuthor->save();
     } else {
         // Or save info to session
         $session = Media::getUploadSession($module, 'author');
         $session->{$id} = $uploadInfo;
     }
     $imageSize = getimagesize(Pi::path($fileName));
     @unlink(Pi::path($sourceName));
     // Prepare return data
     $return['data'] = array('size' => filesize(Pi::path($fileName)), 'w' => $imageSize['0'], 'h' => $imageSize['1'], 'preview_url' => Pi::url($fileName), 'filename' => $fileName);
     $return['status'] = true;
     echo json_encode($return);
     exit;
 }
Ejemplo n.º 3
0
 /**
  * Processing media uploaded. 
  */
 public function uploadAction()
 {
     Pi::service('log')->mute();
     $module = $this->getModule();
     $config = Pi::config('', $module);
     $return = array('status' => false);
     $id = $this->params('id', 0);
     $fakeId = $this->params('fake_id', 0) ?: uniqid();
     $type = $this->params('type', 'attachment');
     $formName = $this->params('form_name', 'upload');
     // Checking whether ID is empty
     if (empty($id) && empty($fakeId)) {
         $return['message'] = _a('Invalid ID!');
         return json_encode($return);
         exit;
     }
     $width = $this->params('width', 0);
     $height = $this->params('height', 0);
     $rawInfo = $this->request->getFiles($formName);
     $ext = strtolower(pathinfo($rawInfo['name'], PATHINFO_EXTENSION));
     $rename = $id ?: $fakeId . '.' . $ext;
     $allowedExtension = $type == 'image' ? $config['image_extension'] : $config['media_extension'];
     $mediaSize = $type == 'image' ? $config['max_image_size'] : $config['max_media_size'];
     $destination = Media::getTargetDir('media', $module, true, true);
     $uploader = new UploadHandler(array('destination' => Pi::path($destination), 'rename' => $rename));
     $uploader->setExtension($allowedExtension)->setSize($mediaSize);
     // Get raw file name
     $rawName = null;
     if (empty($rawInfo)) {
         $content = $this->request->getContent();
         preg_match('/filename="(.+)"/', $content, $matches);
         $rawName = $matches[1];
     }
     // Checking whether uploaded file is valid
     if (!$uploader->isValid($rawName)) {
         $return['message'] = implode(', ', $uploader->getMessages());
         echo json_encode($return);
         exit;
     }
     $uploader->receive();
     $fileName = $destination . '/' . $rename;
     $rawName = $rawName ?: substr($rawInfo['name'], 0, strrpos($rawInfo['name'], '.'));
     // Resolve allowed image extension
     $imageExt = explode(',', $config['image_extension']);
     foreach ($imageExt as &$value) {
         $value = strtolower(trim($value));
     }
     // Scale image if file is image file
     $uploadInfo['tmp_name'] = $fileName;
     $uploadInfo['raw_name'] = $rawName;
     $imageSize = array();
     if (in_array($ext, $imageExt)) {
         $scaleImageSize = $this->scaleImageSize(Pi::path($fileName), $config);
         $uploadInfo['w'] = $width ?: $scaleImageSize['w'];
         $uploadInfo['h'] = $height ?: $scaleImageSize['h'];
         Media::saveImage($uploadInfo);
         $imageSizeRaw = getimagesize(Pi::path($fileName));
         $imageSize['w'] = $imageSizeRaw[0];
         $imageSize['h'] = $imageSizeRaw[1];
     }
     // Save uploaded media
     if ($id) {
         $row = $this->getModel('media')->find($id);
         if ($row->url && $row->url != $fileName) {
             unlink(Pi::path($row->url));
         }
         $row->url = $fileName;
         $row->type = $ext;
         $row->size = filesize(Pi::path($fileName));
         $row->meta = json_encode($imageSize);
         $row->save();
     } else {
         // Or save info to session
         $session = Media::getUploadSession($module, 'media');
         $session->{$fakeId} = $uploadInfo;
     }
     // Prepare return data
     $return['data'] = array_merge(array('originalName' => $rawInfo['name'], 'size' => $rawInfo['size'], 'preview_url' => Pi::url($fileName), 'basename' => basename($fileName), 'type' => $ext, 'id' => $id ?: $fakeId, 'filename' => $fileName), $imageSize);
     $return['status'] = true;
     echo json_encode($return);
     exit;
 }