BB’s Blog
优化上传(5):上传后首选HTML代码的选项
路杨在“MT上传文件的改动”中还对: 1、简化输出,直接上传后就有显示HTML代码的选项。 因为估计大家一般都选择生成HTML代码吧? 顺手根据自己习惯删除了“弹出式代码”,都是“include代码”了。但是并没有删除“以上传的文件/图片建立一个新Blog”的选择,只是把它放在了第2位而已。 2、删除了图片的缩略图功能。 缩略图觉得功能不大,这个对Perl来讲,资源消耗还是比较可观的,故删除之,但是作为补充,添加了如果上传图片宽度大于530px,则生成的HTML代码为按比例缩小尺寸的带原尺寸连接的代码。做了修改。 我并没有全部依照上述方法进行修改: 1、直接显示html我是使用插件来完成的 首先安装www.bayashi.net提供的一个直接显示HTML的插件 doSelectShowMeTheHTML.pl。 使用方法简单,只需要将此文件解压放到plugins里面就可以了。 2、为了让用户有更多的选择,我没有对“缩略图”功能进行删除。仅对上传图片宽度大于530px,将其生成的HTML代码为按比例缩小尺寸的代码进行了修改。 方法如下: 修改lib/MT/APP/CMS.pm文件,将: elsif ($q->param('include') || $q->param('popup')) { if ($thumb) { return <<"HTML"; <a href="$url"><img alt="$fname" src="$thumb" width="$thumb_width" height="$thumb_height" /></a> HTML } else { return <<"HTML"; <img alt="$fname" src="$url" width="$width" height="$height" /> HTML } } elsif ($q->param('link')) { return $app->translate_templatized(<<"HTML"); <a href="$url"><MT_TRANS phrase="Download file"></a> HTML } }修改为 if ($q->param('include') || $q->param('popup')) { if ($width >530 ) { return $app->translate_templatized(<<"HTML"); <a href="$url"><img alt="$fname" src="$url" width="530" title="<MT_TRANS phrase="View image">" /></a> HTML } else { return <<"HTML"; <img alt="$fname" src="$url" width="$width" height="$height" /> HTML } } elsif ($q->param('link')) { return $app->translate_templatized(<<"HTML"); <a href="$url"><MT_TRANS phrase="Download file"></a> HTML } }
下面是我没有更改的,“简化输出手动修改版及删除缩略图和弹出式图片HTML输出”。 同样打开编辑lib/MT/APP/CMS.pm 1、简化输出(即为直接显示html和删除缩略图,这部分我没有修改)。 第一次提交输出的定位函数依然是sub upload_file { };而HTML输出的函数来自sub _process_post_upload { }。这个就要求 upload_file函数中调用 _process_post_upload 。 于是定位到sub upload_file { }函数中 if ($param{is_image}) { eval { require MT::Image; MT::Image->new or die; }; $param{do_thumb} = !$@ ? 1 : 0; MT->run_callbacks('CMSUploadFile', File => $local_file, Url => $url, Size => $bytes, Type => 'image', Blog => $blog); MT->run_callbacks('CMSUploadImage', File => $local_file, Url => $url, Size => $bytes, Height => $h, Width => $w, Type => 'image', ImageType => $id, Blog => $blog); } else { MT->run_callbacks('CMSUploadFile', File => $local_file, Url => $url, Size => $bytes, Type => 'file', Blog => $blog); } 修改成 if ($param{is_image}) { $q->param('include', '1'); # eval { require MT::Image; MT::Image->new or die; }; # $param{do_thumb} = !$@ ? 1 : 0; MT->run_callbacks('CMSUploadFile', File => $local_file, Url => $url, Size => $bytes, Type => 'image', Blog => $blog); MT->run_callbacks('CMSUploadImage', File => $local_file, Url => $url, Size => $bytes, Height => $h, Width => $w, Type => 'image', ImageType => $id, Blog => $blog); } else { $q->param('link', '1'); MT->run_callbacks('CMSUploadFile', File => $local_file, Url => $url, Size => $bytes, Type => 'file', Blog => $blog); } 并且在其后面添加: $q->param('url', $param{url}); $q->param('site_path', $param{site_path}); $q->param('fname', $param{fname}); $q->param('width', $param{width}); $q->param('height', $param{height}); $q->param('image_type', $param{image_type}); 这些部分和前段修改中用颜色强调语句都是给_process_post_upload 函数初始化参数。 再在其后添加: $param{upload_html} =$app->_process_post_upload; 到这里upload_file函数已经添加了输出HTML功能。 我们还需要修改_process_post_upload 函数来改进输出的HTML代码格式和修改输出模版文件(tmpl文件) tmpl/cms/upload_complete.tmpl 来完成输出界面的支持。 这里都是些HTML代码,就不细说了,顺手加了段div和js和添加了HTML代码显示,修改完成的upload_complete.tmpl 文件内容如下: <TMPL_INCLUDE NAME="header-popup.tmpl"> <!-- Modify By EasunLee 2006/09/28 --> <script type="text/javascript"> <!-- function doClick (f, mode) { <TMPL_IF NAME=CAN_POST> if (f.new_entry[1].checked) doStartEntry(f, mode); else </TMPL_IF> window.close(); } function doShowHTML (mode) { var _divs= document.getElementById("ShowHtmlDIV"); if (mode == 1) _divs.style.display="block"; else _divs.style.display="none"; } function doStartEntry (f, mode) { var url = '<TMPL_VAR NAME=SCRIPT_URL>?__mode=start_upload_entry&blog_id=<TMPL_VAR NAME=BLOG_ID>&url=<TMPL_VAR NAME=URL ESCAPE=URL>&site_path=<TMPL_VAR NAME=SITE_PATH>&fname=<TMPL_VAR NAME=FNAME ESCAPE=URL>&' + mode; window.opener.location = url; window.close(); } //--> </script> <form action="<TMPL_VAR NAME=SCRIPT_URL>" onsubmit="return false;"> <h2><span class="weblog-title-highlight"><TMPL_VAR NAME=BLOG_NAME ESCAPE=HTML>:</span> <MT_TRANS phrase="Upload File"></h2> <p><MT_TRANS phrase="The file named '[_1]' has been uploaded. Size: [quant,_2,byte]." params="<TMPL_VAR NAME=FNAME ESCAPE=HTML>%%<TMPL_VAR NAME=BYTES>"></p> <label><input type="radio" name="new_entry" value="0" checked="checked" onClick="doShowHTML('1')" /> <MT_TRANS phrase="Show me the HTML"></label> <div id="ShowHtmlDIV" name="ShowHtmlDIV"> <p><label for="html"><MT_TRANS phrase="Copy and paste this HTML into your entry."></label></p> <p> <textarea name="html" id="html" rows="10" style="width: 100%;"><TMPL_VAR NAME=UPLOAD_HTML ESCAPE=HTML></textarea> </p> </div> <TMPL_IF NAME=CAN_POST> <p> <label><input type="radio" name="new_entry" value="1" onClick="doShowHTML('0')" /> <MT_TRANS phrase="Create a new entry using this uploaded file"></label> <a href="#" onclick="return openManual('file_upload', 'creating_a_new_entry')" class="help">?</a><br /> </p> </TMPL_IF> <div> <TMPL_IF NAME=IS_IMAGE> <input type="button" onclick="doClick(this.form, 'include=1&width=<TMPL_VAR NAME=WIDTH>&height=<TMPL_VAR NAME=HEIGHT>&image_type=<TMPL_VAR NAME=IMAGE_TYPE>')" value="<MT_TRANS phrase="Go">/<MT_TRANS phrase="Close">" /> <TMPL_ELSE> <input type="button" onclick="doClick(this.form, 'link=1')" value="<MT_TRANS phrase="Go">/<MT_TRANS phrase="Close">" /> </TMPL_IF> <input type="button" onclick="window.location='<TMPL_VAR NAME=SCRIPT_URL>?__mode=start_upload&blog_id=<TMPL_VAR NAME=BLOG_ID>'" value="<MT_TRANS phrase="Upload Another">" /> </div> </form> <TMPL_INCLUDE NAME="footer-popup.tmpl"> 2、缩略图的删除 先定位函数 sub _process_post_upload{},找到: my($thumb, $thumb_width, $thumb_height); if ($thumb = $q->param('thumb')) { require MT::Image; my $base_path = $q->param('site_path') ? $blog->site_path : $blog->archive_path; my $file = $q->param('fname'); if ($file =~ m!\.\.|\0|\|!) { return $app->error($app->translate("Invalid filename '[_1]'", $file)); } my $i_file = File::Spec->catfile($base_path, $file); ## Untaint. We checked $file for security holes above. ($i_file) = $i_file =~ /(.+)/s; my $fmgr = $blog->file_mgr; my $data = $fmgr->get_data($i_file, 'upload') or return $app->error($app->translate( "Reading '[_1]' failed: [_2]", $i_file, $fmgr->errstr)); my $image_type = scalar $q->param('image_type'); my $img = MT::Image->new( Data => $data, Type => $image_type ) or return $app->error($app->translate( "Thumbnail failed: [_1]", MT::Image->errstr)); my($w, $h) = map $q->param($_), qw( thumb_width thumb_height ); (my($blob), $thumb_width, $thumb_height) = $img->scale( Width => $w, Height => $h ) or return $app->error($app->translate("Thumbnail failed: [_1]", $img->errstr)); require File::Basename; my($base, $path, $ext) = File::Basename::fileparse($i_file, '\.[^.]*'); my $t_file = $path . $base . '-thumb' . $ext; $fmgr->put_data($blob, $t_file, 'upload') or return $app->error($app->translate( "Error writing to '[_1]': [_2]", $t_file, $fmgr->errstr)); $file =~ s/\Q$base$ext\E$//; my $url = $q->param('site_path') ? $blog->site_url : $blog->archive_url; $url .= '/' unless $url =~ m!/$!; $url .= $file; $thumb = $url . encode_url($base . '-thumb' . $ext); MT->run_callbacks('CMSUploadFile', File => $t_file, Url => $thumb, Size => length($blob), Type => 'thumbnail', Blog => $blog); MT->run_callbacks('CMSUploadImage', File => $t_file, Url => $thumb, Width => $thumb_width, Height => $thumb_height, ImageType => $image_type, Size => length($blob), Type => 'thumbnail', Blog => $blog); } 这段为缩略图生成,彻底删除。 3、弹出式图片删除 然后紧接着的代码 if ($q->param('popup')) { require MT::Template; if (my $tmpl = MT::Template->load({ blog_id => $blog_id, type => 'popup_image' })) { (my $rel_path = $q->param('fname')) =~ s!\.[^.]*$!!; if ($rel_path =~ m!\.\.|\0|\|!) { return $app->error($app->translate( "Invalid basename '[_1]'", $rel_path)); } my $ext = $blog->file_extension || ''; $ext = '.' . $ext if $ext ne ''; require MT::Template::Context; my $ctx = MT::Template::Context->new; $ctx->stash('image_url', $url); $ctx->stash('image_width', $width); $ctx->stash('image_height', $height); my $popup = $tmpl->build($ctx); my $fmgr = $blog->file_mgr; my $root_path = $q->param('site_path') ? $blog->site_path : $blog->archive_path; my $abs_file_path = File::Spec->catfile($root_path, $rel_path . $ext); ## If the popup filename already exists, we don't want to overwrite ## it, because it could contain valuable data; so we'll just make ## sure to generate the name uniquely. my($i, $rel_path_ext) = (0, $rel_path . $ext); while ($fmgr->exists($abs_file_path)) { $rel_path_ext = $rel_path . ++$i . $ext; $abs_file_path = File::Spec->catfile($root_path, $rel_path_ext); } my ($vol, $dirs, $basename) = File::Spec->splitpath($rel_path_ext); my $rel_url_ext = File::Spec->catpath($vol, $dirs, encode_url($basename)); ## Untaint. We have checked for security holes above, so we ## should be safe. ($abs_file_path) = $abs_file_path =~ /(.+)/s; $fmgr->put_data($popup, $abs_file_path, 'upload') or return $app->error($app->translate( "Error writing to '[_1]': [_2]", $abs_file_path, $fmgr->errstr)); $url = $q->param('site_path') ? $blog->site_url : $blog->archive_url; $url .= '/' unless $url =~ m!/$!; $rel_url_ext =~ s!^/!!; $url .= $rel_url_ext; MT->run_callbacks('CMSUploadFile', File => $abs_file_path, Url => $url, Size => length($popup), Type => 'popup', Blog => $blog); } my $link = $thumb ? qq(<img src="$thumb" width="$thumb_width" height="$thumb_height" alt="" />) : q{<MT_TRANS phrase="View image">}; return $app->translate_templatized(<<"HTML"); <a href="$url" onclick="window.open('$url','popup','width=$width,height=$height,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false">$link</a> HTML } 这段是“弹出式图片”的HTML输出,也删除之。
Comments(0)
BB’s Blog: version mobile