Skip to content

Commit

Permalink
Merge pull request square#1502 from square/jw/webp
Browse files Browse the repository at this point in the history
Request WebP from Thumbor when OS supports it.
  • Loading branch information
JakeWharton authored Oct 9, 2016
2 parents 303366b + 0115eca commit a1ba906
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.squareup.picasso.pollexor;

import android.net.Uri;
import android.os.Build;
import com.squareup.picasso.Request;
import com.squareup.pollexor.Thumbor;
import com.squareup.pollexor.ThumborUrlBuilder;
import com.squareup.pollexor.ThumborUrlBuilder.ImageFormat;

import static com.squareup.picasso.Picasso.RequestTransformer;
import static com.squareup.pollexor.ThumborUrlBuilder.format;

/**
* A {@link RequestTransformer} that changes requests to use {@link Thumbor} for some remote
Expand Down Expand Up @@ -64,6 +67,11 @@ public PollexorRequestTransformer(Thumbor thumbor) {
newRequest.clearCenterInside();
}

// If the Android version is modern enough use WebP for downloading.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
urlBuilder.filter(format(ImageFormat.WEBP));
}

// Update the request with the completed Thumbor URL.
newRequest.setUri(Uri.parse(urlBuilder.toUrl()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
import android.net.Uri;
import com.squareup.picasso.Request;
import com.squareup.pollexor.Thumbor;
import com.squareup.pollexor.ThumborUrlBuilder;
import com.squareup.pollexor.ThumborUrlBuilder.ImageFormat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static com.squareup.picasso.Picasso.RequestTransformer;
import static com.squareup.pollexor.ThumborUrlBuilder.format;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.robolectric.annotation.Config.NONE;

@RunWith(RobolectricGradleTestRunner.class)
@Config(
sdk = 18,
sdk = 17,
manifest = NONE,
constants = BuildConfig.class
)
Expand Down Expand Up @@ -55,6 +58,21 @@ public class PollexorRequestTransformerTest {
assertThat(output.uri.toString()).isEqualTo(expected);
}

@Config(sdk = 18)
@Test public void simpleResizeOnJbMr2UsesWebP() {
Request input = new Request.Builder(IMAGE_URI).resize(50, 50).build();
Request output = transformer.transformRequest(input);
assertThat(output).isNotSameAs(input);
assertThat(output.hasSize()).isFalse();

String expected = Thumbor.create(HOST)
.buildImage(IMAGE)
.resize(50, 50)
.filter(format(ImageFormat.WEBP))
.toUrl();
assertThat(output.uri.toString()).isEqualTo(expected);
}

@Test public void simpleResizeWithCenterCrop() {
Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerCrop().build();
Request output = transformer.transformRequest(input);
Expand Down

0 comments on commit a1ba906

Please sign in to comment.