에 의해 원래의 폭과 높이를 곱 여기에 내가 GD를 사용하여 썸네일을 생성하기 위해 작성하는 기능입니다. 최대 너비 또는 높이 또는 둘 모두를 전달할 수 있으며 (0이면 무제한을 의미) 썸네일 비율은 그대로 비율이 $dest
(+ 파일 확장자)로 조정됩니다. 또한 투명 이미지에서도 작동합니다. 여분의 공간은 완전히 투명해야합니다. 다른 배경을 원한다면 imagecopyresampled()
앞에있는 $img
을 수정하십시오.
function picThumb($src, $dest, $width = 0, $height = 0, $quality = 100)
{
$srcType = exif_imagetype($src);
if (!$width && !$height)
{
$ext = image_type_to_extension($srcType, false);
copy($src, $dest . '.' . $ext);
return $ext;
}
ini_set('memory_limit', '134217728');
try
{
switch ($srcType)
{
case IMAGETYPE_JPEG:
$srcImg = imagecreatefromjpeg($src);
break;
case IMAGETYPE_PNG:
$srcImg = imagecreatefrompng($src);
break;
case IMAGETYPE_GIF:
$srcImg = imagecreatefromgif($src);
break;
default:
throw new Exception();
}
$srcWidth = imagesx($srcImg);
$srcHeight = imagesy($srcImg);
if (!$srcWidth || !$srcHeight)
{
throw new Exception();
}
if ($width && $height)
{
$ratio = min($srcWidth/$width, $srcHeight/$height);
$areaWidth = round($width * $ratio);
$areaHeight = round($height * $ratio);
$areaX = round(($srcWidth - $areaWidth)/2);
$areaY = round(($srcHeight - $areaHeight)/2);
}
else // if (!$width || !$height)
{
if ($width)
{
$height = round($width/$srcWidth * $srcHeight);
}
else // if ($height)
{
$width = round($height/$srcHeight * $srcWidth);
}
$areaWidth = $srcWidth;
$areaHeight = $srcHeight;
$areaX = 0;
$areaY = 0;
}
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagecopyresampled($img, $srcImg, 0, 0, $areaX, $areaY, $width, $height, $areaWidth, $areaHeight);
switch ($srcType)
{
case IMAGETYPE_JPEG:
$ext = 'jpg';
imagejpeg($img, $dest . '.' . $ext, $quality);
break;
case IMAGETYPE_PNG:
case IMAGETYPE_GIF:
$ext = 'png';
imagesavealpha($img, true);
imagepng($img, $dest . '.' . $ext, 9);
break;
default:
throw new Exception();
}
imagedestroy($srcImg);
imagedestroy($img);
}
catch (Exception $e)
{
ini_restore('memory_limit');
throw $e;
}
ini_restore('memory_limit');
return $ext;
}
이 매우 명확하지 않다. 필요한 것을 다시 말하거나 몇 가지 시각적 인 예나 다른 것을 주시겠습니까? – JakeParis
@ JMC가 말한 것, 특히 비율을 어떻게 다뤄야 할까 두 번째. – Trufa