Skip to content

Commit b192648

Browse files
authored
fix: dpr srcset when only h param (#76)
* fix: address issue where h only srcsets did not respect ar * test: add h based srcset tests and comment out deprecated tests * test: remove comments and add testHeightIncludesDPRParam
1 parent fc5b506 commit b192648

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

src/Imgix/UrlBuilder.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ private function isDpr($params) {
162162
$hasHeight = array_key_exists('h', $params) ? $params['h'] : NULL;
163163
$hasAspectRatio = array_key_exists('ar', $params) ? $params['ar'] : NULL;
164164

165-
// If `params` have a width param or _both_ height and aspect
166-
// ratio parameters then the srcset to be constructed with
167-
// these params _is dpr based
168-
return $hasWidth || ($hasHeight && $hasAspectRatio);
165+
// If `params` have a width or height parameter then the
166+
// srcset to be constructed with these params _is dpr based
167+
return $hasWidth || $hasHeight;
169168
}
170169

171170
private function createDPRSrcSet($path, $params, $disableVariableQuality=false) {

tests/Imgix/Tests/UrlBuilderTest.php

+10-21
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public function testGivenWidthSignsURLs() {
398398

399399
public function testGivenHeightSrcsetGeneratesPairs() {
400400
$srcset = $this->srcsetBuilder(array("h"=>300));
401-
$expectedNumberOfPairs = 31;
401+
$expectedNumberOfPairs = 5;
402402
$this->assertEquals($expectedNumberOfPairs, count(explode(",", $srcset)));
403403
}
404404

@@ -411,34 +411,23 @@ public function testGivenHeightRespectsParameter() {
411411
}
412412
}
413413

414-
public function testGivenHeightSrcsetPairsWithinBounds() {
414+
public function testHeightBasedSrcsetHasDprValues() {
415415
$srcset = $this->srcsetBuilder(array("h"=>300));
416416
$srclist = explode(",", $srcset);
417417

418-
$minParsed = explode(" ", $srclist[0])[1];
419-
$maxParsed = explode(" ", $srclist[count($srclist)-1])[1];
420-
$min = $this->parseWidth($minParsed);
421-
$max = $this->parseWidth($maxParsed);
422-
423-
$this->assertGreaterThanOrEqual(100, $min);
424-
$this->assertLessThanOrEqual(8192, $max);
418+
foreach ($srclist as $i=>$src) {
419+
$dpr = explode(" ", $src)[1];
420+
$this->assertMatchesRegularExpression("/x/", $dpr);
421+
}
425422
}
426423

427-
public function testGivenHeightSrcsetIteratesEighteenPercent() {
428-
$incrementAllowed = .18;
424+
public function testHeightIncludesDPRParam() {
429425
$srcset = $this->srcsetBuilder(array("h"=>300));
430426
$srclist = explode(",", $srcset);
431427

432-
$widths = array_map(function ($src) {
433-
return $this->parseWidth(explode(" ", $src)[1]);
434-
}, $srclist);
435-
436-
$prev = $widths[0];
437-
$size = count($widths);
438-
for ($i = 1; $i < $size; $i++) {
439-
$width = $widths[$i];
440-
$this->assertLessThan((1 + $incrementAllowed), ($width / $prev));
441-
$prev = $width;
428+
foreach ($srclist as $i=>$src) {
429+
$dpr = explode(" ", $src)[0];
430+
$this->assertMatchesRegularExpression("/dpr=/", $dpr);
442431
}
443432
}
444433

0 commit comments

Comments
 (0)