mirror of
https://we.phorge.it/source/phorge.git
synced 2025-01-10 23:01:04 +01:00
Prefer PNG for thumbnails of PNG
Summary: I am not sure if anybody but me can see it but JPG used for non-photo images is extremely ugly: {F9506, size=full} {F9507, size=full} Test Plan: https://secure.phabricator.com/file/xform/thumb-160x120/ of PNG file. Reviewers: epriestley Reviewed By: epriestley CC: aran, epriestley Differential Revision: https://secure.phabricator.com/D2039
This commit is contained in:
parent
ec283abb14
commit
99b03ffd47
1 changed files with 28 additions and 9 deletions
|
@ -23,8 +23,7 @@ final class PhabricatorImageTransformer {
|
|||
$x,
|
||||
$y) {
|
||||
|
||||
$data = $file->loadFileData();
|
||||
$image = $this->crudelyScaleTo($data, $x, $y);
|
||||
$image = $this->crudelyScaleTo($file, $x, $y);
|
||||
|
||||
return PhabricatorFile::newFromFileData(
|
||||
$image,
|
||||
|
@ -39,8 +38,7 @@ final class PhabricatorImageTransformer {
|
|||
$min_y,
|
||||
$max_y) {
|
||||
|
||||
$data = $file->loadFileData();
|
||||
$image = $this->crudelyCropTo($data, $x, $min_y, $max_y);
|
||||
$image = $this->crudelyCropTo($file, $x, $min_y, $max_y);
|
||||
|
||||
return PhabricatorFile::newFromFileData(
|
||||
$image,
|
||||
|
@ -49,7 +47,8 @@ final class PhabricatorImageTransformer {
|
|||
));
|
||||
}
|
||||
|
||||
private function crudelyCropTo($data, $x, $min_y, $max_y) {
|
||||
private function crudelyCropTo(PhabricatorFile $file, $x, $min_y, $max_y) {
|
||||
$data = $file->loadFileData();
|
||||
$img = imagecreatefromstring($data);
|
||||
$sx = imagesx($img);
|
||||
$sy = imagesy($img);
|
||||
|
@ -68,18 +67,19 @@ final class PhabricatorImageTransformer {
|
|||
$x,
|
||||
$scaled_y);
|
||||
|
||||
return $this->saveImageDataInAnyFormat($img);
|
||||
return $this->saveImageDataInAnyFormat($img, $file->getMimeType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Very crudely scale an image up or down to an exact size.
|
||||
*/
|
||||
private function crudelyScaleTo($data, $dx, $dy) {
|
||||
private function crudelyScaleTo(PhabricatorFile $file, $dx, $dy) {
|
||||
$data = $file->loadFileData();
|
||||
$src = imagecreatefromstring($data);
|
||||
|
||||
$dst = $this->applyScaleTo($src, $dx, $dy);
|
||||
|
||||
return $this->saveImageDataInAnyFormat($dst);
|
||||
return $this->saveImageDataInAnyFormat($dst, $file->getMimeType());
|
||||
}
|
||||
|
||||
private function applyScaleTo($src, $dx, $dy) {
|
||||
|
@ -105,7 +105,26 @@ final class PhabricatorImageTransformer {
|
|||
return $dst;
|
||||
}
|
||||
|
||||
private function saveImageDataInAnyFormat($data) {
|
||||
private function saveImageDataInAnyFormat($data, $preferred_mime = '') {
|
||||
switch ($preferred_mime) {
|
||||
case 'image/jpg':
|
||||
case 'image/jpeg':
|
||||
if (function_exists('imagejpeg')) {
|
||||
ob_start();
|
||||
imagejpeg($data);
|
||||
return ob_get_clean();
|
||||
}
|
||||
break;
|
||||
case 'image/gif': // GIF doesn't support true color.
|
||||
case 'image/png':
|
||||
if (function_exists('imagepng')) {
|
||||
ob_start();
|
||||
imagepng($data);
|
||||
return ob_get_clean();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$img = null;
|
||||
|
||||
if (function_exists('imagejpeg')) {
|
||||
|
|
Loading…
Reference in a new issue