Skip to content

Commit dc86003

Browse files
author
Timot Tarjani
committed
Improve image handling with HasImage trait
1 parent 9b461cd commit dc86003

19 files changed

+78
-67
lines changed

app/Controllers/BlogpostCommentController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function store(Request $request)
3131

3232
$blogpost_comment = new BlogpostComment($request->all());
3333
$blogpost_comment->blogpost_id = $request->input('blogpost_id');
34-
$blogpost_comment->user_id = \Auth::user()->id;
34+
$blogpost_comment->author_id = \Auth::user()->id;
3535

3636
return $this->redirectToSelf()->withMessage(
3737
$blogpost_comment->save() ? ['success' => trans('message.successfully_created_blogpost_comment')]
@@ -61,7 +61,7 @@ public function update(BlogpostComment $blogpost_comment)
6161

6262
$blogpost_comment->blogpost_id = $this->request->input('blogpost_id');
6363
$blogpost_comment->comment = $this->request->input('comment');
64-
$blogpost_comment->user_id = \Auth::user()->id;
64+
$blogpost_comment->author_id = \Auth::user()->id;
6565

6666

6767
return $this->redirectToSelf()->withMessage(

app/Controllers/BlogpostController.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class BlogpostController extends Controller
1111

1212

1313
protected $itemPerPage = 25;
14+
15+
//TODO Use model path
1416
protected $imagePath = 'images/blogposts';
1517

1618
/**
@@ -19,6 +21,7 @@ class BlogpostController extends Controller
1921
*/
2022
public function before()
2123
{
24+
//TODO Use model path
2225
\File::ensureDirectoryExists($this->imagePath . '/thumbs');
2326
}
2427

@@ -66,10 +69,11 @@ public function store(Request $request)
6669

6770
if ($request->hasFile('up_file')) {
6871

72+
//TODO Use model path
6973
$img = $request->up_file->store($this->imagePath);
70-
$blogpost->image = basename($img);
74+
$blogpost->attachImage($img);
7175
if (extension_loaded('gd')) {
72-
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save(storage_path($this->imagePath . '/thumbs/' . $blogpost->image));
76+
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save($blogpost->getThumbnailDirectory(). DIRECTORY_SEPARATOR . $blogpost->image);
7377
}
7478
}
7579

@@ -133,10 +137,10 @@ public function update(Request $request, Blogpost $blogpost)
133137

134138
$img = $request->up_file->store($this->imagePath);
135139

136-
$blogpost->image = basename($img);
140+
$blogpost->attachImage($img);
137141

138142
if (extension_loaded('gd')) {
139-
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save(storage_path($this->imagePath . '/thumbs/' . $blogpost->image));
143+
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save($blogpost->getThumbnailDirectory(). DIRECTORY_SEPARATOR . $blogpost->image);
140144
}
141145
}
142146

app/Controllers/HeaderImageController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function index()
2727

2828
$this->view->title(trans('Header Images'));
2929
return $this->view->render('media/header_images', [
30-
'slider_images' => HeaderImage::active()->get()->orderBy('order', 'ASC'),
31-
'slider_disabled' => HeaderImage::inactive()->get()->orderBy('order','ASC'),
30+
'slider_images' => HeaderImage::active()->orderBy('order', 'ASC')->get(),
31+
'slider_disabled' => HeaderImage::inactive()->orderBy('order','ASC')->get(),
3232
]);
3333
}
3434

@@ -83,7 +83,7 @@ public function store(Request $request)
8383

8484
if ($request->hasFile('up_file')) {
8585

86-
$header_image->image = basename($request->up_file->store($this->imagePath));
86+
$header_image->attachImage($request->up_file->store($this->imagePath));
8787
}
8888

8989
if ($header_image->save()) {

app/Controllers/PageController.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ public function store(Request $request)
8080

8181
$img = $request->up_file->store($this->imagePath);
8282

83-
$page->image = basename($img);
83+
$page->attachImage($img);
8484

8585

8686
if (extension_loaded('gd')) {
87-
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save(storage_path($this->imagePath . '/thumbs/' . $page->image));
87+
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save($page->getThumbnailDirectory(). DIRECTORY_SEPARATOR . $page->image);
8888
}
8989
}
9090

@@ -148,10 +148,11 @@ public function update(Request $request, Page $page)
148148

149149
$img = $request->up_file->store($this->imagePath);
150150

151-
$page->image = basename($img);
151+
$page->attachImage($img);
152152

153153
if (extension_loaded('gd')) {
154-
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save(storage_path($this->imagePath . '/thumbs/' . $page->image));
154+
// TODO Thumbnail size to config
155+
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save($page->getThumbnailDirectory(). DIRECTORY_SEPARATOR . $page->image);
155156
}
156157
}
157158

app/Controllers/SettingsController.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@ class SettingsController extends Controller
1515

