How to display image from Storage(Laravel 5.2) - php

I try to display images which are uploaded in my storage/app directory.I successfully can upload images in storage but when i try to retrieve it shows a broken image.
To retrieve, my function is :
public function getUserImage($filename)
{
$file = Storage::disk('local')->get($filename);
return new Response($file, 200);
}
My view:
<img src="{{route('image.show',['filename'=>'123.jpg'])}}"/>
When i try to display the image , it shows status 200 (Failed to load response data) but image is not being displayed.Just a broken image is shown.
I have no idea what i am missing .Could you give me any advice ?
Thanks in advance

If you want send the image to view
In your controller:
public function index()
{
// get the filename from somewhere
$filename = getFilename();
$file = Storage::disk('local')->get($filename);
return View::make('your_view')
->with('file', $file);
}
In your view
<img src="/path_where_is_the_file/{{$file}}" />
Hope it helps!

Send filename to view and Use src like this
src='/path_to_file/filename.jpg'

Related

tinymce - change the url path of the uploaded images

Im using laravel-tinymce-simple-imageupload so that is possible to have the tinymce plugin with image upload.
But Im not understanding where to change the url path of the uploaded images so that is used an absolute path url?
As you can see, the plugin use a controller :
https://github.com/petehouston/laravel-tinymce-simple-imageupload/blob/master/src/TinymceController.php
public function uploadImage(Request $request)
{
$image = $request->file('image');
$filename = 'image_'.time().'_'.$image->hashName();
$image = $image->move(public_path('img'), $filename);
return mce_back($filename);
}
So as explained here : https://github.com/petehouston/laravel-tinymce-simple-imageupload#some-notes
You just have to create a new Controller Action in your application, route it and call it as explained :
#include('mceImageUpload::upload_form', ['upload_url' =>'YOUR_URL_FOR_HANDLING_IMAGE_UPLOAD'])

How can I upload image to different project on the laravel?

I have two project or url
First url like this :
http://myshop.dev/
Second url like this :
http://backend.myshop.dev/
If the second url, I upload image it will save the image file here :
http://myshop.dev/img/image1.jpg
Both projects use the same database
My code to upload image on the second project like this :
public function __construct(...)
{
...
$this->path = './img';
}
...
public function store(...) {
...
$filename = Input::file('photo')->getClientOriginalName();
...
Input::file('photo')->move($this->path, $filename);
...
}
It works
If I upload image it will save the image in backend-myshop\public\img
But, I want to uploaded image not saved there. But in my first project
So, I want the image file saved here : myshop\public\img
How can I do it?
Hi after uploading copy them from backend-myshop\public\img to myshop\public\img you can use this storage copy method
public function store(...) {
...
$filename = Input::file('photo')->getClientOriginalName();
...
Input::file('photo')->move($this->path, $filename);
Storage::copy('backend-myshop/public/img/'.$filename, 'myshop/public/img/'.$filename);
...
}
hope this will solve your problem

How to Delete Images from Public/Images Folder in laravel 5 (URL Data)

