Clean up createImageFile()

This commit is contained in:
Kijin Sung 2021-01-19 21:13:49 +09:00
parent 71d71a886c
commit 06f973d227

View file

@ -495,7 +495,7 @@ class FileHandler
// check params // check params
if (($source_file = self::exists($source_file)) === FALSE) if (($source_file = self::exists($source_file)) === FALSE)
{ {
return; return false;
} }
$target_file = self::getRealPath($target_file); $target_file = self::getRealPath($target_file);
@ -503,7 +503,6 @@ class FileHandler
{ {
$resize_width = 100; $resize_width = 100;
} }
if(!$resize_height) if(!$resize_height)
{ {
$resize_height = $resize_width; $resize_height = $resize_width;
@ -513,91 +512,59 @@ class FileHandler
$imageInfo = getimagesize($source_file); $imageInfo = getimagesize($source_file);
if(!self::checkMemoryLoadImage($imageInfo)) if(!self::checkMemoryLoadImage($imageInfo))
{ {
return FALSE; return false;
} }
list($width, $height, $type, $attrs) = $imageInfo; list($width, $height, $type) = $imageInfo;
if($width < 1 || $height < 1) if($width < 1 || $height < 1)
{ {
return; return false;
} }
switch($type) $typemap = array(
1 => 'gif',
2 => 'jpg',
3 => 'png',
6 => 'bmp',
18 => 'webp',
);
$type = isset($typemap[$type]) ? $typemap[$type] : null;
if (!$type)
{ {
case '1' : return false;
$type = 'gif';
break;
case '2' :
$type = 'jpg';
break;
case '3' :
$type = 'png';
break;
case '6' :
$type = 'bmp';
break;
case '8' :
$type = 'wbmp';
break;
case '18' :
$type = 'webp';
break;
default :
return;
} }
if(!$target_type) $target_type = strtolower($target_type ?: $type);
if ($target_type === 'jpeg')
{ {
$target_type = $type; $target_type = 'jpg';
} }
$target_type = strtolower($target_type);
// create temporary image having original type // create temporary image having original type
$source = NULL; if ($type === 'gif' && function_exists('imagecreatefromgif'))
switch($type)
{ {
case 'gif' : $source = @imagecreatefromgif($source_file);
if(function_exists('imagecreatefromgif'))
{
$source = @imagecreatefromgif($source_file);
}
break;
case 'jpeg' :
case 'jpg' :
if(function_exists('imagecreatefromjpeg'))
{
$source = @imagecreatefromjpeg($source_file);
}
break;
case 'png' :
if(function_exists('imagecreatefrompng'))
{
$source = @imagecreatefrompng($source_file);
}
break;
case 'bmp' :
if(function_exists('imagecreatefrombmp'))
{
$source = @imagecreatefrombmp($source_file);
}
break;
case 'wbmp' :
if(function_exists('imagecreatefromwbmp'))
{
$source = @imagecreatefromwbmp($source_file);
}
break;
case 'webp' :
if(function_exists('imagecreatefromwebp'))
{
$source = @imagecreatefromwebp($source_file);
}
break;
} }
elseif ($type === 'jpg' && function_exists('imagecreatefromjpeg'))
if(!$source)
{ {
return FALSE; $source = @imagecreatefromjpeg($source_file);
}
elseif ($type === 'png' && function_exists('imagecreatefrompng'))
{
$source = @imagecreatefrompng($source_file);
}
elseif ($type === 'bmp' && function_exists('imagecreatefrombmp'))
{
$source = @imagecreatefrombmp($source_file);
}
elseif ($type === 'webp' && function_exists('imagecreatefromwebp'))
{
$source = @imagecreatefromwebp($source_file);
}
else
{
return false;
} }
// Rotate image // Rotate image
@ -626,8 +593,8 @@ class FileHandler
if ($target_type == 'png') if ($target_type == 'png')
{ {
imagefill($thumb, 0, 0, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagefill($thumb, 0, 0, imagecolorallocatealpha($thumb, 0, 0, 0, 127));
imagesavealpha($thumb, TRUE); imagealphablending($thumb, true);
imagealphablending($thumb, TRUE); imagesavealpha($thumb, true);
} }
else else
{ {
@ -645,34 +612,33 @@ class FileHandler
$ratio_min = min($ratio_x, $ratio_y); $ratio_min = min($ratio_x, $ratio_y);
// Determine the X-Y coordinates to copy // Determine the X-Y coordinates to copy
switch ($thumbnail_type) if ($thumbnail_type === 'stretch')
{ {
case 'stretch': $dst_width = $resize_width;
$dst_width = $resize_width; $dst_height = $resize_height;
$dst_height = $resize_height; }
break; elseif ($thumbnail_type === 'fill')
{
case 'fill': $dst_width = round($width * $ratio_max);
$dst_width = round($width * $ratio_max); $dst_height = round($height * $ratio_max);
$dst_height = round($height * $ratio_max); }
break; elseif ($thumbnail_type === 'center')
{
case 'center': $dst_width = $width;
$dst_width = $width; $dst_height = $height;
$dst_height = $height; }
break; elseif ($thumbnail_type === 'ratio')
{
case 'ratio': $dst_width = round($width * $ratio_min);
$dst_width = round($width * $ratio_min); $dst_height = round($height * $ratio_min);
$dst_height = round($height * $ratio_min); }
break; else // crop
{
case 'crop': $dst_width = round($width * min(1, $ratio_max));
default: $dst_height = round($height * min(1, $ratio_max));
$dst_width = round($width * min(1, $ratio_max));
$dst_height = round($height * min(1, $ratio_max));
} }
// Adjust for small margins of error
if (abs($resize_width - $dst_width) < ($resize_width * 0.02)) $dst_width = $resize_width; if (abs($resize_width - $dst_width) < ($resize_width * 0.02)) $dst_width = $resize_width;
if (abs($resize_height - $dst_height) < ($resize_height * 0.02)) $dst_height = $resize_height; if (abs($resize_height - $dst_height) < ($resize_height * 0.02)) $dst_height = $resize_height;
$dst_x = round(($resize_width - $dst_width) / 2); $dst_x = round(($resize_width - $dst_width) / 2);
@ -685,58 +651,35 @@ class FileHandler
self::makeDir(dirname($target_file)); self::makeDir(dirname($target_file));
// write into the file // write into the file
$output = NULL; if ($target_type === 'gif' && function_exists('imagegif'))
switch($target_type)
{ {
case 'gif' : $output = imagegif($thumb, $target_file);
if(function_exists('imagegif'))
{
$output = imagegif($thumb, $target_file);
}
break;
case 'jpeg' :
case 'jpg' :
if(function_exists('imagejpeg'))
{
$output = imagejpeg($thumb, $target_file, $quality);
}
break;
case 'png' :
if(function_exists('imagepng'))
{
$output = imagepng($thumb, $target_file, 9);
}
break;
case 'bmp' :
if(function_exists('imagebmp'))
{
$output = imagebmp($thumb, $target_file);
}
break;
case 'wbmp' :
if(function_exists('imagewbmp'))
{
$output = imagewbmp($thumb, $target_file);
}
break;
case 'webp' :
if(function_exists('imagewebp'))
{
$output = imagewebp($thumb, $target_file);
}
break;
} }
elseif ($target_type === 'jpg' && function_exists('imagejpeg'))
{
$output = imagejpeg($thumb, $target_file, $quality);
}
elseif ($target_type === 'png' && function_exists('imagepng'))
{
$output = imagepng($thumb, $target_file, 9);
}
elseif ($target_type === 'bmp' && function_exists('imagebmp'))
{
$output = imagebmp($thumb, $target_file);
}
elseif ($target_type === 'webp' && function_exists('imagewebp'))
{
$output = imagewebp($thumb, $target_file);
}
else
{
return false;
}
imagedestroy($thumb); imagedestroy($thumb);
imagedestroy($source); imagedestroy($source);
@chmod($target_file, 0666 & ~Rhymix\Framework\Storage::getUmask());
if(!$output) return $output;
{
return FALSE;
}
@chmod($target_file, 0644);
return TRUE;
} }
/** /**