muuucho's avatar
Level 11

Upload heavy images files over 4G/5G from cameras in modern mobile phones

I have a project where a user can upload pdfs and images. Modern mobile phones take high resolution images, with file sizes up to 64 Mb. I need to downsize the file to 5Mb before storing on my server. Does anyone have experience and like to share their thoughts on how to deal with this?

Can I use jquery or such to convert the file on the mobile to something like max 2Mb before sending it to my server? Or are there other ways?

What is the max file size that is acceptable to upload over the 4G/5G nets? If it's acceptable to upload files of 64Mb, then I can use Imageick to resize the image and save it.

0 likes
1 reply
LaryAI's avatar
Level 58

To handle the downsizing of large image files before uploading them to your server, you can use client-side JavaScript libraries to resize the images. One popular library for this purpose is pica, which is known for its high-quality image resizing. You can use this library in combination with FileReader and Canvas to resize the image before uploading it.

Here's a step-by-step solution:

  1. Include the Pica library: You can include the Pica library in your project via a CDN or by installing it via npm.

  2. Read the image file: Use the FileReader API to read the image file selected by the user.

  3. Resize the image: Use the Pica library to resize the image to the desired dimensions and quality.

  4. Upload the resized image: Convert the resized image to a Blob and upload it to your server.

Here is a complete example using jQuery and Pica:

Explanation:

  1. File Input: The user selects an image file using an input element.
  2. FileReader: Reads the selected file and loads it into an image element.
  3. Canvas: Draws the image onto a canvas element and resizes it to the desired dimensions.
  4. Pica: Uses the Pica library to resize the image with high quality.
  5. Blob: Converts the resized image to a Blob and appends it to a FormData object.
  6. AJAX: Sends the FormData object to the server using an AJAX request.

This approach ensures that the image is resized on the client side before being uploaded, reducing the file size and making the upload process more efficient, especially over 4G/5G networks.

1 like

Please or to participate in this conversation.