Improved Resize tests
This commit is contained in:
parent
d1feb16370
commit
4fded0e2f6
|
@ -131,35 +131,37 @@ class Resize
|
|||
break;
|
||||
|
||||
case 'webp':
|
||||
$this->image->setImageFormat('webp');
|
||||
try {
|
||||
$this->image->setImageFormat('webp');
|
||||
} catch (\ImagickException $th) {
|
||||
$signature = $this->image->getImageSignature();
|
||||
$temp = '/tmp/temp-'.$signature.'.'.\strtolower($this->image->getImageFormat());
|
||||
$output = '/tmp/output-'.$signature.'.webp';
|
||||
|
||||
// $signature = $this->image->getImageSignature();
|
||||
// $temp = '/tmp/temp-'.$signature.'.'.\strtolower($this->image->getImageFormat());
|
||||
// $output = '/tmp/output-'.$signature.'.webp';
|
||||
// save temp
|
||||
$this->image->writeImages($temp, true);
|
||||
|
||||
// // save temp
|
||||
// $this->image->writeImages($temp, true);
|
||||
// convert temp
|
||||
\exec("cwebp -quiet -metadata none -q $quality $temp -o $output");
|
||||
|
||||
// // convert temp
|
||||
// \exec("cwebp -quiet -metadata none -q $quality $temp -o $output");
|
||||
$data = \file_get_contents($output);
|
||||
|
||||
// $data = \file_get_contents($output);
|
||||
//load webp
|
||||
if (empty($path)) {
|
||||
return $data;
|
||||
} else {
|
||||
\file_put_contents($path, $data, LOCK_EX);
|
||||
}
|
||||
|
||||
// //load webp
|
||||
// if (empty($path)) {
|
||||
// return $data;
|
||||
// } else {
|
||||
// \file_put_contents($path, $data, LOCK_EX);
|
||||
// }
|
||||
$this->image->clear();
|
||||
$this->image->destroy();
|
||||
|
||||
// $this->image->clear();
|
||||
// $this->image->destroy();
|
||||
//delete webp
|
||||
\unlink($output);
|
||||
\unlink($temp);
|
||||
|
||||
// //delete webp
|
||||
// \unlink($output);
|
||||
// \unlink($temp);
|
||||
|
||||
// return;
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
@ -215,4 +217,4 @@ class Resize
|
|||
|
||||
return $newHeight;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,14 +19,18 @@ class ResizeTest extends TestCase
|
|||
{
|
||||
$resize = new Resize(\file_get_contents(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'));
|
||||
$target = __DIR__.'/100x100.jpg';
|
||||
$original = __DIR__.'/../../resources/resize/100x100.jpg';
|
||||
|
||||
$resize->crop(100, 100);
|
||||
|
||||
$resize->save($target, 'jpg', 100);
|
||||
|
||||
$this->assertEquals(is_readable($target), true);
|
||||
$this->assertEquals(\md5(\file_get_contents($target)), \md5(\file_get_contents($original)));
|
||||
$this->assertEquals(\is_readable($target), true);
|
||||
$this->assertNotEmpty(\md5(\file_get_contents($target)));
|
||||
|
||||
$image = new \Imagick($target);
|
||||
$this->assertEquals(100, $image->getImageWidth());
|
||||
$this->assertEquals(100, $image->getImageHeight());
|
||||
$this->assertEquals('JPEG', $image->getImageFormat());
|
||||
|
||||
\unlink($target);
|
||||
}
|
||||
|
@ -35,14 +39,18 @@ class ResizeTest extends TestCase
|
|||
{
|
||||
$resize = new Resize(\file_get_contents(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'));
|
||||
$target = __DIR__.'/100x400.jpg';
|
||||
$original = __DIR__.'/../../resources/resize/100x400.jpg';
|
||||
|
||||
$resize->crop(100, 400);
|
||||
|
||||
$resize->save($target, 'jpg', 100);
|
||||
|
||||
$this->assertEquals(is_readable($target), true);
|
||||
$this->assertEquals(\md5(\file_get_contents($target)), \md5(\file_get_contents($original)));
|
||||
$this->assertEquals(\is_readable($target), true);
|
||||
$this->assertNotEmpty(\md5(\file_get_contents($target)));
|
||||
|
||||
$image = new \Imagick($target);
|
||||
$this->assertEquals(100, $image->getImageWidth());
|
||||
$this->assertEquals(400, $image->getImageHeight());
|
||||
$this->assertEquals('JPEG', $image->getImageFormat());
|
||||
|
||||
\unlink($target);
|
||||
}
|
||||
|
@ -51,14 +59,18 @@ class ResizeTest extends TestCase
|
|||
{
|
||||
$resize = new Resize(\file_get_contents(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'));
|
||||
$target = __DIR__.'/400x100.jpg';
|
||||
$original = __DIR__.'/../../resources/resize/400x100.jpg';
|
||||
|
||||
$resize->crop(400, 100);
|
||||
|
||||
$resize->save($target, 'jpg', 100);
|
||||
|
||||
$this->assertEquals(is_readable($target), true);
|
||||
$this->assertEquals(\md5(\file_get_contents($target)), \md5(\file_get_contents($original)));
|
||||
$this->assertEquals(\is_readable($target), true);
|
||||
$this->assertNotEmpty(\md5(\file_get_contents($target)));
|
||||
|
||||
$image = new \Imagick($target);
|
||||
$this->assertEquals(400, $image->getImageWidth());
|
||||
$this->assertEquals(100, $image->getImageHeight());
|
||||
$this->assertEquals('JPEG', $image->getImageFormat());
|
||||
|
||||
\unlink($target);
|
||||
}
|
||||
|
@ -73,8 +85,14 @@ class ResizeTest extends TestCase
|
|||
|
||||
$resize->save($target, 'webp', 100);
|
||||
|
||||
$this->assertEquals(is_readable($target), true);
|
||||
$this->assertEquals(\md5(\file_get_contents($target)), \md5(\file_get_contents($original)));
|
||||
$this->assertEquals(\is_readable($target), true);
|
||||
$this->assertNotEmpty(\md5(\file_get_contents($target)));
|
||||
|
||||
$image = new \Imagick($target);
|
||||
|
||||
$this->assertEquals(100, $image->getImageWidth());
|
||||
$this->assertEquals(100, $image->getImageHeight());
|
||||
$this->assertTrue(in_array($image->getImageFormat(), ['PAM', 'WEBP']));
|
||||
|
||||
\unlink($target);
|
||||
}
|
||||
|
@ -89,9 +107,16 @@ class ResizeTest extends TestCase
|
|||
|
||||
$resize->save($target, 'png', 100);
|
||||
|
||||
$this->assertEquals(is_readable($target), true);
|
||||
$this->assertEquals(\filesize($target), \filesize($original));
|
||||
$this->assertEquals(\is_readable($target), true);
|
||||
$this->assertGreaterThan(15000, \filesize($target));
|
||||
$this->assertLessThan(30000, \filesize($target));
|
||||
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
|
||||
$this->assertNotEmpty(\md5(\file_get_contents($target)));
|
||||
|
||||
$image = new \Imagick($target);
|
||||
$this->assertEquals(100, $image->getImageWidth());
|
||||
$this->assertEquals(100, $image->getImageHeight());
|
||||
$this->assertEquals('PNG', $image->getImageFormat());
|
||||
|
||||
\unlink($target);
|
||||
}
|
||||
|
@ -106,9 +131,16 @@ class ResizeTest extends TestCase
|
|||
|
||||
$resize->save($target, 'jpg', 10);
|
||||
|
||||
$this->assertEquals(is_readable($target), true);
|
||||
$this->assertEquals(\filesize($target), \filesize($original));
|
||||
$this->assertEquals(\is_readable($target), true);
|
||||
$this->assertGreaterThan(500, \filesize($target));
|
||||
$this->assertLessThan(2000, \filesize($target));
|
||||
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
|
||||
$this->assertNotEmpty(\md5(\file_get_contents($target)));
|
||||
|
||||
$image = new \Imagick($target);
|
||||
$this->assertEquals(100, $image->getImageWidth());
|
||||
$this->assertEquals(100, $image->getImageHeight());
|
||||
$this->assertEquals('JPEG', $image->getImageFormat());
|
||||
|
||||
\unlink($target);
|
||||
}
|
||||
|
@ -123,23 +155,16 @@ class ResizeTest extends TestCase
|
|||
|
||||
$resize->save($target, 'gif', 100);
|
||||
|
||||
$this->assertEquals(is_readable($target), true);
|
||||
$this->assertEquals(\filesize($target), \filesize($original));
|
||||
$this->assertEquals(\is_readable($target), true);
|
||||
$this->assertGreaterThan(400000, \filesize($target));
|
||||
$this->assertLessThan(800000, \filesize($target));
|
||||
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
|
||||
$this->assertNotEmpty(\md5(\file_get_contents($target)));
|
||||
|
||||
$image = new \Imagick($target);
|
||||
$this->assertEquals(100, $image->getImageWidth());
|
||||
$this->assertEquals(100, $image->getImageHeight());
|
||||
$this->assertEquals('GIF', $image->getImageFormat());
|
||||
\unlink($target);
|
||||
}
|
||||
|
||||
public function testCrop100x100Output()
|
||||
{
|
||||
$resize = new Resize(\file_get_contents(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'));
|
||||
$original = __DIR__.'/../../resources/resize/100x100.jpg';
|
||||
|
||||
$resize->crop(100, 100);
|
||||
|
||||
$result = $resize->output('jpg', 100);
|
||||
|
||||
$this->assertEquals(!empty($result), true);
|
||||
$this->assertEquals(\md5($result), \md5(\file_get_contents($original)));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue