removed debug code
[editimage_extension] / EditImage_body.php
1 <?php
2 function efRunEditImage( $par ) {
3     EditImage::run( $par );
4 }
5     
6 function resizeImage ( $srcpath, $x1, $y1, $x2, $y2) {
7     global $wgUseImageMagick, $wgImageMagickConvertCommand;
8     global $wgCustomConvertCommand;
9     global $wgTmpDirectory;
10
11     # generate width and height
12     $w = abs($x2 - $x1);
13     $h = abs($y2 - $y1);
14     
15     # create temporary destination file location
16     $dstpath = tempnam($wgTmpDirectory, "EditImage");
17
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).
24         ' -coalesce ' .
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' );
31     return($dstpath);
32 }
33
34 function uploadNewFile ($old_file, $new_filename) {
35     global $wgRequest;
36     $form = new UploadForm($wgRequest);
37
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;
44     //$form->mReUpload       = true;
45
46     // PHP won't auto-cleanup the file
47     $form->mRemoveTempFile = file_exists( $local_file);
48     $form->execute();
49 }
50  
51 class EditImage extends SpecialPage {
52     function EditImage() {
53         SpecialPage::SpecialPage("EditImage", '', true, 'efRunEditImage');
54         wfLoadExtensionMessages('EditImage');
55     } 
56     
57     function run( $par ) {
58         global $wgRequest, $wgOut;
59
60         global $wgContLang;
61         global $wgUser;
62         # add the javascript
63         global $wgJsMimeType, $wgScriptPath ;
64         $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/lib/prototype.js\"></script>\n");
65         $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/lib/scriptaculous.js\"></script>\n");
66         $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/cropper.js\"></script>\n");
67         $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/EditImage/EditImage.js\"></script>\n");
68
69         # try to find the image that the user entered
70         $image = $wgRequest->getText('image');
71         $title = Title::newFromText( $image, NS_IMAGE );
72         $file = wfFindFile( $title );
73
74         if ( ! $title instanceof Title || $title->getNamespace() != NS_IMAGE ) {
75             $wgOut->addWikiText("Sorry, can't find that image!");
76         } else {
77             # see if we're passed editing information
78             if ($wgRequest->wasPosted()) {
79                 $x1 = $wgRequest->getInt(x1);
80                 $x2 = $wgRequest->getInt(x2);
81                 $y1 = $wgRequest->getInt(y1);
82                 $y2 = $wgRequest->getInt(y2);
83
84                 $resized_fn = resizeImage($file->getFullPath(), $x1, $y1, $x2, $y2);
85                 uploadNewFile($file, $resized_fn);
86
87             } else {
88
89                 if ( $file && $file->exists() ) {
90                     $wgOut->addHTML("<p>Use your mouse to select the new area on the image below. When you are done, press crop.</p>\n");
91
92                     # add image
93                     $wgOut->addHTML("<div><img src=\"{$file->getUrl()}\" alt=\"source image\" id=\"sourceImage\" /></div>\n");
94
95                     # add bottom of the template
96                     $encComment = htmlspecialchars($wgRequest->getText('wpUploadDescription'));
97                     $align1 = $wgContLang->isRTL() ? 'left' : 'right';
98                     $align2 = $wgContLang->isRTL() ? 'right' : 'left';
99                     $cols = intval($wgUser->getOption('cols'));
100                     if( $wgUser->getOption( 'editwidth' ) ) { 
101                         $width = " style=\"width:100%\""; 
102                     } else {
103                         $width = ''; 
104                     }
105                     $summary = wfMsgExt( 'fileuploadsummary', 'parseinline' );
106
107                     $wgOut->addHTML("<form action=\"\" method=\"POST\">
108                                      <input type=\"hidden\" name=\"image\" value=\"{$image}\" />
109                                      <input type=\"hidden\" name=\"x1\" id=\"x1\" />
110                                      <input type=\"hidden\" name=\"y1\" id=\"y1\" />
111                                      <input type=\"hidden\" name=\"x2\" id=\"x2\" />
112                                      <input type=\"hidden\" name=\"y2\" id=\"y2\" />
113                                      <input type=\"hidden\" name=\"width\" id=\"width\" />
114                                      <input type=\"hidden\" name=\"height\" id=\"height\" />
115                                      <table border='0'><tr>
116                                        <td align='$align1'><label for='wpUploadDescription'>{$summary}</label></td>
117                                        <td align='$align2'>
118                                          <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6' cols='{$cols}'{$width}>$encComment</textarea>
119                                        </td>
120                                      </tr><table>
121
122                                      <p><input type=\"submit\" value=\"Crop Image\" /></p>
123                                      </form>");
124
125                 } else {
126                     $wgOut->setStatusCode( 404 );
127                 }
128             }
129
130         }
131
132     }
133
134 }
135
136 ?>

Benjamin Mako Hill || Want to submit a patch?