You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

<?php
abstract class Upload
{
/**
* @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
* @return Image
*/
public static function image($file)
{
require_once PATH . '/lib/GreagwarImage/GreagwarImage.php';
$greagwar_image = new GreagwarImage($file['tmp_name']);
$greagwar_image->setCacheDir('media/cache/images');
$image = new Image;
$image->original_filename = $file['name'];
$image->type = $file['type'];
self::saveImage($image, $greagwar_image);
foreach (Image::$sizes as $size) {
$size_parts = explode('x', $size);
self::saveImage($image->getVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
}
return $image;
}
/**
* @param $image Image
* @param $greagwar_image GreagwarImage
* @param null $width
* @param null $height
* @param bool $crop
*/
protected static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true)
{
if ($width || $height) {
$greagwar_image->resize($width, $height, 0xffffff, false, false, $crop);
}
$file_path = $greagwar_image->cacheFile($greagwar_image->guessType(), 87, true);
$path_parts = pathinfo($file_path);
$image->path = $path_parts['dirname'];
$image->filename = $path_parts['basename'];
$image->size = filesize($image->path . '/' . $image->filename);
$image->width = $greagwar_image->width();
$image->height = $greagwar_image->height();
$image->setIsNotEmpty();
}
public static function imageCompareAspectRatio($proportion, $file_path)
{
$imagesize = getimagesize($file_path);
$proprtion_parts = explode('x', $proportion);
if ($proprtion_parts[0] / $proprtion_parts[1] === $imagesize[0] / $imagesize[1]) {
return true;
} else {
false;
}
}
}