i'm creating an API in Laravel 5.5 and this API supports receiving some files for profile pictures and such.
I want to limit this through a form request to accept only JPG and PNG images and i'm setting up my Form Request as follows:
'picture' => 'nullable|image|mimetypes:image/jpeg,image/png,image/jpg|max:2000' // 2000KB or around 2MB per picture
Then, on my PHPUnit/Codeception tests i'm sending a fake image using Laravel's own fake static method as follows
'picture' => UploadedFile::fake()->image("hello.svg")
If i DD the image file that is being sent when i run the test it correctly lists it's MIMEType as image/svg+xml, however, somehow this passes validation and comes out the other end and that should not be the case as my rule should limit only PNG or JPG files.
Does anyone else have this issue?
Make sure your form has an attribute enctype="multipart/form-data"
You can read about the enctype attribute here : What does enctype='multipart/form-data' mean?
you wrote wrong syntax to validate picture type in picture validation.
you need just to create picture validation like this :
'picture' => 'nullable|image|mimes:jpeg,png|max:2000'
Related
I'm currently making a web application that enables users to upload their files. Everything is working fine so far. I was asked to add a display icon on the side of the file that was uploaded.
How do I set an image to be displayed when a certain file type is uploaded?
Adding an accessor method in your file model like;
public function getIconAttribute() {
$extensions = [
'jpg' => 'jpeg.png',
'png' => 'png.png',
'pdf' => 'pdfdocument.png',
'doc' => 'wordicon.jpg',
];
return array_get($extensions,$this->extension,'unknown.png');
}
The array_get is a laravel helper that looks up a value in an array and can have a third parameter of a default if the extension is not found in the array.
Then in blade you can just icon}}' />
Or
A simpler approach where you just make sure to name the icon the same as the extension.
Then in blade you can just extension}}.png' /> and then you don't need to add the accessor at all.
It does not though allow for unknown file types but this might be ok if you are restricting the types of upload (which you should).
I will try to be as clear as possible to explain my problem. If not, tell me !
Mywebapp is based on Codeigniter. In it, I did a controller in order to show images uploaded by the users. By this way, the images can only be seen by members or authorized people and are not public like in facebook for example (image addresses are just hashed but are public).
I also use the mpdf library to generate a pdf from the content of a web page. To use it, I have in the third party directory the mpdf library. To be able to use it with my formatting, I put in the library directory (in application directory) my mpdf library (just to be able to use it from different controller). I have a pdf controller which is called from the view where I want to generate the pdf.
So the process is : I call a function of the pdf controller from my view. In this function, I call a function of my mpdf library which create a mpdf object (the third party class) and generate the pdf.
The generation of the pdf works great. The only I had is for the images. I explain. In my html document, the image are embedded like that :
<img src="http:localhost/www/myapp/library/image/hashed_name_of_the_image" alt="" />
The image function of the library controller retrieve the image and shows it. But in this function, I can protect or not the access to run it. For example, to force to be a logged user to see an image (if($this->session->userdata(‘logged’) == true)).
If there is no restriction, the generated pdf contains the images. Cool :).
But if I put a restriction, it doesn't. I understand that the mpdf object is the one which is asking to image function to show the image. So the verification failed.
I tried to use a config parameter on the CI instance object before I call the pdf function. For example, set to true a "pdf_conversion" and the conversion finished, set it back to false.
But it doesn't work. When I check the value in the image function, it's still false... It's like the config value is not change for all object but only for th pdf object which has changed it.
Is there a way to do what I want to do ??? With config parameter or anything else !!! I think I misunderstood something but don't know what !
thanks by advance for answers
Bastien
You problem is that then the library "calls" the image function, it is not logged in. So your controller code denies access.
There are two things you can do:
Move the code that generates/gets the image form the controller into a new library. then call that library from the controller and from the PDF library. This is the preferred way.
In the "image" action, find a way to check if the request is coming from the PDF library. Best guess is to look for something in the HTTP headers the PDF library sends. Anyway this is not very secure since HTTP headers can be "faked".
OFF TOPIC: Is there a reason you server the image from a controller action instead of by directly referencing the image file?
CLARIFICATION ON SOLUTION 1
So you'll have a library image.php that looks like this
class Image {
...
function get_image($hash){
[code for getting the image here]
}
...
}
In the controller:
function image($hash) {
$this->load->library('image');
if (if($this->session->userdata(‘logged’) == true)) {
$image = $this->image->get_image($hash);
[send it to the browser]
}
}
In the PDF library
function export($hash) {
$ci =& get_instace();
$ci->load->library('image');
[do pdf stuff]
$image = $ci->image->get_image($hash);
$this->add_image($image);
[do more pdf stuff]
}
This is the idea, the actual details of the implementation are up to you.
I need to upload the photo of a user with his details from asp.net mvc3 razor view. The image selected by the user has to be shown as thumbnail before submitting the form.
The model of the view contains a byte array property called Photo. On page load i am converting this byte array to base 64 string and showing it in . this is working properly .
Now i need to show the thumbnail of the image selected by the user. And when he clicks on submit button i need to bind the selected image to the model property Photo.
After googling , i came to know that showing thumbnail is not possible until I upload that image. I tried Uploadify but its UI behavior is not what i am expecting. I also tried the article http://www.dustinhorne.com/post/2011/11/16/AJAX-File-Uploads-with-jQuery-and-MVC-3.aspx, but it is also not suitable in our scenario.
can anyone help me by sharing their experience achieving this scenario.
Thanks in advance.
You could achieve this using HTML5 File API. Take a look at the following article and more specifically the Showing thumbnails of user-selected images section which illustrates an example of how you could achieve that without uploading the image to the server.
And if you want to support legacy browsers that do not yet support the HTML5 File API you could use the jQuery.form plugin which allows you to easily send the contents of a given form to the server using AJAX and it also supports file uploads. So basically you could subscribe to the .change() event of the file input or the .click() event of some see thumbnail ... button and then submit the form to a controller action using AJAX:
$('#myform').ajaxSubmit({
url: '#Url.Action("thumbnail")',
success: function(result) {
// the result variable will contain the result of
// the execution of the Thumbnail action.
// could be a BASE64 encoded representation of
// the thumbnail you generated on the server and then
// simply set it to the src property of your preview `<img>`
// element using the Data Uri scheme
}
});
consider following url:
http://sitename.com/School/Admin/PageViewer
i have a subfolder in School named UserFiles that contains images that users uploaded.
in PageViewer, i used TinyMCE to add HTML Contents. when i insert an image from UserFiles folder, in src attribute of img tag it takes this address: ../UserFiles/imageName.jpg
the real problem is when i request for the url: http://sitename.com/School
this url has an optional parameter called PageId which can have null values. if no value supplied for this parameter, it will open a default page that made by PageViewer.
my problem is images get broken when i request the above url. but if i send the request like h--p://sitename.com/School/Page/1 the images display correctly.
the url of broken images is like: h--p://sitename.com/UserFiles/imageName.jpg
but it should be: h--p://sitename.com/School/UserFiles/imageName.jpg
how can i solve this problem?
thanks in advance.
The easier way is to tell TinyMCE to render full URL instead of relative URL. That way no matter where you show your content, images are not broken.
Put this in your configuration :
relative_urls : false,
remove_script_host : false,
convert_urls : false,
It should be in the javascript which initializes the TinyMCE editor.
change this ../UserFiles/imageName.jpg to /UserFiles/imageName.jpg
This is similar to the problem posted here, but that solution doesn't work for me. Maybe it's because I'm not passing the data in correctly.
I'm pulling screenshots from Flash and displaying them on the page using Jquery:
$SNAPSHOT_PREVIEW.attr("src","data:image/jpg;base64," + imgData);
$HIDDEN_BASE64_STRING.val(imgData);
I had it nice and working where you could save the image to Rails in Flash, but Flash won't allow you invoke a post action without the user pressing a button for security reasons. Makes sense. Anyway, now I can't get Paperclip to save the image coming from the HTML form:
#(photo has_attached_file:image)
#photo = params[:photo]
data = StringIO.new(Base64.decode64(params[:base64_string]))
data.class.class_eval { attr_accessor :original_filename, :content_type }
data.original_filename = "screenshots.jpg"
data.content_type = "image/jpg"
#photo.image = data
Yields the error:
NoMethodError (undefined method `image=' for #<ActiveSupport::HashWithIndifferentAccess:0x8c2e420>):
How do I need to finesse the base64 image data into a paperclip attachment?
For bonus points, do I need the hidden field to pass the data or is there a clever, browser compatible way to use the image src as a form value?
You have the code:
#photo = params[:photo]
params is just a hash, so later, when you call #photo.image, Rails bugs out. Perhaps you want:
#photo = Photo.new(params[:photo])
instead?