Skip to content

Commit

Permalink
support webp for dynamic templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadam committed Apr 30, 2024
1 parent af42186 commit 72fa114
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions php/lib/thumbnail_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ private function load_image () {
case 3: #PNG
$this->src_img = @imagecreatefrompng($src_file);
break;
case 18: #WEBP
$this->src_img = @imagecreatefromwebp($src_file);
break;
default: #Unsupported format
throw new MTUnsupportedImageTypeException($src_file);
}
Expand Down Expand Up @@ -233,6 +236,8 @@ private function _make_dest_name ($w, $h) {
$output = 2;
} elseif (strtolower($this->dest_type) == 'png') {
$output = 3;
} elseif (strtolower($this->dest_type) == 'webp') {
$output = 18;
} else {
$output = $this->src_type;
}
Expand All @@ -246,6 +251,9 @@ private function _make_dest_name ($w, $h) {
case 3:
$ext = '.png';
break;
case 18:
$ext = '.webp';
break;
default:
$ext = image_type_to_extension($output);
}
Expand Down Expand Up @@ -395,6 +403,8 @@ public function get_thumbnail ($args = null) {
$output = 2;
elseif ( strtolower($this->dest_type) == 'png' )
$output = 3;
elseif ( strtolower($this->dest_type) == 'webp' )
$output = 18;
else
$output = $this->src_type;
}
Expand All @@ -408,6 +418,9 @@ public function get_thumbnail ($args = null) {
case 3: #PNG
imagepng($this->dest_img, $dest_file);
break;
case 18: #PNG
imagewebp($this->dest_img, $dest_file);
break;
}
@imagedestroy($this->dest_img);
}
Expand Down
26 changes: 26 additions & 0 deletions php/tests/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,32 @@ public function testFileinfo() {
$this->assertEquals('1', $cat->id);
$this->assertEquals('1', $cat->category_id);
}

public function testThumbnail() {

$tempdir = sys_get_temp_dir(). DIRECTORY_SEPARATOR. 'phpunit_'. getmypid(). rand(1000, 9999). '/';

require_once('thumbnail_lib.php');
$thumb1 = new Thumbnail('t/images/test.jpg');
$this->assertEquals(true, $thumb1->get_thumbnail(['dest' => $tempdir. 'test.jpg']));
$this->assertEquals(640, $thumb1->width());
$this->assertEquals(480, $thumb1->height());

$thumb2 = new Thumbnail('t/images/test.gif');
$this->assertEquals(true, $thumb2->get_thumbnail(['dest' => $tempdir. 'test.gif']));
$this->assertEquals(400, $thumb2->width());
$this->assertEquals(300, $thumb2->height());

$thumb3 = new Thumbnail('t/images/test.png');
$this->assertEquals(true, $thumb3->get_thumbnail(['dest' => $tempdir. 'test.png']));
$this->assertEquals(150, $thumb3->width());
$this->assertEquals(150, $thumb3->height());

$thumb3 = new Thumbnail('t/images/test.webp');
$this->assertEquals(true, $thumb3->get_thumbnail(['dest' => $tempdir. 'test.webp']));
$this->assertEquals(150, $thumb3->width());
$this->assertEquals(150, $thumb3->height());
}
}

class MyCaptchaProvider implements CaptchaProvider {
Expand Down
Binary file added t/images/test.webp
Binary file not shown.

0 comments on commit 72fa114

Please sign in to comment.