how to delete images file from public/images folder in laravel 5 ??
i found some example from this site, but i know they are just using the file name in their record table, but i'm using something like URL e.g localhost/project/uploads/filename.jpg on my record table. so if i tried like this :
$image_path = $data->image; // the value is : localhost/project/image/filename.format
if(File::exists($image_path)) {
File::delete($image_path);
}
the file is not deleted
help pls, thanks
If you want to delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.
Commonly, Laravel 5 file is locate in public folder.
Example: your files are located in public/images
$image_path = "/images/filename.ext"; // Value is not URL but directory file path
if(File::exists($image_path)) {
File::delete($image_path);
}
If I can delete image from server by reference URL then Google is the first target :)
You can use the normal PHP delete file keyword ( #unlink )
$image_path = "the name of your image path here/".$request->Image;
if (file_exists($image_path)) {
#unlink($image_path);
}
This is what I do to delete an image:
public function SliderDelete(String $slider_id)
{
$slider = Slider::findOrFail($slider_id);
$image_path = public_path("\storage\images\sliders\\") .$slider->photo;
if(File::exists($image_path)) {
File::delete($image_path);
}
else{
$slider->delete();
//abort(404);
}
$slider->delete();
return response()->json(['success'=>'Slider deleted successfully!']);
}
in laravel 8 you do it like
// import Storage class
use Illuminate\Support\Facades\Storage;
Storage::disk('public')->delete('path-of-file');
you can use disk of your choice like
Storage::disk('s3')->delete('path-of-file');
$filename = public_path($fileloc);
if(File::exists($filename)) {
File::delete($filename);
}
call this function and pass two parameter
$filepath = path where your file exist
$filename = name of your file
public static function UnlinkImage($filepath,$fileName)
{
$old_image = $filepath.$fileName;
if (file_exists($old_image)) {
#unlink($old_image);
}
}
public function destroy($id)
{
$imagePath = YourModelName::select('image')->where('id', $id)->first();
$filePath = $imagePath->image;
if (file_exists($filePath)) {
unlink($filePath);
YourModelName::where('id', $id)->delete();
}else{
YourModelName::where('id', $id)->delete();
}
}
To be able to delete the image it should be in the following form :
"/uploads/img1.jpg" where uploads directory is in the public directory then use the following code:
$image_path = puplic_path("/uploads/img1.jpg")
if (file_exists($image_path)) {
File::delete($image_path);
}
Here is the correct and easy way .
if (file_exists(public_path() . '/storage/gallery/images/'. $item->image)) {
unlink(public_path() . '/storage/gallery/images/'. $item->image);
}

Display images stored outside the public folder in zend framework

I have my photo folder stored in the data folder in a Zend Framework application. when i try to read the image files from the data folder to display as a user profile photo in my view i cant get it display and it only show up when i disable my view and layout.
This is my sample code:
$appShortPath = $photPlugin->getPathToFileFolders();
$databaseFile = '2011/08/18/17c7025cd9f3436e4ad1c02635cc6754.jpg';
$appPath = $appShortPath . '/data/UploadPhotos/';
if(!isset($databaseFile))
{
return false;
}
$fullPath = $appPath . $databaseFile;
if(!file_exists($fullPath))
{
return false;
}
$this->_helper->viewRenderer->setNoRender(true);
$this->view->layout()->disableLayout();
$image = readfile($fullPath);
header('Content-Type: image/jpeg');
$this->getResponse()
->setHeader('Content-Type', 'image/jpeg');
$this->view->logo = $image;
I want to get the profile photos and display it in the user profile page
hope someone can help - thanks
you can use this, but has problem with some older versions of IE
http://en.wikipedia.org/wiki/Data_URI_scheme
Why are you trying to set a header as type image/jpeg?
Can you not just set $this->view->logo = /path/to/jpeg
Then in your view script
<img src="<?php echo $this->logo; ?>" alt="" />

PHP temporary image and CodeIgniter view

I have .jpg images in path (/home/folder1/folder2/) to deny public access to the images. I'm using CodeIgniter and following function to show single image.
function show_image()
{
$image_path = '/home/folder1/folder2/photo2.jpg';
$image = file_get_contents($image_path);
header("Content-Type: image/png");
echo $image;
}
I would like to have solution where I can pass the resized image to CodeIgniter view and show it using tag.
function show_image2()
{
// TODO image load and resize
// loaded and resized image
$data['image'] = $image;
$this->load->view('show_image', $data);
}
View:
<img src="<?php echo $image; ?>" />
Do I need to make temporary copy of image to public path?
EDIT:
How can I make temporary copy of image? Source folder is "/home/folder1/folder2" and destination folder is "example.com/images". Image should be deleted when user changes the image.
Yes you do, unless you're passing the function the raw image data (which means you'll need to change show_image() a bit :)

Categories