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

Benjamin Mako Hill || Want to submit a patch?