| 1 | package HTML::TurboForm::Element::Imageupload; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use base qw(HTML::TurboForm::Element); | ||
| 5 | use Imager; | ||
| 6 | use File::Finder; | ||
| 7 | |||
| 8 | __PACKAGE__->mk_accessors( qw/ prev upload keeporiginal scaletype filename width height savedir thumbnail loadurl caption maxsize errormessage / ); | ||
| 9 | |||
| 10 | sub new{ | ||
| 11 | my ($class, $request, $upload) = @_; | ||
| 12 | my $self = $class->SUPER::new( $request ); | ||
| 13 | $self->upload( $upload ); | ||
| 14 | $self->{pic}=''; | ||
| 15 | $self->do_img(); | ||
| 16 | return $self; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub ren{ | ||
| 20 | my ($self, $newfilename)=@_; | ||
| 21 | my $file=''; | ||
| 22 | my $request=$self->request; | ||
| 23 | $file=$self->{pic}; | ||
| 24 | if (!$self->{pic}){ | ||
| 25 | $file=$request->{$self->name} if ($request->{$self->name}); | ||
| 26 | } | ||
| 27 | rename($self->savedir.'/med_'.$file, $self->savedir.'/'.$newfilename.'.jpg'); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub ren_thumb{ | ||
| 31 | my ($self, $newfilename)=@_; | ||
| 32 | my $file=''; | ||
| 33 | my $request=$self->request; | ||
| 34 | $file=$self->{pic}; | ||
| 35 | if (!$self->{pic}){ | ||
| 36 | $file=$request->{$self->name} if ($request->{$self->name}); | ||
| 37 | } | ||
| 38 | rename($self->thumbnail->{savedir}.'/thumb_'.$file, $self->thumbnail->{savedir}.'/'.$newfilename.'.jpg'); | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub do_img{ | ||
| 43 | my ($self)=@_; | ||
| 44 | my $request=$self->request; | ||
| 45 | my $pic=''; | ||
| 46 | $pic = $self->request->{$self->name} if ($self->request->{$self->name} ); | ||
| 47 | if ($request->{ $self->name.'_upload' }) { | ||
| 48 | |||
| 49 | if( $self->upload->type !~ /^image\/(jpeg|jpg|gif|png|pjpeg)$/ ) { | ||
| 50 | #$c->stash->{ 'error' } = 'Filetype not supported!'; | ||
| 51 | } else { | ||
| 52 | # read image | ||
| 53 | my $image = Imager->new; | ||
| 54 | $self->{sizeerror}=0; | ||
| 55 | if ($self->maxsize) { | ||
| 56 | if (($self->upload->size/1024) > $self->maxsize){ | ||
| 57 | $self->{sizeerror}=1; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | if (!$self->{sizeerror}){ | ||
| 61 | if( $image->read( file => $self->upload->tempname ) ) { | ||
| 62 | # remove alpha channels because jpg does not support it # and its not used anyways | ||
| 63 | $image = $image->convert( preset => 'noalpha' ); | ||
| 64 | #attribute keeporignal isparams local path for storing orig sized images | ||
| 65 | |||
| 66 | my $tmp = File::Temp->new( DIR => $self->savedir.'', UNLINK => 0, SUFFIX => '.jpg' ); | ||
| 67 | $pic = substr( $tmp, length( $self->savedir )+1 ); | ||
| 68 | $self->{pic}=$pic; | ||
| 69 | |||
| 70 | if ($self->keeporiginal){ | ||
| 71 | $self->upload->copy_to($self->keeporiginal.'/orig_'.$pic); | ||
| 72 | } | ||
| 73 | |||
| 74 | # if there is a save dir, resize. depending if width and/or height is given, scale to dimensions | ||
| 75 | if ($self->savedir){ | ||
| 76 | my $continueflag=1; | ||
| 77 | if ($self->scaletype eq 'smart'){ | ||
| 78 | if ($self->width && $self->height){ | ||
| 79 | $continueflag = 0; | ||
| 80 | my $container_dir='v'; | ||
| 81 | if ($self->width > $self->height){ | ||
| 82 | my $container_dir='h'; | ||
| 83 | } | ||
| 84 | |||
| 85 | my $dir='v'; | ||
| 86 | if ($image->getwidth() > $image->getheight()){ | ||
| 87 | $dir='h'; | ||
| 88 | } | ||
| 89 | |||
| 90 | if ($container_dir ne $dir ){ | ||
| 91 | my $tmp=$self->width; | ||
| 92 | $self->width=$self->height; | ||
| 93 | $self->height=$tmp; | ||
| 94 | } | ||
| 95 | $image = $image->scale(ypixels=>$self->height,xpixels=>$self->width); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | if ($continueflag==1){ | ||
| 99 | if (($self->width) and ($self->height) and ($self->scaletype)) { | ||
| 100 | # Resize height, scale width | ||
| 101 | $image = $image->scale(ypixels=>$self->height,xpixels=>$self->width,type=>$self->scaletype); | ||
| 102 | } elsif (($self->width) and ($self->height)) { | ||
| 103 | # No scale. Resize to given dimensions | ||
| 104 | $image = $image->scaleX(pixels=>$self->width)->scaleY(pixels=>$self->height); | ||
| 105 | } elsif ($self->width) { | ||
| 106 | # Resize width, scale height | ||
| 107 | $image = $image->scale(xpixels=>$self->width); | ||
| 108 | } elsif ($self->height) { | ||
| 109 | # Resize height, scale width | ||
| 110 | $image = $image->scale(ypixels=>$self->height); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | $image->write( | ||
| 114 | file => $self->savedir.'/med_'.$pic, | ||
| 115 | type => 'jpeg', | ||
| 116 | jpegquality => 90 | ||
| 117 | ); | ||
| 118 | unlink($self->upload->tempname); | ||
| 119 | |||
| 120 | |||
| 121 | if ($self->thumbnail) { | ||
| 122 | if ($self->thumbnail->{width} || $self->thumbnail->{height} ) { | ||
| 123 | if (($self->thumbnail->{width}) and ($self->thumbnail->{height})) { | ||
| 124 | # No scale. Resize to given dimensions | ||
| 125 | $image = $image->scaleX(pixels=>$self->thumbnail->{width})->scaleY(pixels=>$self->thumbnail->{height}); | ||
| 126 | } elsif ($self->thumbnail->{width}) { | ||
| 127 | # Resize width, scale height | ||
| 128 | $image = $image->scale(xpixels=>$self->thumbnail->{width}); | ||
| 129 | } elsif ($self->thumbnail->{height}) { | ||
| 130 | # Resize height, scale width | ||
| 131 | $image = $image->scale(ypixels=>$self->thumbnail->{height}); | ||
| 132 | } | ||
| 133 | my $thmb_fn = $self->savedir.'/thumb_'.$pic; | ||
| 134 | $thmb_fn = $self->thumbnail->{savedir}.'/thumb_'.$pic if ($self->thumbnail->{savedir}); | ||
| 135 | $image->write( | ||
| 136 | file => $thmb_fn, | ||
| 137 | type => 'jpeg', | ||
| 138 | jpegquality => 90 | ||
| 139 | ); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | unlink($self->savedir.'/'.$pic); | ||
| 143 | #$self->{pic}=$pic; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | }#end of if upload and submit | ||
| 149 | } | ||
| 150 | |||
| 151 | sub get_value{ | ||
| 152 | my ($self) = @_; | ||
| 153 | my $result=''; | ||
| 154 | my $request=$self->request; | ||
| 155 | |||
| 156 | $result=$self->{pic}; | ||
| 157 | if (!$self->{pic}){ | ||
| 158 | $result=$request->{$self->name} if ($request->{$self->name}); | ||
| 159 | } | ||
| 160 | return $result; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub render{ | ||
| 164 | my ($self, $options, $view)=@_; | ||
| 165 | if ($view) { $self->{view}=$view; } | ||
| 166 | my $request=$self->request; | ||
| 167 | my $result=''; | ||
| 168 | my $disabled=''; | ||
| 169 | my $class='form_image_select'; | ||
| 170 | $self->label(' ') if ($self->label eq ''); | ||
| 171 | $class=$self->{class} if exists($self->{class}); | ||
| 172 | my $name=' name="'.$self->name.'_upload" '; | ||
| 173 | my $checked=''; | ||
| 174 | my $pic=''; | ||
| 175 | $pic= $self->{pic} if ($self->{pic}); | ||
| 176 | $pic=$request->{$self->name} if ($request->{$self->name}); | ||
| 177 | |||
| 178 | if ($options->{frozen}) { | ||
| 179 | $disabled=' disabled ' if ($options->{frozen} == 1); | ||
| 180 | } | ||
| 181 | |||
| 182 | my $tmpres=''; | ||
| 183 | $tmpres.= $self->errormessage if ($self->{sizeerror} && $self->errormessage); | ||
| 184 | $tmpres.='<input type="file" class="'.$class.'" '.$self->get_attr().$disabled.$name.'>'; | ||
| 185 | |||
| 186 | if ($options->{frozen}) { | ||
| 187 | $result .= $tmpres unless ($options->{frozen} == 1 ); | ||
| 188 | } else { | ||
| 189 | $result .= $tmpres; | ||
| 190 | } | ||
| 191 | |||
| 192 | $result.='<input type="hidden" name="'.$self->name.'" value="'.$pic.'">'; | ||
| 193 | if ($pic ne ''){ | ||
| 194 | $result.="<br /><br />"; | ||
| 195 | $result.="<img id='thumbnail' src='".$self->loadurl.$pic."'>" if ($self->loadurl); | ||
| 196 | } | ||
| 197 | |||
| 198 | return $self->vor($options).$result.$self->nach; | ||
| 199 | } | ||
| 200 | |||
| 201 | 1; | ||
| 202 | |||
| 203 | |||
| 204 | __END__ |