Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Adapted from: https://codesalad.dev/blog/how-to-resize-an-image-in-10-lines-of-javascript-29 | ||
function resizeImage(imgEl, wantedWidth, callback) { | ||
const canvas = document.createElement("canvas") | ||
const ctx = canvas.getContext("2d") | ||
|
||
const aspect = imgEl.width / imgEl.height | ||
|
||
canvas.width = wantedWidth | ||
canvas.height = wantedWidth / aspect | ||
|
||
ctx.drawImage(imgEl, 0, 0, canvas.width, canvas.height) | ||
return canvas.toBlob(callback, "image/jpeg", 0.9) | ||
} | ||
|
||
export function convertResizeImageFiles(fileList, wantedWidth, callback) { | ||
Array.from(fileList).forEach(file => { | ||
let reader = new FileReader(); | ||
|
||
reader.addEventListener("load", () => { | ||
let imgEl = document.createElement("img") | ||
|
||
imgEl.addEventListener("load", () => { | ||
|
||
resizeImage(imgEl, wantedWidth, (blob) => { | ||
|
||
callback(blob) | ||
|
||
}) | ||
|
||
}) | ||
|
||
imgEl.src = reader.result | ||
}) | ||
|
||
reader.readAsDataURL(file) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { convertResizeImageFiles } from './image_utils' | ||
|
||
// This hook attaches to a custom input element to resize selected images. | ||
export default ResizeInput = { | ||
getUploadTarget() { return this.el.dataset.uploadTarget }, | ||
mounted() { | ||
this.el.addEventListener("change", (e) => { | ||
e.preventDefault() | ||
e.stopImmediatePropagation() | ||
|
||
if (this.el.files && this.el.files.length > 0) { | ||
convertResizeImageFiles(this.el.files, 300, (resizedImageBlob) => { | ||
// Enqueues the resized blob on the <.live_file_input />. | ||
this.upload(this.getUploadTarget(), [resizedImageBlob]) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule DropsWeb.UploadsLive.Resize do | ||
use DropsWeb, :live_view | ||
|
||
@impl Phoenix.LiveView | ||
def render(assigns) do | ||
~H""" | ||
<h1>Resize Example</h1> | ||
<p>This example illustrates how to resize an image before uploading it to the web server.</p> | ||
<!-- Users select files with this file input. --> | ||
<input type="file" id="resize-target" phx-hook="ResizeInput" data-upload-target="images" /> | ||
<form phx-change="change" phx-submit="submit"> | ||
<!-- The live file input is hidden because users will not interact with it directly. --> | ||
<.live_file_input upload={@uploads.images} style="display:none;" /> | ||
<button type="submit">Upload</button> | ||
</form> | ||
<section class="row upload-demo"> | ||
<section class="column"> | ||
<h3>Pending Uploads</h3> | ||
<div :for={entry <- @uploads.images.entries} class="upload-entry" id={"entry-#{entry.ref}"}> | ||
<% # render in-flight progress using the reactive `entry.progress` %> | ||
<DropsWeb.Uploads.progress entry={entry} /> | ||
<div class="upload-entry__details"> | ||
<% # <.live_img_preview> uses an internal hook to render a client-side image preview %> | ||
<.live_img_preview entry={entry} /> | ||
<% # review the handle_event("cancel-upload") callback in the LiveView %> | ||
<a | ||
href="#" | ||
phx-click="cancel-upload" | ||
phx-value-ref={entry.ref} | ||
class="upload-entry__cancel" | ||
> | ||
× | ||
</a> | ||
</div> | ||
<% # upload_errors/2 returns error atoms per upload entry %> | ||
<p :for={error <- upload_errors(@uploads.images, entry)} class="alert alert-danger"> | ||
<%= upload_error_to_string(error) %> | ||
</p> | ||
</div> | ||
</section> | ||
<section class="column"> | ||
<h3>Uploaded Images</h3> | ||
<DropsWeb.Uploads.figure_group paths={@uploaded_files} /> | ||
</section> | ||
</section> | ||
""" | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def handle_event("change", _, socket) do | ||
{:noreply, socket} | ||
end | ||
|
||
@impl true | ||
def handle_event("cancel-upload", %{"ref" => ref}, socket) do | ||
{:noreply, cancel_upload(socket, :images, ref)} | ||
end | ||
|
||
@impl true | ||
def handle_event("submit", _params, socket) do | ||
{:noreply, DropsWeb.Uploads.consume_prepend_entries(socket, :images, :uploaded_files)} | ||
end | ||
|
||
@impl Phoenix.LiveView | ||
def mount(_, _, socket) do | ||
{:ok, | ||
socket | ||
|> assign(:uploaded_files, []) | ||
|> allow_upload(:images, accept: ~w(image/*), max_entries: 25)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters