This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
In need in the backend the full file path of a uploaded image so I can used the image url in afterCreate(). My model:
namespace name\pluginname\Models; use Model; use DB; use Validator; /** * Post Model */ class Post extends Model { public $table = mytable'; protected $guarded = ['*']; public $attachOne = [ 'featured_images' => ['System\Models\File'] ]; public function afterCreate() { $post = Post::find($this->id); if ($post->featured_images) $featuredImage = $post->featured_images->getPath(); else $featuredImage = 'http://placehold.it/220x300'; traceLog('image url: '. $featuredImage); } }
The var $featuredImage
has always the placeholder image because $post->featured_images
is empty. I do something wrong but I realy don't see what I do wrong...
Last updated
Im still searching for how I can get the image path. What I don't understand is. When I dump $post and $this is see nothing about the uploaded image. Dump of $this:
production.INFO: THIS: {"titel":"adad","slug":"adad","content":"\u200basdasdas","published_at":"2015-07-01 11:00:00","seoTitel":"adad","sitemap":"1","metarobots":"none","updated_at":"2015-07-08 10:41:51","created_at":"2015-07-08 10:41:51","id":"172","featured_images":null}
The reason I need the image link. Is because I want to share the post on Facebook after the post is saved in the Database.
Last updated
Images are stored in the table system_files
with your model namespace as attachment_type
and the record id of the model as attachment_id
To get the path you take the disk_name field which is something like
'558ab0f0aa231581256800.jpg'
Then you can deduct the file path which will start with:
'storage\app\uploads\public\first 3 letters of filename\second 3 letters of filename\3rd 3 letters of filename\filename.jpg'
So in case of the example file I gave it will bestored in
'storage\app\uploads\public\558\ab0\f0a\558ab0f0aa231581256800.jpg'
Last updated
That look very easy! So I have just to query the system_files table for the file name and then I can create the directory where the file is located. That sounds very easy :)
The getPath() wil not work in the afterCreate() function ?
Last updated
@Wouter; I thing is something about Deferred binding. That I can't understand yet. To get the $this->featured_images
in event afterSave
you must create and save again the model. After the second save you has control of the attached file. Like I say before, I don't understand how deferred binding works, I would like to know, and can use the model events to have more control over attached files. In my case, I need it to resize biggest images and apply watermark.
Them, to have the full path you must need to use
$fullPath = base_path() . $this->featured_images->getPath();
Last updated
@planetadeleste ; I have read many times about Deferred binding but I realy not understand how I have to save a biding because $this->featured_images
is empty.
When I dump $this nowhere is there anything what as any relation with the uploaded file.
What I tought before I have to was:
$post = Post::find($this->id); $file= new File; $file->featured_images = this->featured_images; $file->save();
But then I get the follow error syntax error, unexpected '->' (T_OBJECT_OPERATOR)
The error reverence to line $file->featured_images = this->featured_images;
And you try this?:
$sessionKey = uniqid('session_key', true);
$images = $this->featured_images()->withDeferred($sessionKey)->get();
Wouter said:
@planetadeleste ; I have read many times about Deferred binding but I realy not understand how I have to save a biding because
$this->featured_images
is empty. When I dump $this nowhere is there anything what as any relation with the uploaded file.What I tought before I have to was:
$post = Post::find($this->id); $file= new File; $file->featured_images = this->featured_images; $file->save();But then I get the follow error
syntax error, unexpected '->' (T_OBJECT_OPERATOR)
The error reverence to line
$file->featured_images = this->featured_images;
planetadeleste said:
And you try this?:
$sessionKey = uniqid('session_key', true); $images = $this->featured_images()->withDeferred($sessionKey)->get();
$images
will be still empty... I realy don't understand how the get the image path / name with Deferred binding.
If I cant find out how it will be work I can have to use the solution Tschallacka are given....
Last updated
I see in the debug bar the follow query.
update `system_files` set `field` = 'featured_images', `attachment_id` = '4180', `attachment_type` = 'AuthorName\Blog\Models\Post', `updated_at` = '2015-07-09 22:20:04' where `id` = '79'
So there will be created a biding. But now is the question how do I access the biding to know where the image is located.
Last updated
I don't know why you can't get the full path. Let me show my script, fire on afterSave
and works fine.
I try use debug($images)
and give me all recently uploaded images (with deferred)
public function afterSave()
{
$sessionKey = post('_session_key');
$images = $this->images()->withDeferred($sessionKey)->get();
if ($images->count()) {
/** @var DashboardProperty $settings */
$settings = DashboardProperty::instance();
foreach ($images as $image) {
/** @var \Intervention\Image\Image $img */
$img = Image::make(base_path() . $image->getPath());
$imgHeight = $img->height();
$imgWidth = $img->width();
$width = null;
$height = null;
if ($imgWidth > $settings->max_image_width || $imgHeight > $settings->max_image_height) {
if ($imgWidth >= $imgHeight) {
$width = $settings->max_image_width;
} else {
$height = $settings->max_image_height;
}
$img->resize(
$width,
$height,
function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
}
)->save();
}
}
}
}
Config::get('filesystems.disks.local.root', storage_path().'/app').
'/'.$featuredImage->getDiskPath();
@planetadeleste : Thanks for posting your function. I will look to that later because i never think about the media manager and i prefer now the media manager for selecting images.
But i have also to upload some docs so i will play again with deferred binding. I hope i will understand everything with your function.
I think that maybe could be "ugly but works :)"
public function afterSave(){
$last = self::where('id','=',$this->id)->first();
$path = $last->img->getPah(); //works but costs an extra query :)
}
ukebako8008 said:
Config::get('filesystems.disks.local.root', storage_path().'/app'). '/'.$featuredImage->getDiskPath();
just use $featuredImage->getLocalPath();
Last updated
planetadeleste said:
I don't know why you can't get the full path. Let me show my script, fire on
afterSave
and works fine. I try usedebug($images)
and give me all recently uploaded images (with deferred)public function afterSave() { $sessionKey = post('_session_key'); $images = $this->images()->withDeferred($sessionKey)->get(); if ($images->count()) { /* @var DashboardProperty $settings / $settings = DashboardProperty::instance(); foreach ($images as $image) { /* @var \Intervention\Image\Image $img / $img = Image::make(base_path() . $image->getPath()); $imgHeight = $img->height(); $imgWidth = $img->width(); $width = null; $height = null; if ($imgWidth > $settings->max_image_width || $imgHeight > $settings->max_image_height) { if ($imgWidth >= $imgHeight) { $width = $settings->max_image_width; } else { $height = $settings->max_image_height; }
$img->resize( $width, $height, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); } )->save(); } } }
}
"use Image;" not working . Image::make also giving error. what do I need to do to make image::make work
Last updated
@ In this code, I use Intervention\Image lib. You can see that in the comment above Image:make
.
/** @var \Intervention\Image\Image $img */
osarzola said:
I think that maybe could be "ugly but works :)"
public function afterSave(){ $last = self::where('id','=',$this->id)->first(); $path = $last->img->getPah(); //works but costs an extra query :) }
Hi could you please explain where exactly you put this bit of code? Controller or Model?
This probably should have been logged on GitHub as it is an issue with the code. Every aspect of October returns a fully qualified URL except for the system file paths, this has been fixed in Build 401+