Edit and copy image in Laravel [ Can't write image data to path ]
I have created the following 4 model and its table structure for my project:
Media.php To upload images in app
Medias table structure
id | path
Example of path:uploads/images/media/food-1542110154.jpg
Post.php Create post
Posts table structure
id | title | content
FeaturedImage.php Featured image for post
Posts table structure
id | post_id| path
Post model and FeaturedImage model are in a one-to-one relationship
UploadImage.php To resize the uploaded image and move it to another directory. This model doesn't have migration and controller
Code snippet from PostsController.php
to create the post
use AppUploadImage;
use AppMedia;
class PostController extends Controller
{
private $imagePath= "uploads/images/post/";
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
$media = Media::find($request->featured);
if (!File::exists($this->imagePath)) {
File::makeDirectory($this->imagePath);
}
$upload = new UploadImage;
$image= $upload->uploadSingle($this->banner, $media->path, 400,300);
$post->image()->save(new FeaturedImage([
'path' => $image
]));
}
Session::flash('success', 'Post created sucessfully !');
return redirect()->route('post.index');
}
Code snippet from UploadImage.php
use InterventionImageFacadesImage;
use SpatieLaravelImageOptimizerFacadesImageOptimizer;
use IlluminateDatabaseEloquentModel;
class UploadImage extends Model
{
public function uploadSingle($savePath, $image,$width,$height)
{
Image::make(public_path($image))->fit($width, $height)->save($savePath);
ImageOptimizer::optimize($savePath);
return $savePath;
}
}
In my Laravel app, I'm trying to edit dimension of the already uploaded image with method written in UploadImage.php
and save edited image in post
directory and save its path in featured_images
table.
But I've been getting **Can't to write image data to path ** error.
I would be very thankful if anyone could point out the mistakes that I've made.
Please do not mark this as duplicate content. As I've been gone through almost all posts related to this kind of error and they have been no help to me.
php laravel laravel-5.7
add a comment |
I have created the following 4 model and its table structure for my project:
Media.php To upload images in app
Medias table structure
id | path
Example of path:uploads/images/media/food-1542110154.jpg
Post.php Create post
Posts table structure
id | title | content
FeaturedImage.php Featured image for post
Posts table structure
id | post_id| path
Post model and FeaturedImage model are in a one-to-one relationship
UploadImage.php To resize the uploaded image and move it to another directory. This model doesn't have migration and controller
Code snippet from PostsController.php
to create the post
use AppUploadImage;
use AppMedia;
class PostController extends Controller
{
private $imagePath= "uploads/images/post/";
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
$media = Media::find($request->featured);
if (!File::exists($this->imagePath)) {
File::makeDirectory($this->imagePath);
}
$upload = new UploadImage;
$image= $upload->uploadSingle($this->banner, $media->path, 400,300);
$post->image()->save(new FeaturedImage([
'path' => $image
]));
}
Session::flash('success', 'Post created sucessfully !');
return redirect()->route('post.index');
}
Code snippet from UploadImage.php
use InterventionImageFacadesImage;
use SpatieLaravelImageOptimizerFacadesImageOptimizer;
use IlluminateDatabaseEloquentModel;
class UploadImage extends Model
{
public function uploadSingle($savePath, $image,$width,$height)
{
Image::make(public_path($image))->fit($width, $height)->save($savePath);
ImageOptimizer::optimize($savePath);
return $savePath;
}
}
In my Laravel app, I'm trying to edit dimension of the already uploaded image with method written in UploadImage.php
and save edited image in post
directory and save its path in featured_images
table.
But I've been getting **Can't to write image data to path ** error.
I would be very thankful if anyone could point out the mistakes that I've made.
Please do not mark this as duplicate content. As I've been gone through almost all posts related to this kind of error and they have been no help to me.
php laravel laravel-5.7
forget about my previous comment, what is$media->path
? Shouldn't it be$this->imagePath
?
– Taha Paksu
Nov 14 '18 at 10:37
1
public_path to save the images ? usually we use storage_path ... public path is http's user writable ?
– simonecosci
Nov 14 '18 at 11:47
@TahaPaksu$media->path
is the path of original image that has been alreadey uploaded.
– Tanja Forsberg
Nov 16 '18 at 8:22
add a comment |
I have created the following 4 model and its table structure for my project:
Media.php To upload images in app
Medias table structure
id | path
Example of path:uploads/images/media/food-1542110154.jpg
Post.php Create post
Posts table structure
id | title | content
FeaturedImage.php Featured image for post
Posts table structure
id | post_id| path
Post model and FeaturedImage model are in a one-to-one relationship
UploadImage.php To resize the uploaded image and move it to another directory. This model doesn't have migration and controller
Code snippet from PostsController.php
to create the post
use AppUploadImage;
use AppMedia;
class PostController extends Controller
{
private $imagePath= "uploads/images/post/";
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
$media = Media::find($request->featured);
if (!File::exists($this->imagePath)) {
File::makeDirectory($this->imagePath);
}
$upload = new UploadImage;
$image= $upload->uploadSingle($this->banner, $media->path, 400,300);
$post->image()->save(new FeaturedImage([
'path' => $image
]));
}
Session::flash('success', 'Post created sucessfully !');
return redirect()->route('post.index');
}
Code snippet from UploadImage.php
use InterventionImageFacadesImage;
use SpatieLaravelImageOptimizerFacadesImageOptimizer;
use IlluminateDatabaseEloquentModel;
class UploadImage extends Model
{
public function uploadSingle($savePath, $image,$width,$height)
{
Image::make(public_path($image))->fit($width, $height)->save($savePath);
ImageOptimizer::optimize($savePath);
return $savePath;
}
}
In my Laravel app, I'm trying to edit dimension of the already uploaded image with method written in UploadImage.php
and save edited image in post
directory and save its path in featured_images
table.
But I've been getting **Can't to write image data to path ** error.
I would be very thankful if anyone could point out the mistakes that I've made.
Please do not mark this as duplicate content. As I've been gone through almost all posts related to this kind of error and they have been no help to me.
php laravel laravel-5.7
I have created the following 4 model and its table structure for my project:
Media.php To upload images in app
Medias table structure
id | path
Example of path:uploads/images/media/food-1542110154.jpg
Post.php Create post
Posts table structure
id | title | content
FeaturedImage.php Featured image for post
Posts table structure
id | post_id| path
Post model and FeaturedImage model are in a one-to-one relationship
UploadImage.php To resize the uploaded image and move it to another directory. This model doesn't have migration and controller
Code snippet from PostsController.php
to create the post
use AppUploadImage;
use AppMedia;
class PostController extends Controller
{
private $imagePath= "uploads/images/post/";
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
$media = Media::find($request->featured);
if (!File::exists($this->imagePath)) {
File::makeDirectory($this->imagePath);
}
$upload = new UploadImage;
$image= $upload->uploadSingle($this->banner, $media->path, 400,300);
$post->image()->save(new FeaturedImage([
'path' => $image
]));
}
Session::flash('success', 'Post created sucessfully !');
return redirect()->route('post.index');
}
Code snippet from UploadImage.php
use InterventionImageFacadesImage;
use SpatieLaravelImageOptimizerFacadesImageOptimizer;
use IlluminateDatabaseEloquentModel;
class UploadImage extends Model
{
public function uploadSingle($savePath, $image,$width,$height)
{
Image::make(public_path($image))->fit($width, $height)->save($savePath);
ImageOptimizer::optimize($savePath);
return $savePath;
}
}
In my Laravel app, I'm trying to edit dimension of the already uploaded image with method written in UploadImage.php
and save edited image in post
directory and save its path in featured_images
table.
But I've been getting **Can't to write image data to path ** error.
I would be very thankful if anyone could point out the mistakes that I've made.
Please do not mark this as duplicate content. As I've been gone through almost all posts related to this kind of error and they have been no help to me.
php laravel laravel-5.7
php laravel laravel-5.7
edited Nov 14 '18 at 10:28
Khalifa Nikzad
79413
79413
asked Nov 14 '18 at 10:13
Tanja ForsbergTanja Forsberg
351215
351215
forget about my previous comment, what is$media->path
? Shouldn't it be$this->imagePath
?
– Taha Paksu
Nov 14 '18 at 10:37
1
public_path to save the images ? usually we use storage_path ... public path is http's user writable ?
– simonecosci
Nov 14 '18 at 11:47
@TahaPaksu$media->path
is the path of original image that has been alreadey uploaded.
– Tanja Forsberg
Nov 16 '18 at 8:22
add a comment |
forget about my previous comment, what is$media->path
? Shouldn't it be$this->imagePath
?
– Taha Paksu
Nov 14 '18 at 10:37
1
public_path to save the images ? usually we use storage_path ... public path is http's user writable ?
– simonecosci
Nov 14 '18 at 11:47
@TahaPaksu$media->path
is the path of original image that has been alreadey uploaded.
– Tanja Forsberg
Nov 16 '18 at 8:22
forget about my previous comment, what is
$media->path
? Shouldn't it be $this->imagePath
?– Taha Paksu
Nov 14 '18 at 10:37
forget about my previous comment, what is
$media->path
? Shouldn't it be $this->imagePath
?– Taha Paksu
Nov 14 '18 at 10:37
1
1
public_path to save the images ? usually we use storage_path ... public path is http's user writable ?
– simonecosci
Nov 14 '18 at 11:47
public_path to save the images ? usually we use storage_path ... public path is http's user writable ?
– simonecosci
Nov 14 '18 at 11:47
@TahaPaksu
$media->path
is the path of original image that has been alreadey uploaded.– Tanja Forsberg
Nov 16 '18 at 8:22
@TahaPaksu
$media->path
is the path of original image that has been alreadey uploaded.– Tanja Forsberg
Nov 16 '18 at 8:22
add a comment |
1 Answer
1
active
oldest
votes
Tweaked few lines in my UploadImage.php model and got it solved.
public function uploadSingle($savePath, $image,$width,$height)
{
$filename = basename($image);
$location = $savePath . $filename;
Image::make($image)->save($location);
ImageOptimizer::optimize($location);
return $location;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297717%2fedit-and-copy-image-in-laravel-cant-write-image-data-to-path%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Tweaked few lines in my UploadImage.php model and got it solved.
public function uploadSingle($savePath, $image,$width,$height)
{
$filename = basename($image);
$location = $savePath . $filename;
Image::make($image)->save($location);
ImageOptimizer::optimize($location);
return $location;
}
add a comment |
Tweaked few lines in my UploadImage.php model and got it solved.
public function uploadSingle($savePath, $image,$width,$height)
{
$filename = basename($image);
$location = $savePath . $filename;
Image::make($image)->save($location);
ImageOptimizer::optimize($location);
return $location;
}
add a comment |
Tweaked few lines in my UploadImage.php model and got it solved.
public function uploadSingle($savePath, $image,$width,$height)
{
$filename = basename($image);
$location = $savePath . $filename;
Image::make($image)->save($location);
ImageOptimizer::optimize($location);
return $location;
}
Tweaked few lines in my UploadImage.php model and got it solved.
public function uploadSingle($savePath, $image,$width,$height)
{
$filename = basename($image);
$location = $savePath . $filename;
Image::make($image)->save($location);
ImageOptimizer::optimize($location);
return $location;
}
answered Nov 16 '18 at 8:50
Tanja ForsbergTanja Forsberg
351215
351215
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297717%2fedit-and-copy-image-in-laravel-cant-write-image-data-to-path%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
forget about my previous comment, what is
$media->path
? Shouldn't it be$this->imagePath
?– Taha Paksu
Nov 14 '18 at 10:37
1
public_path to save the images ? usually we use storage_path ... public path is http's user writable ?
– simonecosci
Nov 14 '18 at 11:47
@TahaPaksu
$media->path
is the path of original image that has been alreadey uploaded.– Tanja Forsberg
Nov 16 '18 at 8:22