2 /* Copyright (c) 2008 Benjamin Mako Hill <mako@atdot.cc>
3 This file is part of the EditImage Mediawiki Extension.
5 EditImage is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation, either version 3 of the License, or (at
8 your option) any later version.
10 Foobar is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License
16 along with Foobar. If not, see <http://www.gnu.org/licenses/>. */
18 function efRunEditImage( $par ) {
19 EditImage::run( $par );
22 function resizeImage ( $srcpath, $x1, $y1, $x2, $y2) {
23 global $wgUseImageMagick, $wgImageMagickConvertCommand;
24 global $wgCustomConvertCommand;
25 global $wgTmpDirectory;
27 # generate width and height
31 # create temporary destination file location
32 $dstpath = tempnam($wgTmpDirectory, "EditImage");
34 # specify white background color, will be used for transparent images
35 # in internet explorer/windows instead of default black.
36 $cmd = wfescapeshellarg($wgImageMagickConvertCommand) .
37 " -background white " .
38 wfescapeshellarg($srcpath) .
39 // coalesce is needed to scale animated gifs properly (bug 1017).
41 " -crop {$w}x{$h}+$x1+$y1! " .
42 wfescapeshellarg($dstpath) . " 2>&1";
43 wfDebug( __METHOD__.": running ImageMagick: $cmd\n");
44 wfProfileIn( 'convert' );
45 $err = wfShellExec( $cmd, $retval );
46 wfProfileOut( 'convert' );
50 function uploadNewFile ($old_file, $new_filename) {
52 $form = new UploadForm($wgRequest);
54 $form->mTempPath = $new_filename;
55 $form->mSrcName = $old_file->getName();
56 $form->mFileSize = filesize($new_filename);
57 $form->mSessionKey = false;
58 $form->mStashed = false;
59 $form->mUploadClicked = true;
64 class EditImage extends SpecialPage {
65 function EditImage() {
66 SpecialPage::SpecialPage("EditImage", '', true, 'efRunEditImage');
67 wfLoadExtensionMessages('EditImage');
70 function run( $par ) {
71 global $wgRequest, $wgOut;
75 # globals for javascript
76 global $wgJsMimeType, $wgScriptPath;
78 # try to find the image that the user entered
79 $image = $wgRequest->getText('image');
80 $title = Title::newFromText( $image, NS_IMAGE );
81 $file = wfFindFile( $title );
83 if ( ! $title instanceof Title || $title->getNamespace() != NS_IMAGE ) {
84 $wgOut->addWikiText("Sorry, can't find that image!");
86 # see if we're passed editing information
87 if ($wgRequest->wasPosted()) {
88 $x1 = $wgRequest->getInt(x1);
89 $x2 = $wgRequest->getInt(x2);
90 $y1 = $wgRequest->getInt(y1);
91 $y2 = $wgRequest->getInt(y2);
93 $resized_fn = resizeImage($file->getFullPath(), $x1, $y1, $x2, $y2);
94 uploadNewFile($file, $resized_fn);
96 # delete the file if it still exists
97 if (file_exists($resized_fn)) { unlink($resized_fn); }
101 if ( $file && $file->exists() ) {
103 $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/lib/prototype.js\"></script>\n");
104 $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/lib/scriptaculous.js\"></script>\n");
105 $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/cropper.js\"></script>\n");
106 $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/EditImage.js\"></script>\n");
108 $instructions = wfMsg('editimage-instructions');
109 $crop_text = wfMsg('cropimage');
110 $wgOut->addHTML("<p>{$instructions}</p>\n");
113 $wgOut->addHTML("<div><img src=\"{$file->getUrl()}\" alt=\"source image\" id=\"sourceImage\" /></div>\n");
115 # add bottom of the template
116 $encComment = htmlspecialchars($wgRequest->getText('wpUploadDescription'));
117 $align1 = $wgContLang->isRTL() ? 'left' : 'right';
118 $align2 = $wgContLang->isRTL() ? 'right' : 'left';
119 $cols = intval($wgUser->getOption('cols'));
120 if( $wgUser->getOption( 'editwidth' ) ) {
121 $width = " style=\"width:100%\"";
125 $summary = wfMsgExt( 'fileuploadsummary', 'parseinline' );
127 $wgOut->addHTML("<form action=\"\" method=\"POST\">
128 <input type=\"hidden\" name=\"image\" value=\"{$image}\" />
129 <input type=\"hidden\" name=\"x1\" id=\"x1\" />
130 <input type=\"hidden\" name=\"y1\" id=\"y1\" />
131 <input type=\"hidden\" name=\"x2\" id=\"x2\" />
132 <input type=\"hidden\" name=\"y2\" id=\"y2\" />
133 <input type=\"hidden\" name=\"width\" id=\"width\" />
134 <input type=\"hidden\" name=\"height\" id=\"height\" />
135 <table border='0'><tr>
136 <td align='$align1'><label for='wpUploadDescription'>{$summary}</label></td>
138 <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6' cols='{$cols}'{$width}>$encComment</textarea>
142 <p><input type=\"submit\" value=\"{$crop_text}\" /></p>
146 $wgOut->setStatusCode( 404 );