r93067 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93066‎ | r93067 | r93068 >
Date:17:06, 25 July 2011
Author:ashley
Status:deferred
Tags:
Comment:
SocialProfile: support GD for image uploads, too, instead of requiring ImageMagick. Code by Phil Nelson (see http://web.archive.org/web/20090629011952/http://philnelson.name/2009/01/06/using-mediawikis-socialprofile-extension-without-exec-support-in-php/ and http://web.archive.org/web/20090614215532/http://philnelson.name/wpfiles/wp-content/uploads/2009/01/socialprofile-gd.txt), some coding style and code fixes by me. The original code called imagejpeg() unconditionally, even for gif and png images, which caused that the medium-large, medium and small images weren't generated properly. I also adapted the code for SystemGiftManagerLogo and GiftManagerLogo special pages.
Modified paths:
  • /trunk/extensions/SocialProfile/SystemGifts/SpecialSystemGiftManagerLogo.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserGifts/SpecialGiftManagerLogo.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserProfile/SpecialUploadAvatar.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SocialProfile/SystemGifts/SpecialSystemGiftManagerLogo.php
@@ -203,7 +203,7 @@
204204
205205 global $wgCheckFileExtensions;
206206 if ( $wgCheckFileExtensions ) {
207 - if ( ! $this->checkFileExtension( $finalExt, $this->fileExtensions ) ) {
 207+ if ( !$this->checkFileExtension( $finalExt, $this->fileExtensions ) ) {
208208 $warning .= '<li>' . wfMsg( 'badfiletype', htmlspecialchars( $fullExt ) ) . '</li>';
209209 }
210210 }
@@ -230,7 +230,11 @@
231231 * Try actually saving the thing...
232232 * It will show an error form on failure.
233233 */
234 - $status = $this->saveUploadedFile( $this->mUploadSaveName, $this->mUploadTempName, strtoupper( $fullExt ) );
 234+ $status = $this->saveUploadedFile(
 235+ $this->mUploadSaveName,
 236+ $this->mUploadTempName,
 237+ strtoupper( $fullExt )
 238+ );
235239
236240 if ( $status > 0 ) {
237241 $this->showSuccess( $status );
@@ -238,37 +242,94 @@
239243 }
240244
241245 function createThumbnail( $imageSrc, $ext, $imgDest, $thumbWidth ) {
242 - global $wgImageMagickConvertCommand;
 246+ global $wgUseImageMagick, $wgImageMagickConvertCommand;
243247
244248 list( $origWidth, $origHeight, $typeCode ) = getimagesize( $imageSrc );
245249
246 - if ( $origWidth < $thumbWidth ) {
247 - $thumbWidth = $origWidth;
248 - }
249 - $thumbHeight = ( $thumbWidth * $origHeight / $origWidth );
250 - if ( $thumbHeight < $thumbWidth ) {
251 - $border = ' -bordercolor white -border 0x' . ( ( $thumbWidth - $thumbHeight ) / 2 );
252 - }
253 - if ( $typeCode == 2 ) {
254 - exec(
255 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
256 - $thumbWidth . ' -resize ' . $thumbWidth . ' -quality 100 ' .
257 - $border . ' ' . $imageSrc . ' ' .
258 - $this->avatarUploadDirectory . '/sg_' . $imgDest . '.jpg'
 250+ if ( $wgUseImageMagick ) { // ImageMagick is enabled
 251+ if ( $origWidth < $thumbWidth ) {
 252+ $thumbWidth = $origWidth;
 253+ }
 254+ $thumbHeight = ( $thumbWidth * $origHeight / $origWidth );
 255+ if ( $thumbHeight < $thumbWidth ) {
 256+ $border = ' -bordercolor white -border 0x' . ( ( $thumbWidth - $thumbHeight ) / 2 );
 257+ }
 258+ if ( $typeCode == 2 ) {
 259+ exec(
 260+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
 261+ $thumbWidth . ' -resize ' . $thumbWidth . ' -quality 100 ' .
 262+ $border . ' ' . $imageSrc . ' ' .
 263+ $this->avatarUploadDirectory . '/sg_' . $imgDest . '.jpg'
 264+ );
 265+ }
 266+ if ( $typeCode == 1 ) {
 267+ exec(
 268+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
 269+ $thumbWidth . ' -resize ' . $thumbWidth . ' ' . $imageSrc .
 270+ ' ' . $border . ' ' .
 271+ $this->avatarUploadDirectory . '/sg_' . $imgDest . '.gif'
 272+ );
 273+ }
 274+ if ( $typeCode == 3 ) {
 275+ exec(
 276+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
 277+ $thumbWidth . ' -resize ' . $thumbWidth . ' ' . $imageSrc .
 278+ ' ' . $this->avatarUploadDirectory . '/sg_' . $imgDest . '.png'
 279+ );
 280+ }
 281+ } else { // ImageMagick is not enabled, so fall back to PHP's GD library
 282+ // Get the image size, used in calculations later.
 283+ switch( $typeCode ) {
 284+ case '1':
 285+ $fullImage = imagecreatefromgif( $imageSrc );
 286+ $ext = 'gif';
 287+ break;
 288+ case '2':
 289+ $fullImage = imagecreatefromjpeg( $imageSrc );
 290+ $ext = 'jpg';
 291+ break;
 292+ case '3':
 293+ $fullImage = imagecreatefrompng( $imageSrc );
 294+ $ext = 'png';
 295+ break;
 296+ }
 297+
 298+ $scale = ( $thumbWidth / $origWidth );
 299+
 300+ // Create our thumbnail size, so we can resize to this, and save it.
 301+ $tnImage = imagecreatetruecolor(
 302+ $origWidth * $scale,
 303+ $origHeight * $scale
259304 );
260 - }
261 - if ( $typeCode == 1 ) {
262 - exec(
263 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
264 - $thumbWidth . ' -resize ' . $thumbWidth . ' ' . $imageSrc .
265 - ' ' . $border . ' ' . $this->avatarUploadDirectory . '/sg_' . $imgDest . '.gif'
 305+
 306+ // Resize the image.
 307+ imagecopyresampled(
 308+ $tnImage,
 309+ $fullImage,
 310+ 0, 0, 0, 0,
 311+ $origWidth * $scale,
 312+ $origHeight * $scale,
 313+ $origWidth,
 314+ $origHeight
266315 );
267 - }
268 - if ( $typeCode == 3 ) {
269 - exec(
270 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
271 - $thumbWidth . ' -resize ' . $thumbWidth . ' ' . $imageSrc .
272 - ' ' . $this->avatarUploadDirectory . '/sg_' . $imgDest . '.png'
 316+
 317+ // Create a new image thumbnail.
 318+ if ( $typeCode == 1 ) {
 319+ imagegif( $tnImage, $imageSrc );
 320+ } elseif ( $typeCode == 2 ) {
 321+ imagejpeg( $tnImage, $imageSrc );
 322+ } elseif ( $typeCode == 3 ) {
 323+ imagepng( $tnImage, $imageSrc );
 324+ }
 325+
 326+ // Clean up.
 327+ imagedestroy( $fullImage );
 328+ imagedestroy( $tnImage );
 329+
 330+ // Copy the thumb
 331+ copy(
 332+ $imageSrc,
 333+ $this->avatarUploadDirectory . '/sg_' . $imgDest . '.' . $ext
273334 );
274335 }
275336 }
Index: trunk/extensions/SocialProfile/UserProfile/SpecialUploadAvatar.php
@@ -235,40 +235,98 @@
236236 public $mExtension;
237237
238238 function createThumbnail( $imageSrc, $imageInfo, $imgDest, $thumbWidth ) {
239 - global $wgImageMagickConvertCommand;
 239+ global $wgUseImageMagick, $wgImageMagickConvertCommand;
240240
241 - list( $origWidth, $origHeight, $typeCode ) = $imageInfo;
 241+ if ( $wgUseImageMagick ) { // ImageMagick is enabled
 242+ list( $origWidth, $origHeight, $typeCode ) = $imageInfo;
242243
243 - if ( $origWidth < $thumbWidth ) {
244 - $thumbWidth = $origWidth;
245 - }
246 - $thumbHeight = ( $thumbWidth * $origHeight / $origWidth );
247 - $border = ' -bordercolor white -border 0x';
248 - if ( $thumbHeight < $thumbWidth ) {
249 - $border = ' -bordercolor white -border 0x' . ( ( $thumbWidth - $thumbHeight ) / 2 );
250 - }
251 - if ( $typeCode == 2 ) {
252 - exec(
253 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' . $thumbWidth .
254 - ' -resize ' . $thumbWidth . ' -crop ' . $thumbWidth . 'x' .
255 - $thumbWidth . '+0+0 -quality 100 ' . $border . ' ' .
256 - $imageSrc . ' ' . $this->avatarUploadDirectory . '/' . $imgDest . '.jpg'
 244+ if ( $origWidth < $thumbWidth ) {
 245+ $thumbWidth = $origWidth;
 246+ }
 247+ $thumbHeight = ( $thumbWidth * $origHeight / $origWidth );
 248+ $border = ' -bordercolor white -border 0x';
 249+ if ( $thumbHeight < $thumbWidth ) {
 250+ $border = ' -bordercolor white -border 0x' . ( ( $thumbWidth - $thumbHeight ) / 2 );
 251+ }
 252+ if ( $typeCode == 2 ) {
 253+ exec(
 254+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' . $thumbWidth .
 255+ ' -resize ' . $thumbWidth . ' -crop ' . $thumbWidth . 'x' .
 256+ $thumbWidth . '+0+0 -quality 100 ' . $border . ' ' .
 257+ $imageSrc . ' ' . $this->avatarUploadDirectory . '/' . $imgDest . '.jpg'
 258+ );
 259+ }
 260+ if ( $typeCode == 1 ) {
 261+ exec(
 262+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' . $thumbWidth .
 263+ ' -resize ' . $thumbWidth . ' -crop ' . $thumbWidth . 'x' .
 264+ $thumbWidth . '+0+0 ' . $imageSrc . ' ' . $border . ' ' .
 265+ $this->avatarUploadDirectory . '/' . $imgDest . '.gif'
 266+ );
 267+ }
 268+ if ( $typeCode == 3 ) {
 269+ exec(
 270+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' . $thumbWidth .
 271+ ' -resize ' . $thumbWidth . ' -crop ' . $thumbWidth . 'x' .
 272+ $thumbWidth . '+0+0 ' . $imageSrc . ' ' .
 273+ $this->avatarUploadDirectory . '/' . $imgDest . '.png'
 274+ );
 275+ }
 276+ } else { // ImageMagick is not enabled, so fall back to PHP's GD library
 277+ // Get the image size, used in calculations later.
 278+ list( $origWidth, $origHeight, $typeCode ) = getimagesize( $imageSrc );
 279+
 280+ switch( $typeCode ) {
 281+ case '1':
 282+ $fullImage = imagecreatefromgif( $imageSrc );
 283+ $ext = 'gif';
 284+ break;
 285+ case '2':
 286+ $fullImage = imagecreatefromjpeg( $imageSrc );
 287+ $ext = 'jpg';
 288+ break;
 289+ case '3':
 290+ $fullImage = imagecreatefrompng( $imageSrc );
 291+ $ext = 'png';
 292+ break;
 293+ }
 294+
 295+ $scale = ( $thumbWidth / $origWidth );
 296+
 297+ // Create our thumbnail size, so we can resize to this, and save it.
 298+ $tnImage = imagecreatetruecolor(
 299+ $origWidth * $scale,
 300+ $origHeight * $scale
257301 );
258 - }
259 - if ( $typeCode == 1 ) {
260 - exec(
261 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' . $thumbWidth .
262 - ' -resize ' . $thumbWidth . ' -crop ' . $thumbWidth . 'x' .
263 - $thumbWidth . '+0+0 ' . $imageSrc . ' ' . $border . ' ' .
264 - $this->avatarUploadDirectory . '/' . $imgDest . '.gif'
 302+
 303+ // Resize the image.
 304+ imagecopyresampled(
 305+ $tnImage,
 306+ $fullImage,
 307+ 0, 0, 0, 0,
 308+ $origWidth * $scale,
 309+ $origHeight * $scale,
 310+ $origWidth,
 311+ $origHeight
265312 );
266 - }
267 - if ( $typeCode == 3 ) {
268 - exec(
269 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' . $thumbWidth .
270 - ' -resize ' . $thumbWidth . ' -crop ' . $thumbWidth . 'x' .
271 - $thumbWidth . '+0+0 ' . $imageSrc . ' ' .
272 - $this->avatarUploadDirectory . '/' . $imgDest . '.png'
 313+
 314+ // Create a new image thumbnail.
 315+ if ( $typeCode == 1 ) {
 316+ imagegif( $tnImage, $imageSrc );
 317+ } elseif ( $typeCode == 2 ) {
 318+ imagejpeg( $tnImage, $imageSrc );
 319+ } elseif ( $typeCode == 3 ) {
 320+ imagepng( $tnImage, $imageSrc );
 321+ }
 322+
 323+ // Clean up.
 324+ imagedestroy( $fullImage );
 325+ imagedestroy( $tnImage );
 326+
 327+ // Copy the thumb
 328+ copy(
 329+ $imageSrc,
 330+ $this->avatarUploadDirectory . '/' . $imgDest . '.' . $ext
273331 );
274332 }
275333 }
Index: trunk/extensions/SocialProfile/UserGifts/SpecialGiftManagerLogo.php
@@ -1,5 +1,12 @@
22 <?php
3 -
 3+/**
 4+ * A special page to upload images for gifts.
 5+ * This is mostly copied from an old version of Special:Upload and changed a
 6+ * bit.
 7+ *
 8+ * @file
 9+ * @ingroup Extensions
 10+ */
411 class GiftManagerLogo extends UnlistedSpecialPage {
512
613 var $mUploadFile, $mUploadDescription, $mIgnoreWarning;
@@ -37,7 +44,11 @@
3845 }
3946
4047 $gift = Gifts::getGift( $this->gift_id );
41 - if ( $wgUser->getID() == $gift['creator_user_id'] || in_array( 'giftadmin', $wgUser->getGroups() ) ) {
 48+ if (
 49+ $wgUser->getID() == $gift['creator_user_id'] ||
 50+ in_array( 'giftadmin', $wgUser->getGroups() )
 51+ )
 52+ {
4253 return true;
4354 }
4455
@@ -205,13 +216,12 @@
206217
207218 global $wgUploadSizeWarning;
208219 if ( $wgUploadSizeWarning && ( $this->mUploadSize > $wgUploadSizeWarning ) ) {
209 -
210220 $skin = $wgUser->getSkin();
211221 $wsize = $skin->formatSize( $wgUploadSizeWarning );
212222 $asize = $skin->formatSize( $this->mUploadSize );
213223 $warning .= '<li>' . wfMsgHtml( 'large-file', $wsize, $asize ) . '</li>';
 224+ }
214225
215 - }
216226 if ( $this->mUploadSize == 0 ) {
217227 $warning .= '<li>' . wfMsg( 'emptyfile' ) . '</li>';
218228 }
@@ -229,7 +239,11 @@
230240 * Try actually saving the thing...
231241 * It will show an error form on failure.
232242 */
233 - $status = $this->saveUploadedFile( $this->mUploadSaveName, $this->mUploadTempName, strtoupper( $fullExt ) );
 243+ $status = $this->saveUploadedFile(
 244+ $this->mUploadSaveName,
 245+ $this->mUploadTempName,
 246+ strtoupper( $fullExt )
 247+ );
234248
235249 if ( $status > 0 ) {
236250 $this->showSuccess( $status );
@@ -237,38 +251,94 @@
238252 }
239253
240254 function createThumbnail( $imageSrc, $ext, $imgDest, $thumbWidth ) {
241 - global $wgImageMagickConvertCommand;
 255+ global $wgUseImageMagick, $wgImageMagickConvertCommand;
242256
243257 list( $origWidth, $origHeight, $typeCode ) = getimagesize( $imageSrc );
244258
245 - if ( $origWidth < $thumbWidth ) {
246 - $thumbWidth = $origWidth;
247 - }
248 - $thumbHeight = ( $thumbWidth * $origHeight / $origWidth );
249 - if ( $thumbHeight < $thumbWidth ) {
250 - $border = ' -bordercolor white -border 0x' . ( ( $thumbWidth - $thumbHeight ) / 2 );
251 - }
252 - if ( $typeCode == 2 ) {
253 - exec(
254 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
255 - $thumbWidth . ' -resize ' . $thumbWidth . ' -quality 100 ' .
256 - $border . ' ' . $imageSrc . ' ' .
257 - $this->avatarUploadDirectory . '/' . $imgDest . '.jpg'
 259+ if ( $wgUseImageMagick ) { // ImageMagick is enabled
 260+ if ( $origWidth < $thumbWidth ) {
 261+ $thumbWidth = $origWidth;
 262+ }
 263+ $thumbHeight = ( $thumbWidth * $origHeight / $origWidth );
 264+ if ( $thumbHeight < $thumbWidth ) {
 265+ $border = ' -bordercolor white -border 0x' . ( ( $thumbWidth - $thumbHeight ) / 2 );
 266+ }
 267+ if ( $typeCode == 2 ) {
 268+ exec(
 269+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
 270+ $thumbWidth . ' -resize ' . $thumbWidth . ' -quality 100 ' .
 271+ $border . ' ' . $imageSrc . ' ' .
 272+ $this->avatarUploadDirectory . '/' . $imgDest . '.jpg'
 273+ );
 274+ }
 275+ if ( $typeCode == 1 ) {
 276+ exec(
 277+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
 278+ $thumbWidth . ' -resize ' . $thumbWidth . ' ' . $imageSrc .
 279+ ' ' . $border . ' ' .
 280+ $this->avatarUploadDirectory . '/' . $imgDest . '.gif'
 281+ );
 282+ }
 283+ if ( $typeCode == 3 ) {
 284+ exec(
 285+ $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
 286+ $thumbWidth . ' -resize ' . $thumbWidth . ' ' . $imageSrc .
 287+ ' ' . $this->avatarUploadDirectory . '/' . $imgDest . '.png'
 288+ );
 289+ }
 290+ } else { // ImageMagick is not enabled, so fall back to PHP's GD library
 291+ // Get the image size, used in calculations later.
 292+ switch( $typeCode ) {
 293+ case '1':
 294+ $fullImage = imagecreatefromgif( $imageSrc );
 295+ $ext = 'gif';
 296+ break;
 297+ case '2':
 298+ $fullImage = imagecreatefromjpeg( $imageSrc );
 299+ $ext = 'jpg';
 300+ break;
 301+ case '3':
 302+ $fullImage = imagecreatefrompng( $imageSrc );
 303+ $ext = 'png';
 304+ break;
 305+ }
 306+
 307+ $scale = ( $thumbWidth / $origWidth );
 308+
 309+ // Create our thumbnail size, so we can resize to this, and save it.
 310+ $tnImage = imagecreatetruecolor(
 311+ $origWidth * $scale,
 312+ $origHeight * $scale
258313 );
259 - }
260 - if ( $typeCode == 1 ) {
261 - exec(
262 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
263 - $thumbWidth . ' -resize ' . $thumbWidth . ' ' . $imageSrc .
264 - ' ' . $border . ' ' .
265 - $this->avatarUploadDirectory . '/' . $imgDest . '.gif'
 314+
 315+ // Resize the image.
 316+ imagecopyresampled(
 317+ $tnImage,
 318+ $fullImage,
 319+ 0, 0, 0, 0,
 320+ $origWidth * $scale,
 321+ $origHeight * $scale,
 322+ $origWidth,
 323+ $origHeight
266324 );
267 - }
268 - if ( $typeCode == 3 ) {
269 - exec(
270 - $wgImageMagickConvertCommand . ' -size ' . $thumbWidth . 'x' .
271 - $thumbWidth . ' -resize ' . $thumbWidth . ' ' . $imageSrc .
272 - ' ' . $this->avatarUploadDirectory . '/' . $imgDest . '.png'
 325+
 326+ // Create a new image thumbnail.
 327+ if ( $typeCode == 1 ) {
 328+ imagegif( $tnImage, $imageSrc );
 329+ } elseif ( $typeCode == 2 ) {
 330+ imagejpeg( $tnImage, $imageSrc );
 331+ } elseif ( $typeCode == 3 ) {
 332+ imagepng( $tnImage, $imageSrc );
 333+ }
 334+
 335+ // Clean up.
 336+ imagedestroy( $fullImage );
 337+ imagedestroy( $tnImage );
 338+
 339+ // Copy the thumb
 340+ copy(
 341+ $imageSrc,
 342+ $this->avatarUploadDirectory . '/' . $imgDest . '.' . $ext
273343 );
274344 }
275345 }

Status & tagging log