/** * Prepares the list data */ public function prepareVars() { $value = $this->getLoadValue(); $this->vars['value'] = $value; $this->vars['imageUrl'] = $value ? MediaLibrary::url($value) : ''; $this->vars['field'] = $this->formField; $this->vars['prompt'] = str_replace('%s', '<i class="icon-folder"></i>', trans($this->prompt)); $this->vars['mode'] = $this->mode; }
/** * @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; } }
/** * Converts supplied file to a URL relative to the media library. * @param string $file Specifies the media-relative file * @return string */ public function mediaUrl($file = null) { return MediaLibrary::url($file); }
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); }
/** * @dataProvider validPathsProvider */ public function testValidPathsOnValidatePath($path) { MediaLibrary::validatePath($path); }
/** * Returns the raw contents of sourcePath. * * @param $path * * @return array */ protected function getFolderContents($path) { return $this->mediaLibrary->listFolderContents($path, 'title', 'image'); }
public function onResizeImage() { $cropSessionKey = Input::get('cropSessionKey'); if (!preg_match('/^[0-9a-z]+$/', $cropSessionKey)) { throw new ApplicationException('Invalid input data'); } $width = trim(Input::get('width')); if (!strlen($width) || !ctype_digit($width)) { throw new ApplicationException('Invalid input data'); } $height = trim(Input::get('height')); if (!strlen($height) || !ctype_digit($height)) { throw new ApplicationException('Invalid input data'); } $path = Input::get('path'); $path = MediaLibrary::validatePath($path); $params = array('width' => $width, 'height' => $height); return $this->getCropEditImageUrlAndSize($path, $cropSessionKey, $params); }