Display images stored outside the public folder in zend framework - php

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="" />

Related

PHP pChart used with XMAPP - retrieve Stroke image from HTML

i am trying to create a chart in PHP through pChart 2.3 class.
My doubt is on this function:
function autoOutput(string $FileName = "output.png", int $Compression = 6, $Filters = PNG_NO_FILTER)
{
if (php_sapi_name() == "cli") {
$this->Render($FileName, $Compression, $Filters);
} else {
$this->Stroke(TRUE, $Compression, $Filters);
}
}
Html Code:
<div id="filtri">
<?php include($_SERVER['DOCUMENT_ROOT'] . "/reportingBug/generaGrafico1.php");?>
<?php include($_SERVER['DOCUMENT_ROOT'] . "/reportingBug/recuperaTempiMediLavBug.php");?>
<?php include($_SERVER['DOCUMENT_ROOT'] . "/reportingBug/recuperaTempiMediBug.php");?>
<img src="temp/example.drawBarChart.spacing.png">
</div>
Php Script:
/* Render the picture (choose the best way) */
$myPicture->autoOutput("temp/example.drawBarChart.spacing.png");
?>
If i force the code in order to follow the if branch with Render function everything is working right
because it creates a PNG image in root folder and i can retrieve it from HTML.
but it goes through the else branch, so the stroke function is called.
In the case, i have two problems:
i don't know how to retrieve the PNG from HTML
i have a problem because the browser shows following:
I tried to use a phisical image (PNG file create on the server folder) but i think it's not correct because many user will access the application concurrently.

How to display image from Storage(Laravel 5.2)

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'

Resizing an image before displaying it in the browser

I'm creating an API in which I have a model called OnlineAds(), it has these columns title, full_discription, short_discription and image, I also created an input form to fill in these fields and upload an image, in the image field I add the image's name and store the image in this path: storage/uploads. I was able to retrieve the image and display it, but I still didn't figure out how to resize it before displaying (lets say I want it to be 300 x 300), can anybody show me how ?
Note: I tried to use Stapler and Imagine but they seemed to give multiple bugs and failed!
This is my controller:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$filetype = File::type($path);
$response = Response::make(File::get($path), 200);
$response->header('Content-Type', $filetype);
return $response;
}
}
You could look into using Intervention to take care of the image processing as well as handling the response. It can be as easy as this:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$img = Image::make($path);
$img->resize(300, 300);
return $img->response();
}
}
There are detailed instructions on how to install the Intervention library and integrate it with Laravel, and it shouldn't take more than a few minutes to get it up and running.

Laravel: load images stored outside 'public' folder

I'm trying to display an image stored outside the 'public' folder in my view. These are simple profile images whose paths are stored in the DB. The path looks like
/Users/myuser/Documents/Sites/myapp/app/storage/tenants/user2/images/52d645738fb9d-128-Profile (Color) copy.jpg
Since the image is stored a DB column for each user, my first thought was to create an Accessor in the User model to return the image. I tried:
public function getProfileImage()
{
if(!empty($this->profile_image))
{
return readfile($this->profile_image);
}
return null;
}
That produced unreadable characters in the view. I also tried file_get_contents() in place of read file. Any suggestions about how this might be accomplished?
How about this (just tested it myself and it works):
The view:
<img src="/images/theImage.png">
Routes.php:
Route::get('images/{image}', function($image = null)
{
$path = storage_path().'/imageFolder/' . $image;
if (file_exists($path)) {
return Response::download($path);
}
});
Here is a slightly modified version of #Mattias answer. Assume the file is in the storage/app/avatars folder which is outside the web root.
<img src="/avatars/3">
Route::get('/avatars/{userId}', function($image = null)
{
$path = storage_path().'/app/avatars/' . $image.'.jpg';
if (file_exists($path)) {
return response()->file($path);
}
});
Probably needs and else. Also I have wrapped mine inside the middleware auth Route Group which means you have to be logged in to see (my requirements) but I could do with more control over when it is made visible, perhaps alter the middleware.
EDIT
Forgot to mention that this is for Laravel 5.3.
Here's what I came up with:
I'm trying to show the images in the view, not download. Here's what I came up with:
Note that these images are stored above the public folder, which is why we have to take extra steps to display the image in the view.
The view
{{ HTML::image($user->getProfileImage(), '', array('height' => '50px')) }}
The model
/**
* Get profile image
*
*
*
* #return string
*/
public function getProfileImage()
{
if(!empty($this->profile_image) && File::exists($this->profile_image))
{
$subdomain = subdomain();
// Get the filename from the full path
$filename = basename($this->profile_image);
return 'images/image.php?id='.$subdomain.'&imageid='.$filename;
}
return 'images/missing.png';
}
public/images/image.php
<?php
$tenantId = $_GET["id"];
$imageId = $_GET["imageid"];
$path = __DIR__.'/../../app/storage/tenants/' . $tenantId . '/images/profile/' . $imageId;
// Prepare content headers
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
$length = filesize($path);
header ("content-type: $mime");
header ("content-length: $length");
// #TODO: Cache images generated from this php file
readfile($path);
exit;
?>
If somebody has a better way, please enlighten us!! I'm very interested.

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