사진의 크기를 조정하고 자르려고합니다.PHP로 사진 자르기가 늘어납니다.
사진은 가로 또는 세로 방향 일 수 있지만 크기가 250x250이고 뻗어 있지 않길 원합니다 (측면 또는 상단/하단 자르기를 유지하지만 이미지의 대부분을 새 크기로 유지하고 정사각형으로 자른 경우). WordPress의 미리보기 이미지).
이 내가 무슨 일을하고있는 중이 야
... 그것은 적당한 크기하지만이 사진을 스트레칭입니다, 내 기능입니다?
function make_thumb($src, $dest, $desired_width, $ext) {
if ($ext == 'jpg' || $ext == 'jpeg') {
$source_image = imagecreatefromjpeg($src);
}
if ($ext == 'png') {
$source_image = imagecreatefrompng($src);
}
if ($ext == 'gif') {
$source_image = imagecreatefromgif($src);
}
/* read the source image */
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width/$width));
$desired_height = 250;
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
if ($ext == 'jpg' || $ext == 'jpeg') {
imagejpeg($virtual_image, $dest);
}
if ($ext == 'png') {
imagepng($virtual_image, $dest);
}
if ($ext == 'gif') {
imagegif($virtual_image, $dest);
}
}
고마워요!
일단 이미지 크기를 조정하면 자르기에'imagecopy'를 사용할 수 있습니까? –
@Tom No, 크기를 복사 할 때 이미지를 왜곡하면 아무 것도 고칠 수 없습니다. – RiggsFolly
@ RiggsFolly 당신 말이 맞아요. 나는 크기 조정이 올바른 비율로 행해질 것이라고 가정하고있었습니다. 'imagecopyresampled'도 자르기에 사용할 수 있기 때문에'imagecopy'를 사용하는 것은 잔상을 보존하고 싶지 않고 자른 미리보기 이미지 만 남기고 싶다면 중복되는 단계입니다. 그러나 우리는 그것을 모른다. –