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 :)
Related
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
I am trying to resize and replace an image uploaded by the user but the most I can do is resize and output as another file. I have used the library image magician to resize.
If someone can explain how I can do it without using the library it would be better.
public function add() {
$f=Base::instance()->get('FILES');
$fext=pathinfo ($f['usrimg']['name'],PATHINFO_EXTENSION);
$this->copyFrom('POST');
$this->save();
$newName=str_pad($this->_id,5,"0").'.'.$fext;
move_uploaded_file($f['usrimg']['tmp_name'], $newName);
$this->load('id='.$this->_id);
$this->set('photo',$newName);
$this->update();
require_once('php_image_magician.php');
$magicianObj = new imageLib($newName);
$magicianObj->resizeImage(100, 200);
$magicianObj->saveImage('q.jpg', 100);
}
If you want to use gd library, for example you can use this code for resizing jpeg images:
$image=file_get_contents('image.jpg');
$src=imagecreatefromstring($image);
$x=imagesx($src);
$y=imagesy($src);
$y1=100; //new width
$x1=intval($y1*$x/$y); //new height proprtional to new width
$dst=imagecreatetruecolor($x1,$y1);
imagecopyresized($dst,$src,0,0,0,0,$x1,$y1,$x,$y);
header("Content-Type: image/jpeg");
imagejpeg($dst);
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'
I'm trying to figure out the correct use of the Header(content-type:image/png) .I have an upload script that uploads and resizes correctly,with or without the header, but it seems like its recommended to have the header .Problem is that with the header, once the script completes, it throws a 'broken img' icon in the top left corner of the window and exits the script. How do I make it behave differently like if I want to redirect to a new page. Adding Header(location:...) at the end of the script doesn't seem to make a difference. ANY Help is appreciated.
<?PHP
/*image resize with Imagick*/
function imageResize($image){
/*call to imagick class and resize functions will go here*/
echo 'the image to be resized is : '.$image;
$newImage=new Imagick();
$newImage->readImage($image);
$newImage->resizeImage(1024,768,imagick::FILTER_LANCZOS, 1);
$newImage->writeImage('myImage.png');
$newImage->clear();
$newImage->destroy();
}
/*image resize with GD image functions*/
function imgResize($image){
header('Content-Type: image/png');
$newWidth='1024';
$newHeight='768';
$size=getimagesize($image);
$width=$size[0];
$height=$size[1];
//return $width;
$dest=imagecreatetruecolor($newWidth,$newHeight);
$source=imagecreatefrompng($image);
imagecopyresized($dest,$source,0,0,0,0,$newWidth,$newHeight,$width,$height );
return imagepng($dest,'./users/uploads/newimage.png');
imagedestroy($dest);
}
/*Function that actually does the upload*/
function file_upload(){
if(!empty( $_FILES) ){
print_r($_FILES);
echo '<hr>';
$tmpFldr=$_FILES['upFile']['tmp_name'];
$fileDest='./users/uploads/'.$_FILES['upFile']['name'];
if(move_uploaded_file($tmpFldr,$fileDest)){
echo 'Congratulations, your folder was uploaded successfully <br><br>';
}
else{
echo 'Your file failed to upload<br><br>';
}
return $fileDest;
} else{die( 'Nothing to upload');}
} /*End file upload function */
$fileLocation=file_upload();
echo 'location of the new file is : '.$fileLocation.'<hr>';
$newImage=imgResize($fileLocation);
?>
You can't serve an image and serve a redirect at the same time. If you want to output a PNG image then you output the Content-Type header and that's exclusive of the Location header.
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="" />