1616
public function before()
1717
{
18-
if (!file_exists("storage/images/logos")) {
19-
File::makeDirectory("storage/images/logos", $mode = 0777, true, true);
20-
}
21-
22-
if (!file_exists("storage/images/favicons")) {
23-
File::makeDirectory("storage/images/favicons", $mode = 0777, true, true);
24-
}
18+
\File::ensureDirectoryExists('images/logos');
19+
\File::ensureDirectoryExists('images/favicons');
2520
}
2621

2722

app/Controllers/UserController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public function store(Request $request)
7979

8080
$img = $request->up_file->store($this->imagePath);
8181

82-
$user->image = basename($img);
82+
$user->attachImage($img);
8383

8484
if (extension_loaded('gd')) {
85-
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save(storage_path($this->imagePath . '/thumbs/' . $user->image));
85+
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save($user->getThumbnailDirectory(). DIRECTORY_SEPARATOR . $user->image);
8686
}
8787
}
8888

@@ -150,10 +150,10 @@ public function update(Request $request, User $user)
150150

151151
$img = $request->up_file->store($this->imagePath);
152152

153-
$user->image = basename($img);
153+
$user->attachImage($img);
154154

155155
if (extension_loaded('gd')) {
156-
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save(storage_path($this->imagePath . '/thumbs/' . $user->image));
156+
\Intervention\Image\ImageManagerStatic::make(storage_path($img))->fit(300, 200)->save($user->getThumbnailDirectory(). DIRECTORY_SEPARATOR . $user->image);
157157
}
158158
}
159159

