This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Does anyone quickly know how to link to a public image through a page or partial?
It could be an image uploaded via a post, or another plugin, that I'd also like to be able to use in a specific page if needs be, rather than having to place it in my theme directory also. Don't see the point of having two of the same image if it's already been uploaded to the public folder.
I know that all system files (uploads) are stored in the system_files
table in the DB, would just like to know if there's an easy way to access them through TWIG, maybe something like {{ public('image_name.jpg') }}
?
Last updated
Try taking a look here: http://octobercms.com/docs/cms/markup#page-links
I think it should be something like this:
{{ '/url-to-app-img.jpg'|app }}
Last updated
@kuroski
Thanks for that. That would/does work when you know the full path to the uploaded image. The problem is though, if you upload an image to your blog post for example called image-1.jpg
, that image is then placed in the public folder within nested folders like public/539/96e/e84/53996ee84be35350687710.jpg
, plus it is renamed with a hash value, so you'd have to know the exact path to the image to display it. Can be kind of messy with all the folder structures & renaming.
The thing is that all the image data is stored within the DB system_files
table, along with the original file name, so my question is, how can you output an image from the public folder using the original filename (?), if that's even possible? I'd imagine it would require a DB call to the above table per image (maybe not be the best way), but is there a function or method in TWIG that anyone knows about that can do this? Are all uploaded images perhaps stored in a global images variable that can be accessed via TWIG?
Last updated
Hi, anybody else wanting a solution for linking to uploaded images, drop this into the code
section of your page:
function file( $file_name )
{
$systemFiles = System\Models\File;
foreach ( $systemFiles->all() as $row )
if ( $row->file_name === $file_name )
return $row;
}
then in your page you can use it like:
<img src="{{ file('image_name.jpg').path }}" alt="">
Please note that image_name.jpg
IS NOT the 53ab0e3f84f9d369471848.jpg
hash file in the uploads folder, it's the name of the original image you uploaded :)
Last updated
Hi, assuming you've set in the blog Post model:
public $attachMany = [
'featured_images' => ['System\Models\File']
];
You can access images like this:
{% for image in post.featured_images %}
<img src="{{ image.thumb(200, 200) }}" title="{{ image.title }}" alt="{{ image.description }}">
{% endfor %}
Here you have a thumbnail witch is 200px width by 200px height, if you want the full image (not the thumbnail), I think it is something like this:
{{ image.path }}
@Tiipiik
You can also use {{ post.featured_images.0.path }}
to access any Blog Post featured image (where 0
is the image index). If you know you only want the "first" image, you can just use {{ post.featured_images.first.path }}
.
You can also use {{ post.content_images.0.path }}
to retrieve any images in the Blog Post content (again, where 0
is the image index).
If you wanted to retrieve a Blog Post image by its name, you can use:
{{ post.content_images().whereFileName('image-1.jpg').first.path }}
or
{{ post.featured_images().whereFileName('image-1.jpg').first.path }}
Last updated
Yes of course, with a for loop to retrieve all image, and indexed request to pick up images one by one. Wans't knowing about the {{ post.featured_images.first.path }} method, more verbose than {{ post.featured_images.0.path }} thanks :) But I'm wure that retrieve a Blog post image by it's file name should be very useful, most of the time you don't known the name of this image, but again, thanks for the mehod ;)
img src="{{ file('image_name.jpg').path }}" alt=""> not working ..........getting error
The function "file" does not exist
Last updated
Hi, anybody else wanting a solution for linking to uploaded images, drop this into the CODE
section of your page:
function file( $file_name )
{
$systemFiles = System\Models\File;
foreach ( $systemFiles->all() as $row )
if ( $row->file_name === $file_name )
return $row;
}
then in your page you can use it like:
<img src="{{ file('image_name.jpg').path }}" alt="">
Please note that image_name.jpg
IS NOT the 53ab0e3f84f9d369471848.jpg
hash file in the uploads folder, it's the name of the original image you uploaded :)
Is there any solution to this for getting the full file paths in the backend?
I have
use System\Models\File as FileData;
in the controller with able to search the table, but only the filename and diskname is there, not the full path which is something like
/storage/app/uploads/protected/555/23d/f9d
I use the october list which has the fileupload widget and saves to directory and creates relationship with the
public $attachOne = [
'report_url' => ['System\Models\File', 'public' => false]
];
I want to create a function in the controller so I can attach said file to Mail and email on calling it. But it requires I pass it a file path I can't fetch.
Edit:
The following example solves the issue
$filePath = $job->report_url->getPath();
and reference the original model just before to grab the id
$job = Report::find(1);
Last updated
Daniel81 said:
@Tiipiik
You can also use
{{ post.featured_images.0.path }}
to access any Blog Post featured image (where0
is the image index). If you know you only want the "first" image, you can just use{{ post.featured_images.first.path }}
. You can also use{{ post.content_images.0.path }}
to retrieve any images in the Blog Post content (again, where0
is the image index).If you wanted to retrieve a Blog Post image by its name, you can use:
{{ post.content_images().whereFileName('image-1.jpg').first.path }}
or
{{ post.featured_images().whereFileName('image-1.jpg').first.path }}
(y) working
Daniel81 said:
Hi, anybody else wanting a solution for linking to uploaded images, drop this into the
code
section of your page:function file( $file_name ) { $systemFiles = System\Models\File; foreach ( $systemFiles->all() as $row ) if ( $row->file_name === $file_name ) return $row; }
then in your page you can use it like:
<img src="{{ file('image_name.jpg').path }}" alt="">
Please note that
image_name.jpg
IS NOT the53ab0e3f84f9d369471848.jpg
hash file in the uploads folder, it's the name of the original image you uploaded :)
Yes of course, with a for loop to retrieve all image, and indexed request to pick up images one by one. Wans't knowing about the {{ post.featured_images.first.path }} method, more verbose than {{ post.featured_images.0.path }} thanks :) But I'm wure that retrieve a Blog post image by it's file name should be very useful, most of the time you don't known the name of this image, but again, thanks for the mehod ;)
Last updated
File attachments are detailed here, including usage examples:
https://octobercms.com/docs/database/attachments#attachments-usage-example
1-16 of 16