Show image from private storage to rest api client in laravel - php

I'm working on a laravel project and i have saved some pictures on a private storage folder .
This is config/filesystems.php :
'frontend' => [
'driver' => 'local',
'root' => storage_path('app/frontend'),
'url' => env('APP_URL') . '/storage/frontend',
],
I want to show images to client using rest api ( my client is a flutter application ) .
I have tried using some methods of File Storage in Laravel but it wont help .
This is a method that i have tried but when i use it's url it says to me Not Found:
Route::get('/show-pic', function () {
$pic = Picture::find(1);
$link = Storage::disk('frontend')->url($pic->avatar);
return response()->json($link , 200);
});
Also i dont want to get the content of pictures then return response containing content of picture ,
i just need url of picture .
Any suggestion will be helpful .
Thank you .

please try :
$link = Storage::disk('frontend')->getAdapter()->getPathPrefix();
or
$link = Storage::disk('frontend')->path($pic->avatar);

To solve this problem, you can use environment variables as follows:
APP_URL='your_app_url'
And then in your controller, you may access the url like this:
$imageUrl = $('APP_URL').'/uploads/'.$imageFileName;
this assumes that your image files are stored in storage/uploads

The easiest way to solve this issue :
Move your frontend folder inside storage\app\public
Then call
php artisan storage:link
And finally you may use the url method to get the URL for the given file
Storage::disk('public')->url('frontend/' . $pic->avatar);

Related

Requesting Image Assets from external Api using a proxy

I am trying to access images from an external API using guzzle in laravel, however, as understandably the calls are becoming really expensive and have a drastic effect on page loading time. I know that a proxy call would solve the issue, however, is there any way of achieving that in laravel where a certain endpoint can act as a proxy and can return an image. Thanks
I found a solution to my own problem and will post an answer. Mind you, there are better solutions i.e. configuring your server to handle such calls. However, in context of Laravel, here is what I did.
The data I had to work with
[
"orientation" => "portrait"
"fig_type" => "diagram"
"id" => "A_SDEF-05-2016-0003001"
"position" => "float"
"label" => "Figure 1."
"caption" => "some caption"
"link" => "/resource/id/A_SDEF-05-2016-0003001.tif"
]
Solution :
Step 1
Loop through the data using collections in Laravel as in its function;
// Pass the link as a get request parameter to laravel route
// i.e. localhost/proxy/image?link=resource/id/A_SDEF-05-2016-0003001.tif
return collect($images)->each(function ( $image )
{
if ($image['link']) {
$image['link'] = ltrim($image['link'],'/');
return $image['link'] ="link={$image['link']}";
}
return $image;
});
Step 2
Register an endpoint in routes file e.g.
Route::get('/proxy/image', 'ProxyController#request_image');
Step 3
The controller method i.e. request_image makes a Guzzle request and return the response as:
$link = request('link');
return \Response::stream(function() use($image){
echo $image;
},200, [$headers]);
Step 4
Have a static function or a regular function to output some dynamic html that you will build however in img src you would call ProxyController image method i.e.
$img = '<img src="'. action('ProxyController#request_image',$image['link']).'") >";
-Step 5
Output the html in blade
{!! Asset::image($image) !!} // Just output the built html
Hope it helps :D

how to create directory in cpanel with the help of cpanel api

I got the class object code which help to create directory in cpanel but the code require a library or a class . name is cpanel.php
I need this library or class , Can anyone help me in this ?
I just want to create directory or upload files in cpanel with the help of cpanel api .
here is the url of the cpanel api which help to check the create directory code.
(LiveAPI PHP Class)
Thanks in advance ,
Greater help will be really appreciable!
You need to follow below steps to make dir using cpanel API
require_once '../components/xmlapi.php';
$xmlapi = new \xmlapi($cpanel_domain);
$xmlapi->password_auth($username,$password);
$xmlapi->set_port(2083);
$result = $xmlapi->api1_query($password, 'CustInfo', 'getemail', array());
if(isset($result->error) && $result->error!=""){
// error log here
}
$api2args = array(
'path'=> 'public_html',
'name' => $newdir_name,
'permission' => '0755',
);
$result = $xmlapi->api2_query($cpanel_username, 'Fileman', 'mkdir', $api2args);
You can download xmlapi file required for this from here:- https://github.com/CpanelInc/xmlapi-php as its given over here also under developer resources part. https://documentation.cpanel.net/display/SDK/Software+Development+Kit+Home

