photo = $photo; $this->album = $photo->album; $this->albumSource = $this->album->getAlbumSource(); $this->imageHelper = new ImageHelper(); $this->themeHelper = new ThemeHelper(); } public function analyse($queueToken) { $photoFile = join(DIRECTORY_SEPARATOR, [ FileHelper::getQueuePath($queueToken), $this->photo->storage_file_name ]); $imageInfo = null; $originalPhotoResource = $this->imageHelper->openImage($photoFile, $imageInfo); if ($originalPhotoResource === false) { throw new \Exception(sprintf('The image "%s" does not appear to be a valid image, or cannot be read', pathinfo($photoFile, PATHINFO_FILENAME))); } $this->photo->width = $imageInfo[0]; $this->photo->height = $imageInfo[1]; $this->photo->mime_type = $imageInfo['mime']; // Read the Exif data $exifData = @exif_read_data($photoFile); $isExifDataFound = ($exifData !== false && is_array($exifData)); $angleToRotate = 0; // If Exif data contains an Orientation, ensure we rotate the original image as such if ($isExifDataFound && isset($exifData['Orientation'])) { switch ($exifData['Orientation']) { case 3: $angleToRotate = 180; break; case 6: $angleToRotate = 270; break; case 8: $angleToRotate = 90; break; } if ($angleToRotate > 0) { $originalPhotoResource = $this->imageHelper->rotateImage($originalPhotoResource, $angleToRotate); if ($angleToRotate == 90 || $angleToRotate == 270) { $this->photo->width = $imageInfo[1]; $this->photo->height = $imageInfo[0]; } $this->imageHelper->saveImage($originalPhotoResource, $photoFile, $imageInfo); } } if ($isExifDataFound) { $this->photo->metadata_version = self::METADATA_VERSION; $this->photo->taken_at = $this->metadataDateTime($exifData); $this->photo->camera_make = $this->metadataCameraMake($exifData); $this->photo->camera_model = $this->metadataCameraModel($exifData); $this->photo->camera_software = $this->metadataCameraSoftware($exifData); } $this->photo->is_analysed = true; $this->photo->save(); // Save the original $this->albumSource->saveOriginal($this->photo, $photoFile); $this->regenerateThumbnails($originalPhotoResource); } public function changeAlbum(Album $newAlbum) { /** @var IAlbumSource $currentSource */ $currentSource = $this->photo->album->getAlbumSource(); $newSource = $newAlbum->getAlbumSource(); // Get the current photo's path $file = new File($currentSource->getPathToPhoto($this->photo)); // Save to the new album $newSource->saveUploadedPhoto($file, $this->photo->storage_file_name); // Delete the original $this->delete(); // Update the ID and new file name $this->photo->album_id = $newAlbum->id; $this->photo->save(); // Switch to the new album source $this->albumSource = $newSource; // Regenerate the thumbnails in the new album $this->regenerateThumbnails(); } public function delete() { // Remove all thumbnails first - so if any fail, we don't delete the original $themeInfo = $this->themeHelper->info(); $thumbnailsRequired = $themeInfo['thumbnails']; /** @var mixed $thumbnail */ foreach ($thumbnailsRequired as $thumbnail) { $this->albumSource->deleteThumbnail($this->photo, $thumbnail['name']); } // Next remove the original photo $this->albumSource->deleteThumbnail($this->photo); // Finally remove the database record $this->photo->delete(); } public function flip($horizontal, $vertical) { $imageInfo = array(); $photoPath = $this->albumSource->getPathToPhoto($this->photo); $originalPhotoImage = $this->imageHelper->openImage($photoPath, $imageInfo); if ($this->imageHelper->flipImage($originalPhotoImage, boolval($horizontal), boolval($vertical))) { $this->imageHelper->saveImage($originalPhotoImage, $photoPath, $imageInfo); $this->regenerateThumbnails($originalPhotoImage); $this->photo->save(); } } public function regenerateThumbnails($originalPhotoResource = null) { if (is_null($originalPhotoResource)) { $imageInfo = null; $originalPhotoResource = $this->imageHelper->openImage($this->albumSource->getPathToPhoto($this->photo), $imageInfo); } // Generate and save thumbnails $themeInfo = $this->themeHelper->info(); $thumbnailsRequired = $themeInfo['thumbnails']; /** @var mixed $thumbnail */ foreach ($thumbnailsRequired as $thumbnail) { $generatedThumbnailPath = $this->imageHelper->generateThumbnail($originalPhotoResource, $this->photo, $thumbnail); $this->albumSource->saveThumbnail($this->photo, $generatedThumbnailPath, $thumbnail['name']); } } public function rotate($angle) { $imageInfo = array(); $photoPath = $this->albumSource->getPathToPhoto($this->photo); $originalPhotoImage = $this->imageHelper->openImage($photoPath, $imageInfo); $originalPhotoImage = $this->imageHelper->rotateImage($originalPhotoImage, intval($angle)); $this->imageHelper->saveImage($originalPhotoImage, $photoPath, $imageInfo); if ($angle == 90 || $angle == 270) { $width = $this->photo->width; $this->photo->width = $this->photo->height; $this->photo->height = $width; } $this->regenerateThumbnails($originalPhotoImage); $this->photo->save(); } private function metadataCameraMake(array $exifData) { if (isset($exifData['Make'])) { return $exifData['Make']; } return null; } private function metadataCameraModel(array $exifData) { if (isset($exifData['Model'])) { return $exifData['Model']; } return null; } private function metadataCameraSoftware(array $exifData) { if (isset($exifData['Software'])) { return $exifData['Software']; } return null; } private function metadataDateTime(array $exifData) { $dateTime = null; if (isset($exifData['DateTime'])) { $dateTime = $exifData['DateTime']; } if (!is_null($dateTime)) { $dateTime = preg_replace('/^([\d]{4}):([\d]{2}):([\d]{2})/', '$1-$2-$3', $dateTime); } return $dateTime; } }