| 1 | package HTML::TurboForm::Element::Image; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use base qw(HTML::TurboForm::Element); | ||
| 5 | use Imager; | ||
| 6 | __PACKAGE__->mk_accessors( qw/ prev upload keeporiginal width height savedir thumbnail loadurl caption maxsize errormessage / ); | ||
| 7 | |||
| 8 | sub new{ | ||
| 9 | my ($class, $request, $upload) = @_; | ||
| 10 | my $self = $class->SUPER::new( $request ); | ||
| 11 | $self->upload( $upload ); | ||
| 12 | $self->do_img(); | ||
| 13 | return $self; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub do_img{ | ||
| 17 | my ($self)=@_; | ||
| 18 | my $request=$self->request; | ||
| 19 | my $pic=''; | ||
| 20 | $pic = $self->request->{$self->name} if ($self->request->{$self->name} ); | ||
| 21 | |||
| 22 | if ($request->{ $self->name.'_upload' } && $request->{$self->name.'_submit'} ) { | ||
| 23 | if( $self->upload->type !~ /^image\/(jpeg|jpg|gif|png|pjpeg)$/ ) { | ||
| 24 | #$c->stash->{ 'error' } = 'Filetype not supported!'; | ||
| 25 | } else { | ||
| 26 | # read image | ||
| 27 | my $image = Imager->new; | ||
| 28 | $self->{sizeerror}=0; | ||
| 29 | if ($self->maxsize) { | ||
| 30 | if (($self->upload->size/1024) > $self->maxsize){ | ||
| 31 | $self->{sizeerror}=1; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | if (!$self->{sizeerror}){ | ||
| 35 | if( $image->read( file => $self->upload->tempname ) ) { | ||
| 36 | # remove alpha channels because jpg does not support it # and its not used anyways | ||
| 37 | $image = $image->convert( preset => 'noalpha' ); | ||
| 38 | #attribute keeporignal isparams local path for storing orig sized images | ||
| 39 | |||
| 40 | my $tmp = File::Temp->new( DIR => $self->savedir.'', UNLINK => 0, SUFFIX => '.jpg' ); | ||
| 41 | $pic = substr( $tmp, length( $self->savedir )+1 ); | ||
| 42 | $self->{pic}=$pic; | ||
| 43 | |||
| 44 | if ($self->keeporiginal){ | ||
| 45 | $self->upload->copy_to($self->keeporiginal.'/orig_'.$pic); | ||
| 46 | } | ||
| 47 | |||
| 48 | # if there is a save dir, resize. depending if width and/or height is given, scale to dimensions | ||
| 49 | if ($self->savedir){ | ||
| 50 | if (($self->width) and ($self->height)) { | ||
| 51 | # No scale. Resize to given dimensions | ||
| 52 | $image = $image->scaleX(pixels=>$self->width)->scaleY(pixels=>$self->height); | ||
| 53 | } elsif ($self->width) { | ||
| 54 | # Resize width, scale height | ||
| 55 | $image = $image->scale(xpixels=>$self->width); | ||
| 56 | } elsif ($self->height) { | ||
| 57 | # Resize height, scale width | ||
| 58 | $image = $image->scale(ypixels=>$self->height); | ||
| 59 | } | ||
| 60 | |||
| 61 | $image->write( | ||
| 62 | file => $self->savedir.'/med_'.$pic, | ||
| 63 | type => 'jpeg', | ||
| 64 | jpegquality => 90 | ||
| 65 | ); | ||
| 66 | |||
| 67 | if ($self->thumbnail) { | ||
| 68 | if ($self->thumbnail->{width} || $self->thumbnail->{height} ) { | ||
| 69 | if (($self->thumbnail->{width}) and ($self->thumbnail->{height})) { | ||
| 70 | # No scale. Resize to given dimensions | ||
| 71 | $image = $image->scaleX(pixels=>$self->thumbnail->{width})->scaleY(pixels=>$self->thumbnail->{height}); | ||
| 72 | } elsif ($self->thumbnail->{width}) { | ||
| 73 | # Resize width, scale height | ||
| 74 | $image = $image->scale(xpixels=>$self->thumbnail->{width}); | ||
| 75 | } elsif ($self->thumbnail->{height}) { | ||
| 76 | # Resize height, scale width | ||
| 77 | $image = $image->scale(ypixels=>$self->thumbnail->{height}); | ||
| 78 | } | ||
| 79 | my $thmb_fn = $self->savedir.'/thumb_'.$pic; | ||
| 80 | $thmb_fn = $self->thumbnail->{savedir}.'/thumb_'.$pic if ($self->thumbnail->{savedir}); | ||
| 81 | $image->write( | ||
| 82 | file => $thmb_fn, | ||
| 83 | type => 'jpeg', | ||
| 84 | jpegquality => 90 | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | unlink($self->savedir.'/'.$pic); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | }#end of if upload and submit | ||
| 94 | } | ||
| 95 | |||
| 96 | sub get_value{ | ||
| 97 | my ($self) = @_; | ||
| 98 | my $result=''; | ||
| 99 | my $request=$self->request; | ||
| 100 | $result=$self->{pic}; | ||
| 101 | if (!$self->{pic}){ | ||
| 102 | if ($request->{$self->name}){ | ||
| 103 | $result=$request->{$self->name}; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | return $result; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub render{ | ||
| 110 | my ($self, $options, $view)=@_; | ||
| 111 | if ($view) { $self->{view}=$view; } | ||
| 112 | my $request=$self->request; | ||
| 113 | my $result=''; | ||
| 114 | my $disabled=''; | ||
| 115 | my $class='form_image_select'; | ||
| 116 | $self->label(' ') if ($self->label eq ''); | ||
| 117 | $class=$self->{class} if exists($self->{class}); | ||
| 118 | my $name=' name="'.$self->name.'_upload" '; | ||
| 119 | my $checked=''; | ||
| 120 | my $pic=''; | ||
| 121 | $pic= $self->{pic} if ($self->{pic}); | ||
| 122 | $pic=$request->{$self->name} if ($request->{$self->name}); | ||
| 123 | $disabled=' disabled ' if ($options->{frozen} == 1); | ||
| 124 | if ($options->{frozen} != 1 ){ | ||
| 125 | $result.= $self->errormessage if ($self->{sizeerror} && $self->errormessage); | ||
| 126 | $result.='<input type="file" class="'.$class.'" '.$self->get_attr().$disabled.$name.'>'; | ||
| 127 | $result.='<input type="submit" class="form_image_submit" value="'.$self->caption.'" name="'.$self->name.'_submit">'; | ||
| 128 | } | ||
| 129 | |||
| 130 | $result.='<input type="hidden" name="'.$self->name.'" value="'.$pic.'">'; | ||
| 131 | if ($pic ne ''){ | ||
| 132 | $result.="<br /><br />"; | ||
| 133 | $result.="<img id='thumbnail' src='".$self->loadurl."/thumb_".$pic."'>" if (($self->thumbnail) && ($self->prev)); | ||
| 134 | } | ||
| 135 | |||
| 136 | return $self->vor($options).$result.$self->nach; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | |||
| 142 | __END__ |