Yii2 show image in backend from frontend - settting alias

In my project I used an image cropper widget. I setup the widget, that it save in frontend/web/upload. But in backend I save the images to frontend too.
This is working perfect. Then i want to show the image on the backend, if its exist. And i want to reach the frontend.
Thats why i want to set my own aliases in params-local.php file.
But I using vhosts to my webpages and I want to set Aliases to them.
In Yii2 documentation i found an article from aliases, but it wont help me. I mean i tried to use but it wont work.
I tried this:
return [
'aliases' => [
'#front' => 'http://front.mypage.dev',
'#back' => 'http://back.mypage.dev',
],
];
And I also tried this aswell:
Yii::setAlias('#front', 'http://front.mypage.dev');
Yii::setAlias('#back', 'http://back.mypage.dev');
But when i try to echo Yii::getAlias('#front'); it sais
Invalid Parameter – yii\base\InvalidParamException
Invalid path alias: #front
Maybe someone has a solution for this?
Thanks a lot.
Add in backend/config/params.php like:
return [
'front' => 'http://front.mypage.dev',
'back' => 'http://back.mypage.dev',
];
and use it from:
Yii::$app->params['front']
Yii::$app->params['back']
Let me know your thought.
Try this:
Yii::setAlias('#front', 'http://front.mypage.dev');
Yii::setAlias('#back', 'http://back.mypage.dev');
echo Yii::getAlias('#front');
echo Yii::getAlias('#back');
echo Yii::getAlias('#frontend/path/to/file');
echo Yii::getAlias('#backend/path/to/file');
Yii2 Playground
setting-aliases-in-yii2-within-the-app-config-file

Changing a PHP file Array

What I need to achieve is very simple logically, I use Laravel framework and need my users to be able to change few settings directly from a form rather than opening the settings file which look like this :
<?php
return array(
'driver' => 'gd'
);
So the question would be how to access a php array file based on key, an update the value ?
After getting settings with
$settings = require "settings.php";
You can populate the form and get changes submitted back. After you get ALL of the settings into $settings (not just the changed settings, all of 'em), you can write them back with
$php = "<?php return " . var_export($settings,true) . ";";
file_put_contents("settings.php", $php);

Moodle, upload and store files using moodle forms

I'm having some dificulties implementing a moodle upload mechanism using moodle forms. My goal is to let the user/administrator upload images, store them and access later in a block.
Currently, I have this in the form:
$mform->addElement('filemanager', 'attachments', 'Pic:', null, array('subdirs' => 0, 'maxfiles' => 1,'accepted_types' => '*' ));
and this to save the file:
if ($draftitemid = file_get_submitted_draft_itemid('attachments')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_assignment', 'attachments', 0, array('subdirs' => false, 'maxfiles' => 1));
}
and I try to access the file like this:
file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $context->id . '/mod_assignment/attachments')
I don't receive any errors but I can't access the file either. I'm using moodle 2.0.
Thanks in advance,
Take care
It sounds like you are looking to write a custom block, in which case you should be giving block_myblock as the component name instead of mod_assignment.
Each Moodle component which serves files needs to have its own function defined within lib.php to handle file requests. In your case that function wants to be called something like block_myblock_pluginfile().
A good example of this is block_html_pluginfile() which can be found in moodle/blocks/html/lib.php and does something very similar to what you are wanting.

Categories