blue-twilight/app/Helpers/ImageHelper.php

115 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Helpers;
use App\AlbumSources\IAlbumSource;
use App\Photo;
class ImageHelper
{
public function generateThumbnail($gdImageResource, Photo $photo, $thumbnailInfo)
{
$thumbnailWidth = intval($thumbnailInfo['width']);
$thumbnailHeight = intval($thumbnailInfo['height']);
// If image is portrait, restrict the height by the relevant aspect ratio
if ($photo->height > $photo->width)
{
$aspectRatio = $photo->height / $photo->width;
$thumbnailWidth = intval($thumbnailInfo['height']) / $aspectRatio;
$thumbnailHeight = intval($thumbnailInfo['height']);
}
$thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
imagecopyresized(
$thumbnailImageResource,
$gdImageResource,
0,
0,
0,
0,
$thumbnailWidth,
$thumbnailHeight,
$photo->width,
$photo->height
);
// TODO make the /tmp folder configurable
$tempName = tempnam('/tmp', 'btw_thumb_');
$tempNameWithExtension = ($tempName . '.jpg');
rename($tempName, $tempNameWithExtension);
// TODO make thumbnail quality configurable
imagejpeg($thumbnailImageResource, $tempNameWithExtension, 90);
return $tempNameWithExtension;
}
public function openImage($imagePath, &$imageInfo)
{
$imageInfo = getimagesize($imagePath);
$im = false;
$type = $imageInfo[2];
$allowedTypes = [
IMG_GIF,
IMG_PNG,
IMG_JPEG,
IMG_WBMP
];
if (!in_array($type, $allowedTypes))
{
return false;
}
switch ($type)
{
case IMG_GIF:
$im = imagecreatefromgif($imagePath);
break;
case IMG_JPEG:
$im = imagecreatefromjpeg($imagePath);
break;
case IMG_PNG:
$im = imagecreatefrompng($imagePath);
break;
case IMG_WBMP:
$im = imagecreatefromwbmp($imagePath);
break;
}
return $im;
}
public function rotateImage($imageResource, $angle)
{
imagesetinterpolation($imageResource, IMG_SINC);
return imagerotate($imageResource, $angle, 0);
/*switch ($angle)
{
case 90:
case 270:
// Need a new image canvas
$maxDimension = ($photo->width > $photo->height ? $photo->width : $photo->height);
$newImageResource = imagecreatetruecolor($maxDimension, $maxDimension);
imagecopy($newImageResource, $imageResource, 0, 0, 0, 0, $photo->width, $photo->height);
$newImageResource = imagerotate($newImageResource, $angle, 0);
$imageResource = $newImageResource;
break;
case 180:
// We can just rotate this in-place
$imageResource = imagerotate($imageResource, $angle, 0);
break;
}
return $imageResource;*/
}
}