Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support webp for dynamic templates MTC-29630 [MT80] #3122

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 28 additions & 15 deletions php/lib/thumbnail_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,18 @@ private function load_image () {
list($this->src_w, $this->src_h, $this->src_type, $this->src_attr) = getimagesize($src_file);

switch($this->src_type) {
case 1: #GIF
case IMAGETYPE_GIF:
$this->src_img = @imagecreatefromgif($src_file);
break;
case 2: #JPEG
case IMAGETYPE_JPEG:
$this->src_img = @imagecreatefromjpeg($src_file);
break;
case 3: #PNG
case IMAGETYPE_PNG:
$this->src_img = @imagecreatefrompng($src_file);
break;
case IMAGETYPE_WEBP:
$this->src_img = @imagecreatefromwebp($src_file);
break;
default: #Unsupported format
throw new MTUnsupportedImageTypeException($src_file);
}
Expand Down Expand Up @@ -228,24 +231,29 @@ private function _make_dest_name ($w, $h) {
if ($this->dest_type == 'auto') {
$output = $this->src_type;
} elseif (strtolower($this->dest_type) == 'gif') {
$output = 1;
$output = IMAGETYPE_GIF;
} elseif (strtolower($this->dest_type) == 'jpeg') {
$output = 2;
$output = IMAGETYPE_JPEG;
} elseif (strtolower($this->dest_type) == 'png') {
$output = 3;
$output = IMAGETYPE_PNG;
} elseif (strtolower($this->dest_type) == 'webp') {
$output = IMAGETYPE_WEBP;
} else {
$output = $this->src_type;
}
switch($output) {
case 1:
case IMAGETYPE_GIF:
$ext = '.gif';
break;
case 2:
case IMAGETYPE_JPEG:
$ext = '.jpg';
break;
case 3:
case IMAGETYPE_PNG:
$ext = '.png';
break;
case IMAGETYPE_WEBP:
$ext = '.webp';
break;
default:
$ext = image_type_to_extension($output);
}
Expand Down Expand Up @@ -390,24 +398,29 @@ public function get_thumbnail ($args = null) {
$output = $this->src_type;
if ($this->dest_type != 'auto') {
if ( strtolower($this->dest_type) == 'gif' )
$output = 1;
$output = IMAGETYPE_GIF;
elseif ( strtolower($this->dest_type) == 'jpeg' )
$output = 2;
$output = IMAGETYPE_JPEG;
elseif ( strtolower($this->dest_type) == 'png' )
$output = 3;
$output = IMAGETYPE_PNG;
elseif ( strtolower($this->dest_type) == 'webp' )
$output = IMAGETYPE_WEBP;
else
$output = $this->src_type;
}
switch($output) {
case 1: #GIF
case IMAGETYPE_GIF:
imagegif($this->dest_img, $dest_file);
break;
case 2: #JPEG
case IMAGETYPE_JPEG:
imagejpeg($this->dest_img, $dest_file);
break;
case 3: #PNG
case IMAGETYPE_PNG:
imagepng($this->dest_img, $dest_file);
break;
case IMAGETYPE_WEBP:
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.