294a07ddb8a9ab9a016279665b728adbbc68e2b0
[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
45     $form->execute();
46 }
47  
48 class EditImage extends SpecialPage {
49     function EditImage() {
50         SpecialPage::SpecialPage("EditImage", '', true, 'efRunEditImage');
51         wfLoadExtensionMessages('EditImage');
52     } 
53     
54     function run( $par ) {
55         global $wgRequest, $wgOut;
56
57         global $wgContLang;
58         global $wgUser;
59         # globals for javascript
60         global $wgJsMimeType, $wgScriptPath; 
61         
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 );
66
67         if ( ! $title instanceof Title || $title->getNamespace() != NS_IMAGE ) {
68             $wgOut->addWikiText("Sorry, can't find that image!");
69         } else {
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);
76
77                 $resized_fn = resizeImage($file->getFullPath(), $x1, $y1, $x2, $y2);
78                 uploadNewFile($file, $resized_fn);
79
80                 # delete the file if it still exists
81                 if (file_exists($resized_fn)) { unlink($resized_fn); }
82
83             } else {
84
85                 if ( $file && $file->exists() ) {
86                     # add the javascript
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");
91
92                     $instructions = wfMsg('editimage-instructions');
93                     $crop_text = wfMsg('cropimage');
94                     $wgOut->addHTML("<p>{$instructions}</p>\n");
95
96                     # add image
97                     $wgOut->addHTML("<div><img src=\"{$file->getUrl()}\" alt=\"source image\" id=\"sourceImage\" /></div>\n");
98
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%\""; 
106                     } else {
107                         $width = ''; 
108                     }
109                     $summary = wfMsgExt( 'fileuploadsummary', 'parseinline' );
110
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>
121                                        <td align='$align2'>
122                                          <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6' cols='{$cols}'{$width}>$encComment</textarea>
123                                        </td>
124                                      </tr><table>
125
126                                      <p><input type=\"submit\" value=\"{$crop_text}\" /></p>
127                                      </form>");
128
129                 } else {
130                     $wgOut->setStatusCode( 404 );
131                 }
132             }
133         }
134     }
135 }
136
137 ?>

Benjamin Mako Hill || Want to submit a patch?