btongminh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/62233


Change subject: (bug 47311) Add vipsthumbnail support
......................................................................

(bug 47311) Add vipsthumbnail support

Bug: 47311
Change-Id: Ie92da659c78421163ce2a6c2bf774a767564460a
---
M VipsScaler.php
M VipsScaler_body.php
2 files changed, 59 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VipsScaler 
refs/changes/33/62233/1

diff --git a/VipsScaler.php b/VipsScaler.php
index 2617224..1c12770 100644
--- a/VipsScaler.php
+++ b/VipsScaler.php
@@ -41,6 +41,7 @@
 
 # Download vips from http://www.vips.ecs.soton.ac.uk/
 $wgVipsCommand = 'vips';
+$wgVipsthumbnailCommand = 'vipsthumbnail';
 
 /* Options and conditions for images to be scaled with this scaler.
  * Set to an array of arrays. The inner array contains a condition array, which
@@ -56,6 +57,7 @@
  * - convolution: Apply specified convolution matrix
  * - setcomment: Add an exif comment specifying the source of the file.
  *   Requires $wgExiv2Command to be set properly.
+ * - vipsthumbnail: Use vipsthumbnail instead of vips
  */
 $wgVipsOptions = array(
        # Sharpen jpeg files which are shrunk more than 1.2
diff --git a/VipsScaler_body.php b/VipsScaler_body.php
index 69f31b1..5316849 100644
--- a/VipsScaler_body.php
+++ b/VipsScaler_body.php
@@ -69,6 +69,7 @@
                if ( count( $vipsCommands ) == 0 ) {
                        return true;
                }
+               wfDebug( __METHOD__ . ': Running ' . count( $vipsCommands ) . " 
vips commands\n" );
 
                # Execute the commands
                foreach ( $vipsCommands as $i => $command ) {
@@ -94,6 +95,12 @@
                        }
                }
 
+               if ( end( $vipsCommands )->getOutput() !== $params['dstPath'] ) 
{
+                       wfDebug( __METHOD__ . ": renaming " . end( 
$vipsCommands )->getOutput() .
+                               ' to ' . $params['dstPath'] . "\n" );
+                       rename( end( $vipsCommands )->getOutput(), 
$params['dstPath'] );
+               }
+
                # Set comment
                if ( !empty( $options['setcomment'] ) && !empty( 
$params['comment'] ) ) {
                        self::setJpegComment( $params['dstPath'], 
$params['comment'] );
@@ -115,7 +122,7 @@
         * @return array
         */
        public static function makeCommands( $handler, $file, $params, $options 
) {
-               global $wgVipsCommand;
+               global $wgVipsCommand, $wgVipsthumbnailCommand;
                $commands = array();
 
                # Get the proper im_XXX2vips handler
@@ -133,7 +140,13 @@
                $rotation = 360 - $handler->getRotation( $file );
 
                wfDebug( __METHOD__ . " rotating '{$file->getName()}' by 
{$rotation}°\n" );
-               if ( empty( $options['bilinear'] ) ) {
+               if ( !empty( $options['vipsthumbnail'] ) ) {
+                       # Use vipsthumbnail
+                       $args = array( '--size=' . max( 
$params['physicalWidth'], $params['physicalHeight'] ),
+                               '--interpolator=' . ( empty( 
$options['bilinear'] ) ? 'bicubic' : 'bilinear' ) );
+                       wfDebug( __METHOD__ . ' vipsthumbnail args: ' . 
implode( ' ', $args ) . "\n" );
+                       $commands[] = new VipsthumbnailCommand( 
$wgVipsthumbnailCommand, $args );
+               } elseif ( empty( $options['bilinear'] ) ) {
                        # Calculate shrink factors. Offsetting by a small 
amount is required
                        # because of rounding down of the target size by VIPS. 
See 25990#c7
                        #
@@ -174,13 +187,15 @@
                                array( 'im_resize_linear', $dstWidth, 
$dstHeight ) );
                }
 
-               if ( !empty( $options['sharpen'] ) ) {
-                       $options['convolution'] = self::makeSharpenMatrix( 
$options['sharpen'] );
-               }
+               if ( empty( $options['vipsthumbnail'] ) ) {
+                       if ( !empty( $options['sharpen'] ) ) {
+                               $options['convolution'] = 
self::makeSharpenMatrix( $options['sharpen'] );
+                       }
 
-               if ( !empty( $options['convolution'] ) ) {
-                       $commands[] = new VipsConvolution( $wgVipsCommand,
-                               array( 'im_convf', $options['convolution'] ) );
+                       if ( !empty( $options['convolution'] ) ) {
+                               $commands[] = new VipsConvolution( 
$wgVipsCommand,
+                                       array( 'im_convf', 
$options['convolution'] ) );
+                       }
                }
 
                # Rotation
@@ -441,6 +456,40 @@
        }
 
 }
+class VipsthumbnailCommand extends VipsCommand {
+       public function getOutput() {
+               return dirname( $this->input ) . DIRECTORY_SEPARATOR .
+                       'tn_' . basename( $this->input );
+       }
+       /**
+        * Call the vipsthumbnail binary with varargs and returns the return 
value.
+        *
+        * @return int Return value
+        */
+       public function execute() {
+               # Build and escape the command string
+               $env = array( 'IM_CONCURRENCY' => '1' );
+               $limits = array( 'filesize' => 409600 );
+               $cmd = wfEscapeShellArg( $this->vips );
+
+               foreach ( $this->args as $arg ) {
+                       $cmd .= ' ' . wfEscapeShellArg( $arg );
+               }
+
+               $cmd .= ' ' . wfEscapeShellArg( $this->input ) . ' 2>&1';
+
+               # Execute
+               $retval = 0;
+               $this->err = wfShellExec( $cmd, $retval, $env, $limits );
+
+               # Cleanup temp file
+               if ( $this->removeInput ) {
+                       unlink( $this->input );
+               }
+
+               return $retval;
+       }
+}
 
 /**
  * A wrapper class around im_conv because that command expects a a convolution

-- 
To view, visit https://gerrit.wikimedia.org/r/62233
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie92da659c78421163ce2a6c2bf774a767564460a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VipsScaler
Gerrit-Branch: master
Gerrit-Owner: btongminh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to