Skip to content

Commit

Permalink
Merge pull request #3122 from movabletype/fix/MTC-29630-support-webp-…
Browse files Browse the repository at this point in the history
…on-php2-mt80

support webp for dynamic templates MTC-29630 [MT80]
  • Loading branch information
jamadam committed May 10, 2024
2 parents a4ba817 + bbd0612 commit 7c352d3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
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.

0 comments on commit 7c352d3

Please sign in to comment.