0
이미지 크기를 400 x 300px로 조정하고 싶습니다. 그리고 이미지가 너비에서 400 픽셀보다 큰 경우 높이를 자르기 전에 먼저 크기를 조정하고 싶습니다.PHP img 자르기 전에 크기 조정
이미지는 원격 웹 사이트에서 제공되므로 인물 사진이 될 수 있으며 가로 모드 일 수 있습니다. 그러나 가능한 한 자르기 양을 최소화하고 가급적 자르기 전에 크기를 조정해야합니다.
다음 코드를 사용하고 있습니다. (솔직히 말해서 혼란 스럽습니다.) 비율과 숫자가 정확하지 않습니다. 코드는 여러 개의 SO 응답 코드에서 나옵니다.
function makeThumb($imgsrc, $imgtarg, $imgtarg_d) {
$ext = exif_imagetype($imgsrc);
if ($ext == false) {
return;
}
//getting the image dimensions
list($width, $height) = getimagesize($imgsrc);
//saving the image into memory (for manipulation with GD Library)
switch($ext) {
case 1:
$myImage = imagecreatefromgif($imgsrc);
break;
case 2:
$myImage = imagecreatefromjpeg($imgsrc);
break;
case 3:
$myImage = imagecreatefrompng($imgsrc);
break;
}
// calculating the part of the image to use for thumbnail
if ($width > $height) {
$y = 0;
$x = ($width - $height)/2;
$smallestSide = $height;
if ($width >= 400) {
$thumbSizeWidth = 400;
$thumbSizeHeight = 300;
} else {
$thumbSizeWidth = $width;
$thumbSizeHeight = 300;
}
} else {
$x = 0;
$y = ($height - $width)/2;
$smallestSide = $width;
if ($height >= 300) {
$thumbSizeHeight = 300;
$thumbSizeWidth = 400;
} else {
$thumbSizeHeight = $height;
$thumbSizeWidth = 400;
}
}
$thumb = imagecreatetruecolor($thumbSizeWidth, $thumbSizeHeight);
/*RESIZE FIRST*/
imagecopyresampled($thumb, $myImage, 0, 0, 0, $y, $thumbSizeWidth, $thumbSizeHeight, $width, $height);
/*CROP*/
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSizeWidth, $thumbSizeHeight, $smallestSide, $smallestSide);
//final output
imagejpeg($thumb, $imgtarg_d . '/' . $imgtarg,80);
imagedestroy($thumb);
}
이미지는 항상 (의도 한대로) 중심에서 잘립니다되지만 이미지가 폭 이상 400 픽셀을 경우는, 첫째 크기 조정 아니에요.
imagecopyresampled($thumb, $myImage, 0, 0, 0, $y, $thumbSizeWidth, $thumbSizeHeight, $width, $height);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSizeWidth, $thumbSizeHeight, $smallestSide, $smallestSide);
귀하의 코드는 아주 잘 작동보십시오! 그러나 나는 생각합니다. 편집 할 곳을 보여줄 수 있습니까? IF 너비가 높이보다 크다. 나는 높이를 자르고 싶지 않다. –