2016-09-03 17:09:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
use App\AlbumSources\IAlbumSource;
|
|
|
|
use App\Photo;
|
|
|
|
|
|
|
|
class ImageHelper
|
|
|
|
{
|
2016-09-03 22:13:05 +01:00
|
|
|
public function generateThumbnail($gdImageResource, Photo $photo, $thumbnailInfo)
|
2016-09-03 17:09:49 +01:00
|
|
|
{
|
|
|
|
$thumbnailWidth = intval($thumbnailInfo['width']);
|
|
|
|
$thumbnailHeight = intval($thumbnailInfo['height']);
|
2016-09-04 21:59:32 +01:00
|
|
|
|
|
|
|
// 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']);
|
|
|
|
}
|
|
|
|
|
2016-09-09 11:09:03 +01:00
|
|
|
/*$thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
|
2016-09-03 17:09:49 +01:00
|
|
|
|
|
|
|
imagecopyresized(
|
|
|
|
$thumbnailImageResource,
|
|
|
|
$gdImageResource,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
$thumbnailWidth,
|
|
|
|
$thumbnailHeight,
|
|
|
|
$photo->width,
|
|
|
|
$photo->height
|
2016-09-09 11:09:03 +01:00
|
|
|
);*/
|
|
|
|
$thumbnailImageResource = imagescale($gdImageResource, $thumbnailWidth, $thumbnailHeight, $this->getInterpolationMethod());
|
2016-09-03 22:13:05 +01:00
|
|
|
|
|
|
|
// TODO make the /tmp folder configurable
|
|
|
|
$tempName = tempnam('/tmp', 'btw_thumb_');
|
|
|
|
$tempNameWithExtension = ($tempName . '.jpg');
|
|
|
|
rename($tempName, $tempNameWithExtension);
|
2016-09-03 17:09:49 +01:00
|
|
|
|
|
|
|
// TODO make thumbnail quality configurable
|
2016-09-03 22:13:05 +01:00
|
|
|
imagejpeg($thumbnailImageResource, $tempNameWithExtension, 90);
|
|
|
|
|
|
|
|
return $tempNameWithExtension;
|
2016-09-03 17:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function openImage($imagePath, &$imageInfo)
|
|
|
|
{
|
|
|
|
$imageInfo = getimagesize($imagePath);
|
2016-09-05 14:06:41 +01:00
|
|
|
if ($imageInfo === false)
|
|
|
|
{
|
|
|
|
throw new \Exception(sprintf('The image "%s" does not appear to be a valid image, or cannot be read', pathinfo($imagePath, PATHINFO_FILENAME)));
|
|
|
|
}
|
|
|
|
|
2016-09-03 17:09:49 +01:00
|
|
|
$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;
|
|
|
|
}
|
2016-09-05 09:58:37 +01:00
|
|
|
|
|
|
|
public function rotateImage($imageResource, $angle)
|
|
|
|
{
|
2016-09-09 11:09:03 +01:00
|
|
|
imagesetinterpolation($imageResource, $this->getInterpolationMethod());
|
2016-09-05 09:58:37 +01:00
|
|
|
|
|
|
|
return imagerotate($imageResource, $angle, 0);
|
|
|
|
}
|
2016-09-08 23:22:29 +01:00
|
|
|
|
|
|
|
public function saveImage($imageResource, $imagePath, $imageInfo)
|
|
|
|
{
|
|
|
|
switch ($imageInfo[2])
|
|
|
|
{
|
|
|
|
case IMG_GIF:
|
|
|
|
imagegif($imageResource, $imagePath);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMG_JPEG:
|
|
|
|
imagejpeg($imageResource, $imagePath);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMG_PNG:
|
|
|
|
imagepng($imageResource, $imagePath);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMG_WBMP:
|
|
|
|
imagewbmp($imageResource, $imagePath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 11:09:03 +01:00
|
|
|
|
|
|
|
private function getInterpolationMethod()
|
|
|
|
{
|
|
|
|
return IMG_SINC;
|
|
|
|
}
|
2016-09-03 17:09:49 +01:00
|
|
|
}
|