refactor: Toggle optimization using checkbox

This commit is contained in:
MitulDavid 2021-08-06 18:31:41 +05:30
parent 313bde9ceb
commit 2d887187d2
3 changed files with 14 additions and 11 deletions

View file

@ -702,7 +702,4 @@
<symbol xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" id="icon-crop">
<path d="M14.88,11.63H4.33V1.12m7.34,10.51v3.25M6,4.37h5.64V10M1.13,4.37h3.2" stroke-linecap="round" stroke-linejoin="round"/>
</symbol>
<symbol xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" id="icon-optimized">
<path d="M4.88,11.91,7.63,9.15m-2.39,0H7.63v2.4m3.49-5.9L8.37,8.41m2.39,0H8.37V6m.94-4.58H3.63A1.31,1.31,0,0,0,2.32,2.75v10.5a1.31,1.31,0,0,0,1.31,1.31h8.74a1.31,1.31,0,0,0,1.31-1.31V5.81Z" stroke-linecap="round" stroke-linejoin="round"/>
</symbol>
</svg>

Before

Width:  |  Height:  |  Size: 92 KiB

After

Width:  |  Height:  |  Size: 92 KiB

View file

@ -20,9 +20,6 @@
<button class="ml-2 btn-reset" @click="$emit('toggle_private')" :title="__('Toggle Public/Private')">
<div v-html="private_icon"></div>
</button>
<button class="ml-2 btn-reset" v-if="is_optimizable" @click="$emit('toggle_optimize')" :title="__('Toggle optimization on/off')">
<div v-html="optimize_icon"></div>
</button>
</span>
</div>
@ -31,6 +28,7 @@
{{ file.file_obj.size | file_size }}
</span>
</div>
<label v-if="is_optimizable" class="optimize-checkbox"><input type="checkbox" @change="$emit('toggle_optimize')">Optimize</label>
</div>
<div class="file-actions">
<ProgressRing
@ -86,9 +84,6 @@ export default {
private_icon() {
return frappe.utils.icon(this.is_private ? 'lock' : 'unlock');
},
optimize_icon() {
return frappe.utils.icon(this.file.optimize ? 'optimized' : 'image');
},
is_private() {
return this.file.doc ? this.file.doc.is_private : this.file.private;
},
@ -204,4 +199,12 @@ export default {
.muted:hover {
opacity: 1;
}
.optimize-checkbox {
font-size: var(--text-sm);
color: var(--text-light);
display: flex;
align-items: center;
padding-top: 0.25rem;
}
</style>

View file

@ -40,6 +40,9 @@ def strip_exif_data(content, content_type):
return content
def optimize_image(content, content_type, max_width=1920, max_height=1080, optimize=True, quality=85):
if content_type == 'image/svg+xml':
return content
image = Image.open(io.BytesIO(content))
image_format = content_type.split('/')[1]
size = max_width, max_height
@ -48,5 +51,5 @@ def optimize_image(content, content_type, max_width=1920, max_height=1080, optim
output = io.BytesIO()
image.save(output, format=image_format, optimize=optimize, quality=quality, save_all=True if image_format=='gif' else None)
content = output.getvalue()
return content
optimized_content = output.getvalue()
return optimized_content if len(optimized_content) < len(content) else content