Skip to content

Commit

Permalink
Merge pull request #222 from ResponsiveImagesCG/dev
Browse files Browse the repository at this point in the history
Version 3.0.0
  • Loading branch information
tevko committed Nov 6, 2015
2 parents de4a841 + c38194a commit 602a683
Show file tree
Hide file tree
Showing 11 changed files with 1,141 additions and 708 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ php:
- 7.0

env:
- WP_VERSION=latest WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=1
- WP_VERSION=trunk WP_MULTISITE=0
- WP_VERSION=trunk WP_MULTISITE=1
- WP_VERSION=4.3 WP_MULTISITE=0
- WP_VERSION=4.3 WP_MULTISITE=1
- WP_VERSION=4.2 WP_MULTISITE=0
- WP_VERSION=4.2 WP_MULTISITE=1
- WP_VERSION=4.1 WP_MULTISITE=0
Expand Down
76 changes: 59 additions & 17 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,56 @@ WP_VERSION=${5-latest}
WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}

download() {
if [ `which curl` ]; then
curl -s "$1" > "$2";
elif [ `which wget` ]; then
wget -nv -O "$2" "$1"
fi
}

if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
WP_TESTS_TAG="tags/$WP_VERSION"
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
WP_TESTS_TAG="trunk"
else
# http serves a single offer, whereas https serves multiple. we only want one
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
if [[ -z "$LATEST_VERSION" ]]; then
echo "Latest WordPress version could not be found"
exit 1
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi

set -ex