app/Controllers/UserRoleController.php

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ class UserRoleController extends Controller
1313
public function index()
1414
{
1515

16-
17-
18-
// dd($permission_list);
19-
2016
$this->view->title('User roles');
2117
return $this->view->render('users/roles/index', [
2218
'all_user_roles' => \App\Model\UserRole::all(),

app/Controllers/WebsiteController.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function __construct(Request $request){
3434
*/
3535
public function index($page = null)
3636
{
37+
return $this->show($page);
38+
}
3739

40+
public function show($page){
3841
if (\Session::has("lang")) {
3942
\App::setLocale(\Session::get("lang"));
4043
}
@@ -61,8 +64,8 @@ public function index($page = null)
6164
\App\Model\Visits::newVisitor($this->request);
6265

6366

64-
if ($requested_page != NULL) {
65-
if (isset($requested_page->url) && $requested_page->url != "" && $theme_engine->templateExists($requested_page->url)) {
67+
if (!empty($requested_page)) {
68+
if (!empty($requested_page->url) && $theme_engine->templateExists($requested_page->url)) {
6669
$template = "page_templates." . $requested_page->url;
6770
} else {
6871
$template = 'page';
@@ -83,10 +86,6 @@ public function index($page = null)
8386
]);
8487
}
8588

86-
public function show($page){
87-
return $this->index($page);
88-
}
89-
9089
public function registration()
9190
{
9291

app/Helpers/Functions/link.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ function plugin_link($link,$param = null){
1111

1212
}
1313

14-
14+
/**
15+
* @deprecated deprecated since version 1.0.0
16+
*/
1517
function namespace_to_slug($string){
1618
return ltrim(strtolower(preg_replace('/(?<!\ )[A-Z]/', '-$0', $string)),"-");
1719
}

app/Http/RouteResolver.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace App\Http;
44

5+
/**
6+
* @deprecated deprecated since version 1.0.0
7+
*/
58
class RouteResolver{
69

710
private $defaultNamespace = "\App\\Controllers\\";

app/Libs/Theme.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ public function hasWebsiteDownTemplate()
107107

108108
public function getRequiredCoreVersion()
109109
{
110-
return isset($this->getInfo('requires')->core) ? $this->getInfo('requires')->core : NULL;
110+
return ltrim(empty($this->getInfo('requires')->core)? 'v0.0.0' : $this->getInfo('requires')->core, 'v');
111111
}
112112

113113
public function isCompatibleWithCore()
114114
{
115-
return \Composer\Semver\Comparator::greaterThanOrEqualTo(\App\Model\SystemUpgrade::getCurrentVersion()->version, $this->getRequiredCoreVersion());
115+
return \Composer\Semver\Comparator::greaterThanOrEqualTo(ltrim(config('horizontcms.version'), 'v'), $this->getRequiredCoreVersion());
116116
}
117117
}

app/Libs/ViewResolver.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Config;
66

7+
/**
8+
* @deprecated deprecated since version 1.0.0
9+
*/
710
class ViewResolver
811
{
912

app/Model/Blogpost.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ class Blogpost extends Model
3030
protected $search = ['title', 'summary', 'text'];
3131

3232
protected $defaultImage = "resources/images/icons/newspaper.png";
33+
3334

34-
protected $imageDir = "storage/images/blogposts";
35-
36-
35+
//TODO Use https://github.com/spatie/laravel-sluggable
3736
public static function findBySlug($slug)
3837
{
3938

4039
$blogpost = self::where('slug', $slug)->get()->first();
4140

42-
if (!isset($blogpost)) {
41+
if (isset($blogpost)) {
4342
return $blogpost;
4443
} else {
4544

app/Model/BlogpostComment.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ class BlogpostComment extends Model {
1717
* @var array
1818
*/
1919
protected $fillable = [
20-
'blogpost_id', 'comment', 'active',
20+
'blogpost_id', 'author_id' , 'comment', 'active',
2121
];
2222

2323
public function blogpost(){
2424
return $this->belongsTo(\App\Model\Blogpost::class,'blogpost_id','id');
2525
}
2626

27-
// TODO Use HasAuthor trait
2827
public function user(){
2928
return $this->author();
3029
}

app/Model/HeaderImage.php

-14
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,5 @@ class HeaderImage extends Model {
2121
protected $fillable = [
2222
'title', 'link' ,'description', 'image', 'active',
2323
];
24-
25-
public $timestamps = false;
26-
27-
protected $imageDir = "storage/images/header_images";
28-
29-
//TODO Use local scope
30-
/* public static function getActive($order = 'ASC'){
31-
return self::where('active','>',0)->orderBy('order',$order);
32-
} */
33-
34-
//TODO Use local scope
35-
/* public static function getInactive($order = 'ASC'){
36-
return self::where('active','=',0)->orderBy('order',$order);
37-
} */
3824

3925
}

app/Model/Page.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class Page extends Model {
2626

2727
protected $defaultImage = "resources/images/icons/page.png";
2828

29-
protected $imageDir = "storage/images/pages";
30-
3129
//TODO Use local scope
3230
public static function home(){
3331
return self::find(Settings::get('home_page'));
@@ -37,7 +35,7 @@ public static function findBySlug($slug){
3735

3836
$page = self::where('slug',$slug)->get()->first();
3937

40-
if($page!=NULL){
38+
if(isset($page)){
4139
return $page;
4240
}else{
4341

app/Model/Plugin.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Plugin extends Model
2020

2121
protected $defaultImage = "resources/images/icons/plugin.png";
2222

23-
protected $imageDir = null;
23+
protected $imageDir;
2424

2525
public function __construct($root_dir = null)
2626
{

app/Model/Trait/HasImage.php

+31-4
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,52 @@
55
trait HasImage
66
{
77

8+
public function attachImage($image)
9+
{
10+
$this->image = basename($image);
11+
}
12+
13+
public function getImageDirectory()
14+
{
15+
16+
return empty($this->imageDir) ? 'storage/images/' . $this->getTable() : rtrim($this->imageDir, DIRECTORY_SEPARATOR);
17+
}
18+
19+
public function getThumbnailDirectory()
20+
{
21+
return $this->getImageDirectory() . DIRECTORY_SEPARATOR . 'thumbs';
22+
}
23+
824
public function hasImage()
925
{
1026
return (isset($this->image) && !empty($this->image));
1127
}
1228

29+
public function imageFileExists()
30+
{
31+
return $this->hasImage() && file_exists($this->getImageDirectory() . DIRECTORY_SEPARATOR . $this->image);
32+
}
33+
34+
public function thumbnailFileExists()
35+
{
36+
return $this->hasImage() && file_exists($this->getImageDirectory() . DIRECTORY_SEPARATOR . $this->image);
37+
}
38+
1339
public function getThumb()
1440
{
1541

16-
if ($this->hasImage() && file_exists(rtrim($this->imageDir, DIRECTORY_SEPARATOR) . "/thumbs/" . $this->image)) {
17-
return url(rtrim($this->imageDir, DIRECTORY_SEPARATOR). "/thumbs/" . $this->image);
42+
if ($this->thumbnailFileExists()) {
43+
return url($this->getThumbnailDirectory() . DIRECTORY_SEPARATOR . $this->image);
1844
} else {
1945
return $this->getImage();
2046
}
2147
}
2248

2349
public function getImage()
2450
{
25-
if ($this->hasImage() && file_exists(rtrim($this->imageDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $this->image)) {
26-
return url(rtrim($this->imageDir, DIRECTORY_SEPARATOR) . "/" . $this->image);
51+
52+
if ($this->imageFileExists()) {
53+
return url($this->getImageDirectory() . DIRECTORY_SEPARATOR . $this->image);
2754
} else {
2855
return url($this->getDefaultImage());
2956
}

app/Model/User.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
4545

4646

4747
protected $defaultImage = "resources/images/icons/profile.png";
48-
49-
protected $imageDir = "storage/images/users";
48+
5049

5150
public static function findBySlug($slug){
5251

0 commit comments

Comments
 (0)