2 function efRunEditImage( $par ) {
3 EditImage::run( $par );
6 function resizeImage ( $srcpath, $x1, $y1, $x2, $y2) {
7 global $wgUseImageMagick, $wgImageMagickConvertCommand;
8 global $wgCustomConvertCommand;
9 global $wgTmpDirectory;
11 # generate width and height
15 # create temporary destination file location
16 $dstpath = tempnam($wgTmpDirectory, "EditImage");
18 # specify white background color, will be used for transparent images
19 # in internet explorer/windows instead of default black.
20 $cmd = wfescapeshellarg($wgImageMagickConvertCommand) .
21 " -background white " .
22 wfescapeshellarg($srcpath) .
23 // coalesce is needed to scale animated gifs properly (bug 1017).
25 " -crop {$w}x{$h}+$x1+$y1! " .
26 wfescapeshellarg($dstpath) . " 2>&1";
27 wfDebug( __METHOD__.": running ImageMagick: $cmd\n");
28 wfProfileIn( 'convert' );
29 $err = wfShellExec( $cmd, $retval );
30 wfProfileOut( 'convert' );
34 function uploadNewFile ($old_file, $new_filename) {
36 $form = new UploadForm($wgRequest);
38 $form->mTempPath = $new_filename;
39 $form->mSrcName = $old_file->getName();
40 $form->mFileSize = filesize($new_filename);
41 $form->mSessionKey = false;
42 $form->mStashed = false;
43 $form->mUploadClicked = true;
48 class EditImage extends SpecialPage {
49 function EditImage() {
50 SpecialPage::SpecialPage("EditImage", '', true, 'efRunEditImage');
51 wfLoadExtensionMessages('EditImage');
54 function run( $par ) {
55 global $wgRequest, $wgOut;
59 # globals for javascript
60 global $wgJsMimeType, $wgScriptPath;
62 # try to find the image that the user entered
63 $image = $wgRequest->getText('image');
64 $title = Title::newFromText( $image, NS_IMAGE );
65 $file = wfFindFile( $title );
67 if ( ! $title instanceof Title || $title->getNamespace() != NS_IMAGE ) {
68 $wgOut->addWikiText("Sorry, can't find that image!");
70 # see if we're passed editing information
71 if ($wgRequest->wasPosted()) {
72 $x1 = $wgRequest->getInt(x1);
73 $x2 = $wgRequest->getInt(x2);
74 $y1 = $wgRequest->getInt(y1);
75 $y2 = $wgRequest->getInt(y2);
77 $resized_fn = resizeImage($file->getFullPath(), $x1, $y1, $x2, $y2);
78 uploadNewFile($file, $resized_fn);
80 # delete the file if it still exists
81 if (file_exists($resized_fn)) { unlink($resized_fn); }
85 if ( $file && $file->exists() ) {
87 $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/lib/prototype.js\"></script>\n");
88 $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/lib/scriptaculous.js\"></script>\n");
89 $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/cropper.js\"></script>\n");
90 $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/EditImage.js\"></script>\n");
92 $instructions = wfMsg('editimage-instructions');
93 $crop_text = wfMsg('cropimage');
94 $wgOut->addHTML("<p>{$instructions}</p>\n");
97 $wgOut->addHTML("<div><img src=\"{$file->getUrl()}\" alt=\"source image\" id=\"sourceImage\" /></div>\n");
99 # add bottom of the template
100 $encComment = htmlspecialchars($wgRequest->getText('wpUploadDescription'));
101 $align1 = $wgContLang->isRTL() ? 'left' : 'right';
102 $align2 = $wgContLang->isRTL() ? 'right' : 'left';
103 $cols = intval($wgUser->getOption('cols'));
104 if( $wgUser->getOption( 'editwidth' ) ) {
105 $width = " style=\"width:100%\"";
109 $summary = wfMsgExt( 'fileuploadsummary', 'parseinline' );
111 $wgOut->addHTML("<form action=\"\" method=\"POST\">
112 <input type=\"hidden\" name=\"image\" value=\"{$image}\" />
113 <input type=\"hidden\" name=\"x1\" id=\"x1\" />
114 <input type=\"hidden\" name=\"y1\" id=\"y1\" />
115 <input type=\"hidden\" name=\"x2\" id=\"x2\" />
116 <input type=\"hidden\" name=\"y2\" id=\"y2\" />
117 <input type=\"hidden\" name=\"width\" id=\"width\" />
118 <input type=\"hidden\" name=\"height\" id=\"height\" />
119 <table border='0'><tr>
120 <td align='$align1'><label for='wpUploadDescription'>{$summary}</label></td>
122 <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6' cols='{$cols}'{$width}>$encComment</textarea>
126 <p><input type=\"submit\" value=\"{$crop_text}\" /></p>
130 $wgOut->setStatusCode( 404 );