install_wp() {

if [ -d $WP_CORE_DIR ]; then
return;
fi

mkdir -p $WP_CORE_DIR

if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p /tmp/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
fi

wget -nv -O /tmp/wordpress.tar.gz https://wordpress.org/${ARCHIVE_NAME}.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR

wget -nv -O $WP_CORE_DIR/wp-content/db.php https://raw.github.com/markoheijnen/wp-mysqli/master/db.php
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

install_test_suite() {
Expand All @@ -39,17 +74,24 @@ install_test_suite() {
local ioption='-i'
fi

# set up testing suite
mkdir -p $WP_TESTS_DIR
# set up testing suite if it doesn't yet exist
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
fi

cd $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/trunk/tests/phpunit/includes/

wget -nv -O wp-tests-config.php https://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
fi

}

install_db() {
Expand Down
63 changes: 34 additions & 29 deletions class-respimg.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* hacked up version of php-respimg <https://github.com/nwtn/php-respimg>
* Hacked up version of php-respimg: https://github.com/nwtn/php-respimg
*
* @package wp-respimg
* @version 0.0.1
Expand Down Expand Up @@ -35,9 +35,10 @@ class Respimg extends Imagick {
*
* @access public
*
* @param integer $columns The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
* @param integer $rows The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
* @param bool $optim Whether you intend to perform optimization on the resulting image. Note that setting this to `true` doesn’t actually perform any optimization.
* @param integer $columns The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
* @param integer $rows The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
* @param bool $optim Whether you intend to perform optimization on the resulting image.
* Note that setting this to 'true' doesn't actually perform any optimization.
*/

public function smartResize( $columns, $rows, $optim = false ) {
Expand All @@ -61,41 +62,41 @@ public function smartResize( $columns, $rows, $optim = false ) {
if ( ! $optim ) {
$this->stripImage();
}

}


/**
* Changes the size of an image to the given dimensions and removes any associated profiles.
*
* `thumbnailImage` changes the size of an image to the given dimensions and
* removes any associated profiles. The goal is to produce small low cost
* removes any associated profiles. The goal is to produce small low cost
* thumbnail images suited for display on the Web.
*
* With the original Imagick thumbnailImage implementation, there is no way to choose a
* resampling filter. This class recreates Imagick’s C implementation and adds this
* additional feature.
*
* Note: <https://github.com/mkoppanen/imagick/issues/90> has been filed for this issue.
* Note: https://github.com/mkoppanen/imagick/issues/90 has been filed for this issue.
*
* @access public
*
* @param integer $columns The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
* @param integer $rows The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
* @param bool $bestfit Treat $columns and $rows as a bounding box in which to fit the image.
* @param bool $fill Fill in the bounding box with the background colour.
* @param integer $filter The resampling filter to use. Refer to the list of filter constants at <http://php.net/manual/en/imagick.constants.php>.
* @param integer $columns The number of columns in the output image. 0 = maintain aspect ratio based on $rows.
* @param integer $rows The number of rows in the output image. 0 = maintain aspect ratio based on $columns.
* @param bool $bestfit Treat $columns and $rows as a bounding box in which to fit the image.
* @param bool $fill Fill in the bounding box with the background colour.
* @param integer $filter The resampling filter to use. Refer to the list of filter constants at <http://php.net/manual/en/imagick.constants.php>.
*
* @return bool Indicates whether the operation was performed successfully.
* @return bool Indicates whether the operation was performed successfully.
*/

public function thumbnailImage( $columns, $rows, $bestfit = false, $fill = false, $filter = Imagick::FILTER_TRIANGLE ) {

// sample factor; defined in original ImageMagick thumbnailImage function
// the scale to which the image should be resized using the `sample` function
/*
* Sample factor; defined in original ImageMagick thumbnailImage function
* the scale to which the image should be resized using the 'sample' function.
*/
$SampleFactor = 5;

// filter whitelist
// Filter whitelist.
$filters = array(
Imagick::FILTER_POINT,
Imagick::FILTER_BOX,
Expand All @@ -114,23 +115,23 @@ public function thumbnailImage( $columns, $rows, $bestfit = false, $fill = false
Imagick::FILTER_SINC
);

// Parse parameters given to function
// Parse parameters given to function.
$columns = (double) $columns;
$rows = (double) $rows;
$bestfit = (bool) $bestfit;
$fill = (bool) $fill;

// We can’t resize to (0,0)
// We can’t resize to (0,0).
if ( $rows < 1 && $columns < 1 ) {
return false;
}

// Set a default filter if an acceptable one wasn’t passed
// Set a default filter if an acceptable one wasn’t passed.
if ( ! in_array( $filter, $filters ) ) {
$filter = Imagick::FILTER_TRIANGLE;
}

// figure out the output width and height
// Figure out the output width and height.
$width = (double) $this->getImageWidth();
$height = (double) $this->getImageHeight();
$new_width = $columns;
Expand All @@ -144,8 +145,10 @@ public function thumbnailImage( $columns, $rows, $bestfit = false, $fill = false
$new_width = round( $y_factor * $width );
}

// if bestfit is true, the new_width/new_height of the image will be different than
// the columns/rows parameters; those will define a bounding box in which the image will be fit
/*
* If bestfit is true, the new_width/new_height of the image will be different than
* the columns/rows parameters; those will define a bounding box in which the image will be fit.
*/
if ( $bestfit && $x_factor > $y_factor ) {
$x_factor = $y_factor;
$new_width = round( $y_factor * $width );
Expand All @@ -160,8 +163,10 @@ public function thumbnailImage( $columns, $rows, $bestfit = false, $fill = false
$new_height = 1;
}

// if we’re resizing the image to more than about 1/3 it’s original size
// then just use the resize function
/*
* If we’re resizing the image to more than about 1/3 it’s original size
* then just use the resize function.
*/
if ( ( $x_factor * $y_factor ) > 0.1 ) {
$this->resizeImage( $new_width, $new_height, $filter, 1 );

Expand Down Expand Up @@ -215,8 +220,10 @@ public function thumbnailImage( $columns, $rows, $bestfit = false, $fill = false
$this->setImageProperty( 'Thumb::Document::Pages', '' );
}

// In case user wants to fill use extent for it rather than creating a new canvas
// …fill out the bounding box
/*
* In case user wants to fill use extent for it rather than creating a new canvas
* fill out the bounding box.
*/
if ( $bestfit && $fill && ( $new_width != $columns || $new_height != $rows ) ) {
$extent_x = 0;
$extent_y = 0;
Expand All @@ -233,7 +240,5 @@ public function thumbnailImage( $columns, $rows, $bestfit = false, $fill = false
}

return true;

}

}
18 changes: 11 additions & 7 deletions class-wp-image-editor-respimg.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );

/**
* WordPress Image Editor Class for Image Manipulation through Imagick PHP Module with php-respimg
* WordPress Image Editor Class for Image Manipulation through Imagick PHP Module with php-respimg.
*
* @package wp-respimg
* @uses WP_Image_Editor_Imagick Extends class
Expand All @@ -34,8 +34,11 @@ public function load() {
return new WP_Error( 'error_loading_image', __('File doesn&#8217;t exist?'), $this->file );
}

/** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
// Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits
/*
* This filter is documented in wp-includes/class-wp-image-editor-imagick.php
*
* Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits.
*/
@ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );

try {
Expand All @@ -44,7 +47,7 @@ public function load() {
if ( ! $this->image->valid() ) {
return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file);
}
// Select the first frame to handle animated images properly
// Select the first frame to handle animated images properly.
if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) {
$this->image->setIteratorIndex(0);
}
Expand Down Expand Up @@ -200,8 +203,10 @@ public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = nu
$this->image->setImagePage( $src_w, $src_h, 0, 0);

if ( $dst_w || $dst_h ) {
// If destination width/height isn't specified, use same as
// width/height from source.
/*
* If destination width/height isn't specified, use same as
* width/height from source.
*/
if ( ! $dst_w ) {
$dst_w = $src_w;
}
Expand All @@ -222,5 +227,4 @@ public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = nu
}
return $this->update_size();
}

}
Loading

0 comments on commit 602a683

Please sign in to comment.