hedera-web/rest/image/image.php

148 lines
3.5 KiB
PHP
Executable File

<?php
namespace Vn\Hedera;
static class Image
{
/**
* Creates an image resource from a valid image file.
*
* @param string $srcFile The source file name
**/
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:
throw new Exception (s('Bad file format'), 'badFileFormat');
}
else
throw new Exception (s('Image open error'), 'openError');
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
**/
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)
throw new Exception (sprintf (s('File save error: %s'), $dstFile), 'saveError', ));
}
}
?>