|
<?php |
|
|
|
namespace Kanboard\Core; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Thumbnail |
|
{ |
|
protected $compression = -1; |
|
protected $metadata = array(); |
|
protected $srcImage; |
|
protected $dstImage; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function createFromFile($filename) |
|
{ |
|
$self = new static(); |
|
$self->fromFile($filename); |
|
return $self; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function createFromString($blob) |
|
{ |
|
$self = new static(); |
|
$self->fromString($blob); |
|
return $self; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function fromFile($filename) |
|
{ |
|
$this->metadata = getimagesize($filename); |
|
$this->srcImage = imagecreatefromstring(file_get_contents($filename)); |
|
return $this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function fromString($blob) |
|
{ |
|
if (!function_exists('getimagesizefromstring')) { |
|
$uri = 'data://application/octet-stream;base64,' . base64_encode($blob); |
|
$this->metadata = getimagesize($uri); |
|
} else { |
|
$this->metadata = getimagesizefromstring($blob); |
|
} |
|
|
|
$this->srcImage = imagecreatefromstring($blob); |
|
return $this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function resize($width = 250, $height = 100) |
|
{ |
|
$srcWidth = $this->metadata[0]; |
|
$srcHeight = $this->metadata[1]; |
|
$dstX = 0; |
|
$dstY = 0; |
|
|
|
if ($width == 0 && $height == 0) { |
|
$width = 100; |
|
$height = 100; |
|
} |
|
|
|
if ($width > 0 && $height == 0) { |
|
$dstWidth = $width; |
|
$dstHeight = floor($srcHeight * ($width / $srcWidth)); |
|
$this->dstImage = imagecreatetruecolor($dstWidth, $dstHeight); |
|
} elseif ($width == 0 && $height > 0) { |
|
$dstWidth = floor($srcWidth * ($height / $srcHeight)); |
|
$dstHeight = $height; |
|
$this->dstImage = imagecreatetruecolor($dstWidth, $dstHeight); |
|
} else { |
|
$srcRatio = $srcWidth / $srcHeight; |
|
$resizeRatio = $width / $height; |
|
|
|
if ($srcRatio <= $resizeRatio) { |
|
$dstWidth = $width; |
|
$dstHeight = floor($srcHeight * ($width / $srcWidth)); |
|
$dstY = ($dstHeight - $height) / 2 * (-1); |
|
} else { |
|
$dstWidth = floor($srcWidth * ($height / $srcHeight)); |
|
$dstHeight = $height; |
|
$dstX = ($dstWidth - $width) / 2 * (-1); |
|
} |
|
|
|
$this->dstImage = imagecreatetruecolor($width, $height); |
|
} |
|
|
|
imagealphablending($this->dstImage, false); |
|
imagesavealpha($this->dstImage, true); |
|
|
|
imagecopyresampled($this->dstImage, $this->srcImage, (int) $dstX, (int) $dstY, 0, 0, (int) $dstWidth, (int) $dstHeight, (int) $srcWidth, (int) $srcHeight); |
|
|
|
return $this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function toFile($filename) |
|
{ |
|
imagepng($this->dstImage, $filename, $this->compression); |
|
imagedestroy($this->dstImage); |
|
imagedestroy($this->srcImage); |
|
return $this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function toString() |
|
{ |
|
ob_start(); |
|
imagepng($this->dstImage, null, $this->compression); |
|
imagedestroy($this->dstImage); |
|
imagedestroy($this->srcImage); |
|
return ob_get_clean(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public function toOutput() |
|
{ |
|
imagepng($this->dstImage, null, $this->compression); |
|
imagedestroy($this->dstImage); |
|
imagedestroy($this->srcImage); |
|
} |
|
} |
|
|