/** * Delete the old thumbnail image on save if changing it * @param array $options * * @return bool * @throws \Exception */ public function save(array $options = []) { /** * Remember these IDs now because the parent::save method clears * the isDirty flag and the original data. */ $originalThumbnailImageFileId = null; if ($this->isDirty('thumbnailImageFileId')) { $originalThumbnailImageFileId = $this->getOriginal('thumbnailImageFileId'); } // Save the model $result = parent::save($options); if ($originalThumbnailImageFileId && ($originalThumbnailImageFile = ImageFile::find($originalThumbnailImageFileId))) { $originalThumbnailImageFile->delete(); } return $result; }
/** * Save the Image model to the database. * If the fileId is changed (meaning the picture has changed) delete * the old file and generate a new thumbnail. Delete the old thumbnail file if one exists. * * @param array $options * * @return bool */ public function save(array $options = []) { $this->table = 'images'; /** * Remember these IDs now because the parent::save method clears * the isDirty flag and the original data. */ $generateNewThumbnail = false; $originalImageFileId = null; $originalThumbnailImageFileId = null; if ($this->isDirty('imageFileId') || !$this->exists) { $originalImageFileId = $this->getOriginal('imageFileId'); $this->setThumbnailImageFile(null); $originalThumbnailImageFileId = $this->getOriginal('thumbnailImageFileId'); $generateNewThumbnail = true; } // Save the model $result = parent::save($options); if ($originalImageFileId && ($originalImageFile = ImageFile::find($originalImageFileId))) { if ($originalImageFileId != $this->uncroppedImageFileId) { $originalImageFile->delete(); } } if ($originalThumbnailImageFileId && ($originalThumbnailImageFile = ImageFile::find($originalThumbnailImageFileId))) { $originalThumbnailImageFile->delete(); } if ($generateNewThumbnail && ($imageFile = ImageFile::find($this->imageFileId))) { /** @var ImageFile $imageFile */ $this->dispatch(new MakeThumbnailJob($imageFile)); } $this->table = 'view_images'; return $result; }