hedera-web/rest/image/image.php

147 lines
3.4 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
2016-09-20 18:36:22 +00:00
use Vn\Lib\UserException;
2016-08-25 10:47:09 +00:00
class Image
2016-07-22 20:00:27 +00:00
{
/**
* Creates an image resource from a valid image file.
*
* @param string $srcFile The source file name
2016-12-20 09:32:17 +00:00
*/
2016-07-22 20:00:27 +00:00
static function create ($srcFile)
{
$imageType = exif_imagetype ($srcFile);
if ($imageType !== FALSE)
switch ($imageType)
{
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg ($srcFile);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng ($srcFile);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif ($srcFile);
break;
default:
2016-09-20 18:36:22 +00:00
throw new UserException (s('Bad file format'));
2016-07-22 20:00:27 +00:00
}
else
2016-09-20 18:36:22 +00:00
throw new UserException (s('Image open error'));
2016-07-22 20:00:27 +00:00
return $image;
}
/**
* Resizes and saves an image resource.
*
* @param resource $image The image resource
* @param string $dstFile The destination file name
* @param integer $maxHeight The maximum height of resized image in pixels
* @param integer $maxWidth The maximum width of resized image in pixels
* @param boolean $crop Wether to crop the image
* @param boolean $symbolicSrc If it is not necessary to resize the image creates a symbolic link using the passed path as source
2016-12-20 09:32:17 +00:00
*/
2016-07-22 20:00:27 +00:00
static function resizeSave ($image, $dstFile, $maxHeight, $maxWidth, $crop = FALSE, $symbolicSrc = NULL)
{
$width = imagesx ($image);
$height = imagesy ($image);
$dirname = dirname ($dstFile);
if (!is_dir ($dirname))
mkdir ($dirname, 0775, TRUE);
if (file_exists ($dstFile))
@unlink ($dstFile);
// Check if it is necessary to resize the image
if ($height > $maxHeight || $width > $maxWidth)
{
$srcX = 0;
$srcY = 0;
$srcWidth = $width;
$srcHeight = $height;
$dstWidth = $width;
$dstHeight = $height;
if (!$crop) // Resize
{
$ratio = NULL;
if ($dstWidth > $maxWidth)
{
$ratio = $dstWidth / $maxWidth;
$dstWidth = $maxWidth;
$dstHeight = (int) ($dstHeight / $ratio);
}
if ($dstHeight > $maxHeight)
{
$ratio = $dstHeight / $maxHeight;
$dstHeight = $maxHeight;
$dstWidth = (int) ($dstWidth / $ratio);
}
}
else // Cut & resize
{
if ($width > $maxWidth)
$dstWidth = $maxWidth;
if ($height > $maxWidth)
$dstHeight = $maxHeight;
if ($width <= $maxWidth)
{
if ($height > $srcHeight)
$srcHeight = $maxHeight;
}
elseif ($height <= $maxHeight)
{
if ($width > $maxWidth)
$srcWidth = $maxWidth;
}
else
{
$srcWidth = (int) ($maxWidth * ($height / $maxHeight));
$srcHeight = (int) ($maxHeight * ($width / $maxWidth));
if ($srcWidth <= $width)
$srcHeight = $height;
else
$srcWidth = $width;
}
if ($width !== $srcWidth)
$srcX = (int) (($width / 2) - ($srcWidth / 2));
if ($height !== $srcHeight)
$srcY = (int) (($height / 2) - ($srcHeight / 2));
}
$resizedImage = imagecreatetruecolor ($dstWidth, $dstHeight);
imagealphablending ($resizedImage, FALSE);
imagesavealpha ($resizedImage, TRUE);
imagecopyresampled ($resizedImage, $image,
0, 0, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
$saved = imagepng ($resizedImage, $dstFile);
imagedestroy ($resizedImage);
}
elseif (isset ($symbolicSrc))
{
$saved = symlink ($symbolicSrc, $dstFile);
}
else
{
imagesavealpha ($image, TRUE);
$saved = imagepng ($image, $dstFile);
}
if (!$saved)
2016-09-20 18:36:22 +00:00
throw new UserException (sprintf (s('File save error: %s'), $dstFile));
2016-07-22 20:00:27 +00:00
}
}