/** * RandomImages constructor. * * Get an MediaLibrary instance to work with. * * @param CodeBase|null $cmsObject * @param array $properties */ public function __construct(CodeBase $cmsObject = null, $properties = [], $mediaLibrary = null) { parent::__construct($cmsObject, $properties); $this->mediaLibrary = $mediaLibrary ?: MediaLibrary::instance(); $storagePath = rtrim(Config::get('cms.storage.media.path', '/storage/app/media'), '/'); $this->storagePath = str_replace('/storage', '', $storagePath); }
/** * @param $username * * @return string|null */ private function findAvatar($username) { $library = MediaLibrary::instance(); $files = $library->listFolderContents($this->imageStoragePath, 'title', 'image'); foreach ($files as $file) { $pathinfo = pathinfo($file->publicUrl); if ($pathinfo['filename'] == $username) { $file = new File(); $file->is_public = $this->imagePublic; $file->fromFile(base_path() . $pathinfo['dirname'] . '/' . $pathinfo['basename']); return $file; } } }
/** * @return bool|string * @throws \SystemException */ public function getContent() { switch ($this->popup->content_type) { case 'imageUrl': return "<img src='{$this->popup->content_image_url}'>"; break; case 'imageUpload': return '<img src="' . MediaLibrary::instance()->getPathUrl($this->popup->content_image_upload) . '">'; break; case 'page': /** @var Page $page */ $page = Page::load(Theme::getActiveTheme(), $this->popup->content_page); $cms = new CmsController(); return $cms->run($page->url)->getContent(); break; case 'markdown': return \Markdown::parse($this->popup->content_markdown); break; } }
protected function cropImage($imageSrcPath, $selectionData, $cropSessionKey, $path) { $originalFileName = basename($path); $path = rtrim(dirname($path), '/') . '/'; $fileName = basename($imageSrcPath); if (strpos($fileName, '..') !== false || strpos($fileName, '/') !== false || strpos($fileName, '\\') !== false) { throw new SystemException('Invalid image file name.'); } $selectionParams = ['x', 'y', 'w', 'h']; foreach ($selectionParams as $paramName) { if (!array_key_exists($paramName, $selectionData)) { throw new SystemException('Invalid selection data.'); } if (!ctype_digit($selectionData[$paramName])) { throw new SystemException('Invalid selection data.'); } } $sessionDirectoryPath = $this->getCropSessionDirPath($cropSessionKey); $fullSessionDirectoryPath = temp_path($sessionDirectoryPath); if (!File::isDirectory($fullSessionDirectoryPath)) { throw new SystemException('The image editing session is not found.'); } /* * Find the image on the disk and resize it */ $imagePath = $fullSessionDirectoryPath . '/' . $fileName; if (!File::isFile($imagePath)) { throw new SystemException('The image is not found on the disk.'); } $extension = pathinfo($originalFileName, PATHINFO_EXTENSION); $targetImageName = basename($originalFileName, '.' . $extension) . '-' . $selectionData['x'] . '-' . $selectionData['y'] . '-' . $selectionData['w'] . '-' . $selectionData['h'] . '-'; $targetImageName .= time(); $targetImageName .= '.' . $extension; $targetTmpPath = $fullSessionDirectoryPath . '/' . $targetImageName; /* * Crop the image, otherwise copy original to target destination. */ if ($selectionData['w'] == 0 || $selectionData['h'] == 0) { File::copy($imagePath, $targetTmpPath); } else { $resizer = Resizer::open($imagePath); $resizer->resample($selectionData['x'], $selectionData['y'], $selectionData['w'], $selectionData['h'], $selectionData['w'], $selectionData['h']); $resizer->save($targetTmpPath, 95); } /* * Upload the cropped file to the Library */ $targetFolder = $path . 'cropped-images'; $targetPath = $targetFolder . '/' . $targetImageName; $library = MediaLibrary::instance(); $library->put($targetPath, file_get_contents($targetTmpPath)); return ['publicUrl' => $library->getPathUrl($targetPath), 'documentType' => MediaLibraryItem::FILE_TYPE_IMAGE, 'itemType' => MediaLibraryItem::TYPE_FILE, 'path' => $targetPath, 'title' => $targetImageName, 'folder' => $targetFolder]; }
protected function cropImage($imageSrcPath, $selectionData, $cropSessionKey, $path) { $originalFileName = basename($path); $path = rtrim(dirname($path), '/') . '/'; $fileName = basename($imageSrcPath); if (strpos($fileName, '..') !== false || strpos($fileName, '/') !== false || strpos($fileName, '\\') !== false) { throw new SystemException('Invalid image file name.'); } $selectionParams = ['x', 'y', 'w', 'h']; foreach ($selectionParams as $paramName) { if (!array_key_exists($paramName, $selectionData)) { throw new SystemException('Invalid selection data.'); } if (!ctype_digit($selectionData[$paramName])) { throw new SystemException('Invalid selection data.'); } } $sessionDirectoryPath = $this->getCropSessionDirPath($cropSessionKey); $fullSessionDirectoryPath = temp_path($sessionDirectoryPath); if (!File::isDirectory($fullSessionDirectoryPath)) { throw new SystemException('The image editing session is not found.'); } // Find the image on the disk and resize it $imagePath = $fullSessionDirectoryPath . '/' . $fileName; if (!File::isFile($imagePath)) { throw new SystemException('The image is not found on the disk.'); } $extension = pathinfo($originalFileName, PATHINFO_EXTENSION); $targetImageName = basename($originalFileName, '.' . $extension) . '-' . $selectionData['x'] . '-' . $selectionData['y'] . '-' . $selectionData['w'] . '-' . $selectionData['h'] . '-'; $targetImageName .= time(); $targetImageName .= '.' . $extension; $targetTmpPath = $fullSessionDirectoryPath . '/' . $targetImageName; if ($selectionData['w'] == 0 || $selectionData['h'] == 0) { // If cropping is not required, copy the oiginal image to the target destination. File::copy($imagePath, $targetTmpPath); } else { $resizer = Resizer::open($imagePath); $resizer->resample($selectionData['x'], $selectionData['y'], $selectionData['w'], $selectionData['h'], $selectionData['w'], $selectionData['h']); $resizer->save($targetTmpPath, 95); } // Upload the cropped file to the Library $targetPath = $path . 'cropped-images/' . $targetImageName; $library = MediaLibrary::instance(); $library->put($targetPath, file_get_contents($targetTmpPath)); return $library->getPathUrl($targetPath); }