Example #1
0
 /**
  * @param array $photo
  * @param array $photo Photo data
  * @param array $options Available options
  * <pre>
  * fallback: A fallback photo to use when photo is not found
  * transform: Transformation options to be applied to photo
  * </pre>
  * @return bool|PhotoResult
  */
 public function build($photo, array $options = array())
 {
     $options = array_merge(array('transform' => array(), 'fallback' => null), $options);
     if (empty($photo)) {
         // Could not find photo data
         if ($options['fallback'] == null || !$this->storageManager->has(StorageManager::FALLBACK_STORAGE)) {
             // No fallback photo is defined or no fallback storage added
             // We shouldn't probably continue. Seems developer prefers
             // to handle missing images manually.
             return false;
         }
         // Construct default data
         $photo = array('photo_id' => 0, 'storage_name' => StorageManager::FALLBACK_STORAGE, 'file_name' => pathinfo($options['fallback'], PATHINFO_FILENAME), 'file_path' => $options['fallback'], 'file_mime' => 'image/png', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'));
     }
     $photoResult = new PhotoResult($photo);
     $storage = $this->storageManager->get($photo['storage_name']);
     if (!empty($options['transform'])) {
         // Transformation options available
         $modifiedFileName = $this->generateModifiedSaveName($photo['file_path'], $options['transform']);
         $photoResult->setOriginalFilePath($photo['file_path']);
         $photoResult->setOriginalPath($storage->getPhotoPath($photo['file_path']));
         if (!($info = $storage->getInfo($modifiedFileName))) {
             // Only do image manipulation once
             // (ie if file does not exists)
             list($modifiedFileName, $info['file_size']) = $this->transformPhoto($storage, $storage->getPhotoResource($photoResult->originalFilePath(), tempnam($this->options['tmp_dir'], 'sp')), $modifiedFileName, $photo['file_mime'], $options['transform']);
         }
         $photoResult->setFileSize($info['file_size']);
         // Set the file path to the new modified photo path
         $photoResult->setFilePath($modifiedFileName);
     }
     $photoResult->setPath($storage->getPhotoPath($photoResult->filePath()));
     $photoResult->setUrl($storage->getPhotoUrl($photoResult->filePath()));
     return $photoResult;
 }
 /**
  * @param \SimplePhoto\PhotoResult $photo
  * @return array
  */
 public function getPhotosAttribute($photo)
 {
     return ['url' => $photo->url()];
 }