Просмотр поста

.
Delphinum

По поводу превьюшек в текущем проекте решил эту проблему с наименьшими потерями. Сделал примерно так:

FileStorage (+/-)

class FileStorage{
  // ...

  public function addVariant($id, $name, callable $handler){
    $filePath = $this->dir . '/' . $id;
    $variantPath = $filePath . '_' . $name;

    if(!file_exists($filePath)){
      throw new RuntimeException('File not found');
    }

    $editor = new SimpleImage($filePath);
    list($quality, $mimetype) = call_user_func_array($handler, [$editor]);
    $editor->save($variantPath, $quality, $mimetype);
  }

  /**
   * @param string $id Идентификатор исходного файла.
   * @param string $name Имя вариации исходного файла.
   *
   * @return string|null Путь до вариации исходного файла.
   */
  public function atVariant($id, $name){
    $path = $this->dir . '/' . $id . '_' . $name;
    if(!file_exists($path)){
      return null;
    }

    return $path;
  }
}



Работать с этим надо так:
$filestorage = new FileStorage(...);

$filestorage->addVariant($idFile, 'small', function(SimpleImage $editor){
  $editor->crop(68, 68);
  return [80, IMG_JPG];
});

$smallFile = $filestorage->atVariant($idFile, 'small');
echo $smallFile->getAddress();