Forum OpenACS Development: resize image in Xowiki form

Collapse
Posted by Alessandro Landim on
Hi,

I had trying to implement an option in File Field that resize image.

Yesterday, I created one parameter resize_width in file class and, in convert_to_internal, I implemented the resize image..

So, ::xowiki::formfield::file become it:

Class file -superclass FormField -parameter {
{resize_width 480}
{size 40}
link_label
}
file instproc tmpfile {value} {my set [self proc] $value}
file instproc content-type {value} {my set [self proc] $value}
file instproc initialize {} {
my type file
my set widget_type file(file)
next
}
file instproc entry_name {value} {
return [list name file:[my name] parent_id [[my object] item_id]]
}

file instproc convert_to_internal {} {
my instvar value

if {[my value] eq ""} {
# nothing to do, keep the old value
set value [[my object] form_parameter __old_value_[my name] ""]
[my object] set_property [my name] $value
return
}


regsub -all {\\+} $value {/} value ;# fix IE upload path
set value [::file tail $value]
[my object] set_property [my name] $value

set folder_id [[my object] set parent_id]
array set entry_info [my entry_name $value]

set content_type [my set content-type]
if {$content_type eq "application/octetstream"} {
set content_type [::xowiki::guesstype $value]
}
#my msg "mime_type of $entry_name = [::xowiki::guesstype $value] // [my set content-type] ==> $content_type"

if {[set id [::xo::db::CrClass lookup -name $entry_info(name) -parent_id $entry_info(parent_id)]]} {
# file entry exists already, create a new revision
set file_object [::xo::db::CrClass get_instance_from_db -item_id $id]
$file_object set import_file [my set tmpfile]
$file_object set mime_type $content_type
$file_object set title $value
$file_object save
} else {

if {[lindex [split $content_type "/"] 0] eq "image"} {
ImageMagick::convert -options "-resize [my set resize_width]" [my set tmpfile] [my set tmpfile]
}

# create a new file
set file_object [::xowiki::File new -destroy_on_cleanup \
-title $value \
-name $entry_info(name) \
-parent_id $entry_info(parent_id) \
-mime_type $content_type \
-package_id [[my object] package_id] \
-creation_user [::xo::cc user_id] ]

$file_object set import_file [my set tmpfile]
$file_object save_new
}
}

It works well, but i have doubts about the implemetation and I don't know where I cat implement this.

Anybody can help?

Collapse
Posted by Alessandro Landim on
Ops,

Change cat to can.

It works well, but i have doubts about the implemetation and I don't know where I can implement this.

Thanks.

Collapse
Posted by Gustaf Neumann on
Why do you want to resize form fields of type "file" and not form fields of type "image"? Is it really useful to resize every image to the same size (if you upload e.g. an icon). Are you aware of the fact, that you are only resizing when an initial file is uploaded, but not when a new revision of the file is added?

You can e.g. define a subclass of file/image and define the option there. Just make sure that your file is loaded after form-field-procs by choosing e.g. an appropriate name.

Collapse
Posted by Alessandro Landim on
Hi Gustaf,

Thank you for reply.

I know that the implementation is completely wrong, but I didn't know where I would implement this.

Now, I will return the form-field-procs to original and define other file to implement this option.