noted the new URL for git repository
[editimage_extension] / EditImage_body.php
1 <?php
2 /* Copyright (c) 2008 Benjamin Mako Hill <mako@atdot.cc>
3    This file is part of the EditImage Mediawiki Extension.  
4
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.  
9    
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
13    for more details.
14
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/>.  */
17
18 function efRunEditImage( $par ) {
19     EditImage::run( $par );
20 }
21     
22 function resizeImage ( $srcpath, $x1, $y1, $x2, $y2) {
23     global $wgUseImageMagick, $wgImageMagickConvertCommand;
24     global $wgCustomConvertCommand;
25     global $wgTmpDirectory;
26
27     # generate width and height
28     $w = abs($x2 - $x1);
29     $h = abs($y2 - $y1);
30     
31     # create temporary destination file location
32     $dstpath = tempnam($wgTmpDirectory, "EditImage");
33
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).
40         ' -coalesce ' .
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' );
47     return($dstpath);
48 }
49
50 function uploadNewFile ($old_file, $new_filename) {
51     global $wgRequest;
52     $form = new UploadForm($wgRequest);
53
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;
60
61     $form->execute();
62 }
63  
64 class EditImage extends SpecialPage {
65     function EditImage() {
66         SpecialPage::SpecialPage("EditImage", '', true, 'efRunEditImage');
67         wfLoadExtensionMessages('EditImage');
68     } 
69     
70     function run( $par ) {
71         global $wgRequest, $wgOut;
72
73         global $wgContLang;
74         global $wgUser;
75         # globals for javascript
76         global $wgJsMimeType, $wgScriptPath; 
77         
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 );
82
83         if ( ! $title instanceof Title || $title->getNamespace() != NS_IMAGE ) {
84             $wgOut->addWikiText("Sorry, can't find that image!");
85         } else {
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);
92
93                 $resized_fn = resizeImage($file->getFullPath(), $x1, $y1, $x2, $y2);
94                 uploadNewFile($file, $resized_fn);
95
96                 # delete the file if it still exists
97                 if (file_exists($resized_fn)) { unlink($resized_fn); }
98
99             } else {
100
101                 if ( $file && $file->exists() ) {
102                     # add the javascript
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");
107
108                     $instructions = wfMsg('editimage-instructions');
109                     $crop_text = wfMsg('cropimage');
110                     $wgOut->addHTML("<p>{$instructions}</p>\n");
111
112                     # add image
113                     $wgOut->addHTML("<div><img src=\"{$file->getUrl()}\" alt=\"source image\" id=\"sourceImage\" /></div>\n");
114
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%\""; 
122                     } else {
123                         $width = ''; 
124                     }
125                     $summary = wfMsgExt( 'fileuploadsummary', 'parseinline' );
126
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>
137                                        <td align='$align2'>
138                                          <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6' cols='{$cols}'{$width}>$encComment</textarea>
139                                        </td>
140                                      </tr></table>
141
142                                      <p><input type=\"submit\" value=\"{$crop_text}\" /></p>
143                                      </form>");
144
145                 } else {
146                     $wgOut->setStatusCode( 404 );
147                 }
148             }
149         }
150     }
151 }
152
153 ?>

Benjamin Mako Hill || Want to submit a patch?