예제 #1
0
 /**
  * Processes the uploaded image.
  * @param CUploadedFile $uploadedImg The uploaded image
  * @param string $title The title (or caption) of this image (optional)
  * @param string $description The detailed description of this image (optional)
  * @param string $albumName The name of the album to put the image in (optional)
  * @return string The ID of new uploaded image | false
  */
 private function saveImg($uploadedImg, $title = '', $description = '', $albumName = '')
 {
     // Key
     $userId = CassandraUtil::import(Yii::app()->user->getId())->__toString();
     $key = $userId . '_' . CassandraUtil::uuid1();
     // Path
     $path = realpath(Yii::app()->params['storagePath']) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $userId;
     if (!is_dir($path)) {
         mkdir($path, 0775);
     }
     if (!empty($albumName)) {
         $path .= DIRECTORY_SEPARATOR . $albumName;
     }
     if (!is_dir($path)) {
         mkdir($path, 0775);
     }
     // Save the image information before processing the image
     $data = array('storage_path' => $uploadedImg->getTempName(), 'mime_type' => $uploadedImg->getType(), 'extension' => $uploadedImg->getExtensionName(), 'user_id' => Yii::app()->user->getId());
     if (!empty($title)) {
         $data['title'] = $title;
     }
     if (!empty($description)) {
         $data['description'] = $description;
     }
     if ($this->insert($key, $data) === false) {
         $uploadedImg->clean();
         return false;
     }
     // Render this image into different versions
     $photoTypes = Setting::model()->photo_types;
     // Get list of image types in JSON format. Decode it.
     $photoTypes = json_decode($photoTypes['value'], true);
     $img = Yii::app()->imagemod->load($data['storage_path']);
     $convertedImgs = array();
     foreach ($photoTypes as $type => $config) {
         $img->image_convert = 'jpg';
         $img->image_resize = true;
         $img->image_ratio = true;
         $img->image_x = $config['width'];
         $img->image_y = $config['height'];
         if (isset($config['suffix'])) {
             $img->file_safe_name = $key . $config['suffix'] . '.jpg';
         } else {
             $img->file_safe_name = $key . '.jpg';
         }
         $img->process($path);
         if (!$img->processed) {
             // Delete the original image
             $uploadedImg->clean();
             // Delete the record in db
             $this->delete($key);
             // Log the error
             Yii::log('Cannot resize the image ' . $data['storage_path'] . ' to ' . $path . '/' . $img->file_safe_name, 'error', 'application.modules.storage.Storage');
             // Delete all converted images
             foreach ($convertedImgs as $imgType => $imgPath) {
                 unlink($imgPath);
             }
             // Return false
             return false;
         } else {
             // Remember the path of converted image
             $convertedImgs[$type] = $img->file_dst_path;
         }
         // Update the database
         $data = array('storage_path' => $convertedImgs['original'], 'mime_type' => 'image/jpeg', 'extension' => 'jpg');
         unset($convertedImgs['original']);
         $data = array_merge($data, $convertedImgs);
         if ($this->insert($key, $data) === false) {
             // Delete the original image
             $uploadedImg->clean();
             // Delete the record in db
             $this->delete($key);
             // Log the error
             Yii::log('Cannot resize the image ' . $data['storage_path'] . ' to ' . $path . '/' . $img->file_safe_name, 'error', 'application.modules.storage.Storage');
             // Delete all converted images
             unlink($data['storage_path']);
             foreach ($convertedImgs as $imgType => $imgPath) {
                 unlink($imgPath);
             }
             // Return false
             return false;
         }
     }
     // Delete the temporary image
     $uploadedImg->clean();
     return $key;
 }