forked from GNUsocial/gnu-social
Enable square thumbnailing
This commit is contained in:
parent
71bda34e72
commit
07b232774d
@ -115,7 +115,7 @@ class MediaFile
|
|||||||
*
|
*
|
||||||
* @return File_thumbnail or null
|
* @return File_thumbnail or null
|
||||||
*/
|
*/
|
||||||
function storeThumbnail()
|
function storeThumbnail($maxWidth=null, $maxHeight=null, $square=true)
|
||||||
{
|
{
|
||||||
$imgPath = null;
|
$imgPath = null;
|
||||||
$media = common_get_mime_media($this->mimetype);
|
$media = common_get_mime_media($this->mimetype);
|
||||||
@ -146,11 +146,14 @@ class MediaFile
|
|||||||
$outname = File::filename($this->scoped, 'thumb-' . $this->filename, $this->mimetype);
|
$outname = File::filename($this->scoped, 'thumb-' . $this->filename, $this->mimetype);
|
||||||
$outpath = File::path($outname);
|
$outpath = File::path($outname);
|
||||||
|
|
||||||
$maxWidth = common_config('attachments', 'thumb_width');
|
$maxWidth = $maxWidth ?: common_config('attachments', 'thumb_width');
|
||||||
$maxHeight = common_config('attachments', 'thumb_height');
|
$maxHeight = $maxHeight ?: common_config('attachments', 'thumb_height');
|
||||||
list($width, $height) = $this->scaleToFit($image->width, $image->height, $maxWidth, $maxHeight);
|
list($width, $height, $x, $y, $w2, $h2) =
|
||||||
|
$this->scaleToFit($image->width, $image->height,
|
||||||
|
$maxWidth, $maxHeight,
|
||||||
|
common_config('attachments', 'thumb_square'));
|
||||||
|
|
||||||
$image->resizeTo($outpath, $width, $height);
|
$image->resizeTo($outpath, $width, $height, $x, $y, $w2, $h2);
|
||||||
|
|
||||||
// Avoid deleting the original
|
// Avoid deleting the original
|
||||||
if ($image->getPath() != $this->getPath()) {
|
if ($image->getPath() != $this->getPath()) {
|
||||||
@ -162,17 +165,52 @@ class MediaFile
|
|||||||
$height);
|
$height);
|
||||||
}
|
}
|
||||||
|
|
||||||
function scaleToFit($width, $height, $maxWidth, $maxHeight)
|
// This will give parameters to scale up if max values are larger than original
|
||||||
|
function scaleToFit($width, $height, $maxWidth=null, $maxHeight=null, $square=true)
|
||||||
{
|
{
|
||||||
$aspect = $maxWidth / $maxHeight;
|
if ($width <= 0 || $height <= 0
|
||||||
$w1 = $maxWidth;
|
|| ($maxWidth !== null && $maxWidth <= 0)
|
||||||
$h1 = intval($height * $maxWidth / $width);
|
|| ($maxHeight !== null && $maxHeight <= 0)) {
|
||||||
if ($h1 > $maxHeight) {
|
throw new ServerException('Bad scaleToFit parameters for MediaFile');
|
||||||
$w2 = intval($width * $maxHeight / $height);
|
} elseif ($maxWidth === null) {
|
||||||
$h2 = $maxHeight;
|
// maxWidth must be a positive number
|
||||||
return array($w2, $h2);
|
throw new ServerException('MediaFile::scaleToFit maxWidth is null');
|
||||||
|
} elseif ($square || $maxHeight === null) {
|
||||||
|
// if square thumb ratio or if maxHeight is null,
|
||||||
|
// we set maxHeight to equal maxWidth
|
||||||
|
$maxHeight = $maxWidth;
|
||||||
|
$square = true;
|
||||||
}
|
}
|
||||||
return array($w1, $h1);
|
|
||||||
|
// cropping data
|
||||||
|
$cx = 0; // crop x
|
||||||
|
$cy = 0; // crop y
|
||||||
|
$cw = null; // crop area width
|
||||||
|
$ch = null; // crop area height
|
||||||
|
|
||||||
|
if ($square) {
|
||||||
|
// resulting width and height
|
||||||
|
$rw = $maxWidth;
|
||||||
|
$rh = $maxHeight;
|
||||||
|
|
||||||
|
// minSide will determine the smallest image size
|
||||||
|
// and crop-values are determined from this
|
||||||
|
$minSide = $width > $height ? $height : $width;
|
||||||
|
$cx = $width / 2 - $minSide / 2;
|
||||||
|
$cy = $height / 2 - $minSide / 2;
|
||||||
|
$cw = $minSide;
|
||||||
|
$ch = $minSide;
|
||||||
|
} else {
|
||||||
|
// resulting sizes
|
||||||
|
$rw = $maxWidth;
|
||||||
|
$rh = floor($height * $maxWidth / $width);
|
||||||
|
|
||||||
|
if ($rh > $maxHeight) {
|
||||||
|
$rw = floor($width * $maxHeight / $height);
|
||||||
|
$rh = $maxHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return array($rw, $rh, $cx, $cy, $cw, $ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
function rememberFile($file, $short)
|
function rememberFile($file, $short)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user