/** * Resize an image while maintaining the aspect ratio. * * Note that the returned data will most likely not be `$width`x`$height`, * but as close as possible while keeping the original aspect ratio. * * @param Image $image The input image * @param integer $width The maximum width * @param integer $height The maximum height * * @return void */ public function resizeKeepAspect(Image $image, $width, $height) { $originalWidth = $image->getWidth(); $originalHeight = $image->getHeight(); if ($originalWidth >= $originalHeight) { // Image is larger horizontally $scale = $width / $originalWidth; $height = $originalHeight * $scale; } else { // Image is larger vertically $scale = $height / $originalHeight; $width = $originalWidth * $scale; } return $this->resize($image, $width, $height); }
public function testItWillReturnItsContents() { $types = array('png' => 'png', 'jpg' => 'jpeg', 'gif' => 'gif'); foreach ($types as $ext => $command) { $image = new GDImage(__DIR__ . "/fixtures/image." . $ext); $in = "imagecreatefrom" . $command; $out = "image" . $command; // This sucks. I hate it. $actual = $in(__DIR__ . "/fixtures/image." . $ext); ob_start(); $out($actual); $expected = ob_get_clean(); $this->assertEquals($expected, $image->getContents()); } }