2016-07-22 20:00:27 +00:00
|
|
|
<?php
|
|
|
|
|
2016-09-20 18:36:22 +00:00
|
|
|
use Vn\Lib\UserException;
|
|
|
|
|
2018-05-23 10:14:20 +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
|
|
|
|
**/
|
2018-05-23 10:14:20 +00:00
|
|
|
static function create($srcFile) {
|
|
|
|
$imageType = exif_imagetype($srcFile);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
if ($imageType !== FALSE)
|
2018-05-23 10:14:20 +00:00
|
|
|
switch ($imageType) {
|
2016-07-22 20:00:27 +00:00
|
|
|
case IMAGETYPE_JPEG:
|
2018-05-23 10:14:20 +00:00
|
|
|
$image = imagecreatefromjpeg($srcFile);
|
2016-07-22 20:00:27 +00:00
|
|
|
break;
|
|
|
|
case IMAGETYPE_PNG:
|
2018-05-23 10:14:20 +00:00
|
|
|
$image = imagecreatefrompng($srcFile);
|
2016-07-22 20:00:27 +00:00
|
|
|
break;
|
|
|
|
case IMAGETYPE_GIF:
|
|
|
|
$image = imagecreatefromgif ($srcFile);
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new UserException(s('Bad file format'));
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
else
|
2018-05-23 10:14:20 +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
|
|
|
|
**/
|
2018-05-23 10:14:20 +00:00
|
|
|
static function resizeSave($image, $dstFile, $maxHeight, $maxWidth, $crop = FALSE, $symbolicSrc = NULL) {
|
|
|
|
$width = imagesx($image);
|
|
|
|
$height = imagesy($image);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$dirname = dirname($dstFile);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!is_dir($dirname))
|
|
|
|
mkdir($dirname, 0775, TRUE);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (file_exists($dstFile))
|
|
|
|
@unlink($dstFile);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
// Check if it is necessary to resize the image
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($height > $maxHeight || $width > $maxWidth) {
|
2016-07-22 20:00:27 +00:00
|
|
|
$srcX = 0;
|
|
|
|
$srcY = 0;
|
|
|
|
$srcWidth = $width;
|
|
|
|
$srcHeight = $height;
|
|
|
|
$dstWidth = $width;
|
|
|
|
$dstHeight = $height;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!$crop) // Resize {
|
2016-07-22 20:00:27 +00:00
|
|
|
$ratio = NULL;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($dstWidth > $maxWidth) {
|
2016-07-22 20:00:27 +00:00
|
|
|
$ratio = $dstWidth / $maxWidth;
|
|
|
|
$dstWidth = $maxWidth;
|
2018-05-23 10:14:20 +00:00
|
|
|
$dstHeight =(int)($dstHeight / $ratio);
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($dstHeight > $maxHeight) {
|
2016-07-22 20:00:27 +00:00
|
|
|
$ratio = $dstHeight / $maxHeight;
|
|
|
|
$dstHeight = $maxHeight;
|
2018-05-23 10:14:20 +00:00
|
|
|
$dstWidth =(int)($dstWidth / $ratio);
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 10:14:20 +00:00
|
|
|
else // Cut & resize {
|
2016-07-22 20:00:27 +00:00
|
|
|
if ($width > $maxWidth)
|
|
|
|
$dstWidth = $maxWidth;
|
|
|
|
if ($height > $maxWidth)
|
|
|
|
$dstHeight = $maxHeight;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($width <= $maxWidth) {
|
2016-07-22 20:00:27 +00:00
|
|
|
if ($height > $srcHeight)
|
|
|
|
$srcHeight = $maxHeight;
|
|
|
|
}
|
2018-05-23 10:14:20 +00:00
|
|
|
elseif ($height <= $maxHeight) {
|
2016-07-22 20:00:27 +00:00
|
|
|
if ($width > $maxWidth)
|
|
|
|
$srcWidth = $maxWidth;
|
|
|
|
}
|
2018-05-23 10:14:20 +00:00
|
|
|
else {
|
|
|
|
$srcWidth =(int)($maxWidth *($height / $maxHeight));
|
|
|
|
$srcHeight =(int)($maxHeight *($width / $maxWidth));
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
if ($srcWidth <= $width)
|
|
|
|
$srcHeight = $height;
|
|
|
|
else
|
|
|
|
$srcWidth = $width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($width !== $srcWidth)
|
2018-05-23 10:14:20 +00:00
|
|
|
$srcX =(int)(($width / 2) -($srcWidth / 2));
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
if ($height !== $srcHeight)
|
2018-05-23 10:14:20 +00:00
|
|
|
$srcY =(int)(($height / 2) -($srcHeight / 2));
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$resizedImage = imagecreatetruecolor($dstWidth, $dstHeight);
|
|
|
|
imagealphablending($resizedImage, FALSE);
|
|
|
|
imagesavealpha($resizedImage, TRUE);
|
|
|
|
imagecopyresampled($resizedImage, $image,
|
2016-07-22 20:00:27 +00:00
|
|
|
0, 0, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
|
2018-05-23 10:14:20 +00:00
|
|
|
$saved = imagepng($resizedImage, $dstFile);
|
|
|
|
imagedestroy($resizedImage);
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
2018-05-23 10:14:20 +00:00
|
|
|
elseif (isset($symbolicSrc)) {
|
|
|
|
$saved = symlink($symbolicSrc, $dstFile);
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
2018-05-23 10:14:20 +00:00
|
|
|
else {
|
|
|
|
imagesavealpha($image, TRUE);
|
|
|
|
$saved = imagepng($image, $dstFile);
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$saved)
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new UserException(sprintf(s('File save error: %s'), $dstFile));
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|