Beispiel #1
0
 /**
  * @param \Agency\Media\Photos\UploadedPhoto $original
  * @param                                        $small_dimensions
  * @param                                        $thumb_dimensions
  * @param                                        $square_dimensions
  *
  * @return array
  */
 public function resize(UploadedPhoto $original, $small_dimensions, $thumb_dimensions, $square_dimensions)
 {
     /**
      * @todo  Handle exceptions and delete cached files when they occur.
      */
     // store the resized stuff here
     $resized = [];
     // trash: hold all the file paths that needs to be removed from filesystem (unliked)
     $trash = [];
     // extract photo data
     $file = $original->file();
     $file_name = $original->name();
     $file_path = $original->path();
     $file_extention = $original->extension();
     $mime = $original->mime();
     $original_width = $original->width();
     $original_height = $original->height();
     $crop_width = $original->cropWidth();
     $crop_height = $original->cropHeight();
     $crop_x = $original->cropX();
     $crop_y = $original->cropY();
     $name = $this->nameFile($file_name, $file_extention);
     $original_photo = $this->editor->makePhoto($file_path);
     $original_photo->name = $file_name;
     $original_path = $this->editor->cache($original_photo);
     $trash[] = $original_path;
     // set original photo
     $resized['original'] = ['name' => $name, 'file' => $original_photo, 'mime' => $mime];
     // determine whether this is a landscape or portrait image
     $orientation = $this->editor->orientation($original_width, $original_height);
     if ($orientation === 'landscape') {
         $small_width = $small_dimensions['width'];
         $small_height = $original_height;
     } else {
         $small_width = $original_width;
         $small_height = $small_dimensions['height'];
     }
     // generate the small photo
     $small_name = $this->nameFile($name, $file_extention, 'small', true);
     $small = $this->editor->scale($file_path, $small_width, $small_height);
     $small_path = $this->editor->cache($small);
     $trash[] = $small_path;
     // set the edited file's name
     $small->name = $file_name;
     $resized['small'] = ['file' => $small, 'name' => $small_name, 'mime' => $mime];
     // crop photo according to the cropping dimensions
     // to get a 3/2 aspect ratio
     $cropped = $this->editor->crop($file_path, $crop_width, $crop_height, $crop_x, $crop_y);
     // cache the generated crop
     $cropped_path = $this->editor->cache($cropped);
     $trash[] = $cropped_path;
     // generate thumbnail
     $thumb_name = $this->nameFile($name, $file_extention, 'thumb', true);
     $thumb = $this->editor->resize($cropped_path, $thumb_dimensions['width'], $thumb_dimensions['height']);
     // set the edited file's name
     $thumb->name = $file_name;
     // cache the generated thumbnail
     $thumb_path = $this->editor->cache($thumb);
     $trash[] = $thumb_path;
     // set thumbnail photo
     $resized['thumbnail'] = ['name' => $thumb_name, 'file' => $thumb, 'mime' => $mime];
     // generate square
     $square_name = $this->nameFile($name, $file_extention, 'sq', true);
     $square = $this->editor->crop($thumb_path, $square_dimensions['width'], $square_dimensions['height'], $crop_x = 50);
     $square->name = $file_name;
     $square_path = $this->editor->cache($square);
     $trash[] = $square_path;
     $resized['square'] = ['name' => $square_name, 'file' => $square, 'mime' => $mime];
     // remove cached files
     array_map(function ($path) {
         @unlink($path);
     }, $trash);
     return $resized;
 }