I am trying to convert my images into greyscale and then downloading it in Laravel but each and every time I am getting this error
The file "" does not exist
Dont' know why it giving this error here is my code.
$file = public_path() . "/large/s/" . $sheet[0]->sheet_f_id . '-s.jpg';
$image = Image::make($file);
$grayScale = $image->greyscale();
return Response::download($grayScale);
When I dump my $file variable I got the response something like this.
"D:\xampp\htdocs\wikistaging\public/large/s/03-02-05-025-s.jpg"
But still it is giving me the sam error why is that happening. Any help would be great.
If you want to download then first you have to save the created file and need to give the path to download. Here is working example
$img_name=$sheet[0]->sheet_f_id . '-s.jpg';
$destination_path=public_path() . "/large/s/";
$file = $destination_path.$img_name;
$image = Image::make($file);
$image->greyscale()->save($destination_path.'gray-'.$img_name);
return Response::download($destination_path.'gray_'.$img_name);
And if you don't want to keep the file you can delete, replace the last line with below line.
return Response::download($destination_path.'gray_'.$img_name)->deleteFileAfterSend(true);
Hope it will work for you.
You must save your image after call greyscale().
You can try it:
$filePath = public_path() . "/large/s/" . $sheet[0]->sheet_f_id . '-s-test.jpg';
$image->greyscale();
$image->save($filePath);
Related
my problem is when I save image from line bot messenger they see each event and then work so I count number of file in directory(where I save image) and I want image name as
1.jpg
2.jpg
3.jpg
if($typeMessage=='image'){
$responseMedia = $bot->getMessageContent($idMessage);
$dataBinary = $responseMedia->getRawBody();
$files = scandir($botDataUserFolder);
$num_files = count($files)-2;
$filenamesave = $num_files.'.'.jpg;
file_put_contents($fileFullSavePath,$dataBinary);
}
please let me know what wrong with this code? I still save image as 0.jpg or 1.jpg and they overwrite on the old file
Problem with you code that you make count($files)-2;
But in php function "scandir" you will always receive back additionally dots directories(current dir and parent dir).
By first call count($files)-2 will return 0 and you system will create 0.jpg
Try this code, it will automatically skip dots using FilesystemIterator::SKIP_DOTS:
$save_path = "/tmp/dir_to_save";
$filesystem_iterator = new FilesystemIterator($save_path,
FilesystemIterator::SKIP_DOTS);
$number_of_files = iterator_count($filesystem_iterator);
$filename_save = ($number_of_files + 1) . '.jpg';
file_put_contents($save_path . DIRECTORY_SEPARATOR . $filename_save, "binary file data");
You will need to adapt it a little bit for you method, but it works correctly.
I have a <form> with a multiple file input that stores those files into the server. The names of the files must follow a numeric standard and that's why I rename them before storing. Each post has an ID an can have more than one file so the pattern is: post-id + dash and a number + file extension, this way, if the same post has two file with the same extensions, the dash + number will avoid them to be overwriten. So, before I store the files I run a loop to find the proper number to the name. The problem is, the function to verify the existence of the file seems not to be returning true when it should. The code:
$counter = 0;
do{
$nomeArquivo = $post->id . "-{$counter}" . "." . $arq->extension();
$counter++;
//these commented are other ways of verification I tried
//}while(Storage::exists($nomeArquivo));
//}while(file_exists("/storage/" . $nomeArquivo));
}while(is_file("/storage/" . $post->id . "-{$counter}"));
Storage::putFileAs('/public', $arq, $nomeArquivo);
This code above runs inside a foreach($files as $arq) where $files are the files from the form input.
Reference: https://laravel.com/docs/5.4/filesystem and https://laravel.com/api/5.4/Illuminate/Filesystem/Filesystem.html
Use the File Facade to check for the existence of a file:
File::exists($myfile)
http://laravel-recipes.com/recipes/123/determining-if-a-file-exists
If name of the file need to be unique only then,
You can give Unique Name like this:
$file = $request->file('file');
$extension = $file->getClientOriginalExtension();
$destination ='/files/';
$filename = uniqid() . '.' . $extension;
$file->move($destination, $filename);
Now save above file name in your database. Hope it may help you
As user #fubar pointed in the comments, I was referencing the wrong path to verify the existence of the file. I change the while condition to while(\File::exists(public_path() . '/storage/' . $nomeArquivo)) and it worked well. Thanks
i am trying to upload remote file using below code for testing purpose and to my surprise only last images are being uploaded.. i changed the sequence of images and it was always last image which was uploading rest are failing. inbrowser console i can see below error.
Warning: copy(http://example.com/your-heart.jpg
): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in /home/example/minimum.php on line 205
it is coming from form with text field name is urls
here is my php
$image_links = explode("\n", $_POST['urls']);
$i = 1;
foreach($image_links as $image_link){
$file_info = pathinfo($image_link);
copy($image_link, '/home/xxxxx/00' . $i . '_original.' . $file_info['extension']);
$i++;
}
any help will be great.
i tried so many different things for copy parameters like single quote, double quote and several other things but always same output. only last image upload and it says all other images are not found. i even tried to replace http with www . my sample urls are
http://example.com/color-your-heart.jpg
output of var dump
array(2) {
[0]=>
string(61) "http://example.com/thumb/color-your-heart.jpg
"
[1]=>
string(64) "http://example.com/thumb/mobile-price-real-time-calculator.png"
}
It looks like there's a space after the filename in the error message. Try removing spaces with trim()
copy(trim($image_link), '/home/xxxxx/00' . $i . '_original.' . $file_info['extension']);
I always use like below.
$image_data = file_get_contents($image_url);
$fileext = 'jpg'; // ext
$thenew_name = 'new random name or something';
$file = $upload_dir . '/' . $thenew_name . '.' . $fileext;
file_put_contents($file, $image_data);
Then use $file with additional relative directory path to save the image for you, like replace $upload_dir with your path variable. You can use into your foreach loop also. Also remove all the extra pert from image url. Most of expert said you in comments.
I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that
Storage::disk('s3')->get(imageUrlonS3);
retrieve the image in binary and is giving me errors. This is my code:
function createSlices($imagePath) {
//create transform driver object
$im = imagecreatefromjpeg($imagePath);
$sizeArray = getimagesize($imagePath);
//Set the Image dimensions
$imageWidth = $sizeArray[0];
$imageHeight = $sizeArray[1];
//See how many zoom levels are required for the width and height
$widthLog = ceil(log($imageWidth/256,2));
$heightLog = ceil(log($imageHeight/256,2));
//more code here to slice the image
.
.
.
.
}
// ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg
$content = Storage::disk('s3')->get(imageUrlonS3);
createSlices($content);
What am I missing here ?
Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself.
If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have.
Here's the relevant documentation:
createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php
getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php
Resulting code might look something like this:
function createSlices($imageData) {
$im = imagecreatefromstring($imageData);
$sizeArray = getimagesizefromstring($imageData);
//Everything else can probably be the same
.
.
.
.
}
$contents = Storage::disk('s3')->get($imageUrlOnS3);
createSlices($contents);
Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.
Basically I could upload files based on a project. Whenever I create a project, a new directory is created with the directory name as the project_name e.g. this is a test -> this-is-a-test. But my problem is I couldn't delete a file in a directory.
function delete_image($id)
{
$this->load->model(array('work_model', 'project_model'));
$result = $this->work_model->get_work($id);
$result = $this->project_model->get_project($result->project_id);
$dir = str_replace(" ", "-", $result->project_name);
$result = $this->work_model->delete($id);
if (isset($result)){
unlink('./uploads/' . $dir . '/' . $result->full_path);
}
redirect('admin/project/view_project/' . $result->project_id);
}
Need help on this thanks.
Well the error message is self-explanatory.
$result is not an object.
Your problem is matter of debugging, not SO question. And matter of reading error messages, of course. Why do you ask here what's going wrong if you already have explanation from your PHP? And, on the other hand, noone here know your code and have no idea what type of variable get_work($id) supposed to return.
This line is brilliant:
if (isset($result)){
Of course it is set, you have set it 3 times!
Use different variable names for each result you return. Why not use:
function delete_image($id)
{
$this->load->model(array('work_model', 'project_model'));
$work = $this->work_model->get_work($id);
$project = $this->project_model->get_project($work->project_id);
$dir = str_replace(" ", "-", $project->project_name);
if ($this->work_model->delete($id))
{
unlink('./uploads/' . $dir . '/' . $project->full_path);
}
redirect('admin/project/view_project/' . $project->project_id);
}
If that doesn't work, try some debug steps.
var_dump($this->work_model->delete($id));
That will tell you TRUE/FALSE, I'd assume right now it is FALSE which is why unlink isn't erroring or succeeding.
Debug is the way forward. We can't do it for you!