Commit 4274444d authored by nirgendswo's avatar nirgendswo

finish imageHelper

parent e5df8925
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class ImageHelperFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'App\ImageHelper';
}
}
......@@ -6,79 +6,113 @@ use Illuminate\Support\Facades\Lang;
use Eventviva\ImageResize;
/**
* Helper for View
* Helper for resizing and store images
*
* @author Björn Hase
*/
class ImageHelper
{
public function scale($filePath, $percentage)
{
/**
* scale image with percentage
*
* @param string $relativePath
* @param string $percentage
* @return string
*/
public function scale($relativePath, $percentage)
{
$src = $this->getFilePathSource($filePath);
$destinationPath = $this->getFilePathname($relativePath, 'relative_'.$percentage);
$image = $this->has($destinationPath, $relativePath);
if (file_exists($src)) {
$image = new ImageResize($src);
if ($image) {
$image->scale($percentage);
$dest = $this->getFilePathSource($filePath);
$image->save($dest);
$this->save($image, $destinationPath);
}
return $filePath;
return config('app.mediaPublicPath').'/'.$destinationPath;
}
public function resize($src, $width, $height)
/**
* resize image with width and height
*
* @param string $relativePath
* @param string $width
* @param string $height
* @return string
*/
public function resize($relativePath, $width, $height)
{
$src = $this->getFilePathSource($filePath);
$destinationPath = $this->getFilePathname($relativePath, $width.'_'.$height);
$image = $this->has($destinationPath, $filePathname);
if (file_exists($src)) {
$image = new ImageResize($src);
if ($image) {
$image->resize($width, $height);
$dest = $this->getFilePathSource($filePath);
$image->save($dest);
$this->save($image, $destinationPath);
}
return $filePath;
return config('app.mediaPublicPath').'/'.$destinationPath;
}
public function resizeToHeight($src, $height)
/**
* resize image height
*
* @param string $relativePath
* @param string $height
* @return string
*/
public function resizeToHeight($relativePath, $height)
{
$src = $this->getFilePathSource($filePath);
$destinationPath = $this->getFilePathname($relativePath, 'height_'.$height);
$image = $this->has($destinationPath, $relativePath);
if (file_exists($src)) {
$image = new ImageResize($src);
if ($image) {
$image->resizeToHeight($height);
$dest = $this->getFilePathSource($filePath);
$image->save($dest);
$this->save($image, $destinationPath);
}
return $filePath;
return config('app.mediaPublicPath').'/'.$destinationPath;
}
public function resizeToWidth($src, $width)
/**
* resize image width
*
* @param string $relativePath
* @param string $width
* @return string
*/
public function resizeToWidth($relativePath, $width)
{
$src = $this->getFilePathSource($filePath);
$destinationPath = $this->getFilePathname($filePathname, 'width_'.$width);
$image = $this->has($destination, $relativePath);
if (file_exists($src)) {
$image = new ImageResize($src);
if ($image) {
$image->resizeToWidth($width);
$dest = $this->getFilePathSource($filePath);
$image->save($dest);
$this->save($image, $destinationPath);
}
return $filePath;
return config('app.mediaPublicPath').'/'.$destinationPath;
}
protected function save($image, $filePathname)
{
$image->save(base_path().'/public/'.config('app.mediaPublicPath').'/'.$filePathname);
}
protected function getFilePathSource($src)
protected function getFilePathname($filePathnam, $suffix)
{
return base_path().'/'.env('APP_MEDIA_PATH', 'resources/media').'/'.$src;
return preg_replace('/(\.[^.]+)$/', sprintf('%s$1', '_'.$suffix), $filePathname);
}
protected function getFilePathDestination()
protected function has($destination, $source)
{
return base_path().'/public/media/'.$src;
$image = NULL;
// path to check if file exists
$exists = base_path().'/public/'.config('app.mediaPublicPath').'/'.$destination;
if (!file_exists($exists)) {
$image = new ImageResize(base_path().'/'.config('app.mediaPath').'/'.$source);
}
return $image;
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class ImageHelperServiceProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('App\ImageHelper', function() {
return new \App\Helpers\ImageHelper();
});
}
}
......@@ -79,6 +79,7 @@ $app->routeMiddleware([
$app->register(App\Providers\PageRepositoryServiceProvider::class);
$app->register(App\Providers\PageParserServiceProvider::class);
$app->register(App\Providers\ViewHelperServiceProvider::class);
$app->register(App\Providers\ImageHelperServiceProvider::class);
/*
|--------------------------------------------------------------------------
......
<?php
return [
'mediaPath' => 'resources/media',
'mediaPublicPath' => 'media',
'repositoryPath' => 'resources/data',
];
......@@ -2,6 +2,7 @@
"title": "At vero eos et accusam et justo duo dolores et ea rebum",
"view": "post",
"type": "post",
"thumbnail": "test.jpg",
"publishedAt": "14-01-2017 23:46:50"
}
Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.
......
......@@ -4,6 +4,7 @@
@inject('pageRepository', 'App\PageRepository')
@inject('pageService', 'App\PageService')
@inject('viewHelper', 'App\ViewHelper')
@inject('imageHelper', 'App\ImageHelper')
@foreach($pageRepository->paginate(
['type' => 'post'],
......@@ -19,9 +20,9 @@
<time class="post__time">
{{ $viewHelper->timeElapsed($post->data['publishedAt']) }}
</time>
@if (isset($post->data['media']))
@if (isset($post->data['thumbnail']))
<figure class="post__figure">
<img class="post__img" src="{{ $post->data['media'] }}">
<img class="post__img" src="{{ $imageHelper->resizeToWidth($post->data['thumbnail'], 1000) }}" />
</figure>
@endif
</header>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment