Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JS to read image dimensions on upload #2705

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions frontend/js/components/media-library/Uploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -197,46 +197,53 @@
this.uploadProgress(0)
},
_onSubmitCallback (id, name) {
this.$emit('clear')
// each upload session will add upload files with original filenames in a folder named using a uuid
this.unique_folder_name = this.unique_folder_name || (this.uploaderConfig.endpointRoot + qq.getUniqueId())
this._uploader.methods.setParams({
unique_folder_name: this.unique_folder_name,
media_to_replace_id: this.media_to_replace_id
}, id)
return new Promise((resolve, reject) => {
// halt fine uploader upload until image is loaded
// so we can send image dimensions with the upload
const img = new Image()
img.onload = () => {
this.$emit('clear')
// each upload session will add upload files with original filenames in a folder named using a uuid
this.unique_folder_name = this.unique_folder_name || (this.uploaderConfig.endpointRoot + qq.getUniqueId())

// determine the image dimensions and add it to params sent on upload success
const imageUrl = URL.createObjectURL(this._uploader.methods.getFile(id))
const img = new Image()
// determine the image dimensions and add it to params sent on upload success
this._uploader.methods.setParams({
width: img.width,
height: img.height,
unique_folder_name: this.unique_folder_name,
media_to_replace_id: this.media_to_replace_id
}, id)

img.onload = () => {
this._uploader.methods.setParams({
width: img.width,
height: img.height,
unique_folder_name: this.unique_folder_name,
media_to_replace_id: this.media_to_replace_id
}, id)
this.media_to_replace_id = null
}
this.media_to_replace_id = null

img.src = imageUrl
const media = {
id: this._uploader.methods.getUuid(id),
name: sanitizeFilename(name),
progress: 0,
error: false,
errorMessage: null,
isReplacement: !!this.media_to_replace_id,
replacementId: this.media_to_replace_id
}

const media = {
id: this._uploader.methods.getUuid(id),
name: sanitizeFilename(name),
progress: 0,
error: false,
errorMessage: null,
isReplacement: !!this.media_to_replace_id,
replacementId: this.media_to_replace_id
}
if (this.type.value === 'file') {
this.media_to_replace_id = null
}

if (this.type.value === 'file') {
this.media_to_replace_id = null
}
this.loadingMedias.push(media)
this.loadingProgress(media)

// resolve the promise, allow the upload to continue
resolve(img)
}

img.onerror = (err) => {
console.error(err) // eslint-disable-line
reject(err);
}

this.loadingMedias.push(media)
this.loadingProgress(media)
img.src = URL.createObjectURL(this._uploader.methods.getFile(id))
});
},
_onProgressCallback (id, name, uploadedBytes, totalBytes) {
const index = this.loadingMedias.findIndex((m) => m.id === this._uploader.methods.getUuid(id))
Expand Down
5 changes: 5 additions & 0 deletions src/Http/Controllers/Admin/MediaLibraryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ public function storeFile($request)

[$w, $h] = getimagesize($uploadedFile->path());
Copy link
Member

@ifox ifox Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@13twelve let's move this into an else below to avoid the extra call if we don't need it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


if ($request->input('width') && $request->input('height')) {
$w = $request->input('width');
$h = $request->input('height');
}

$uploadedFile->storeAs($fileDirectory, $filename, $disk);

$fields = [
Expand Down
Loading