0
다음 코드를 사용하여 이미지를 업로드하고 300x300 이미지와 50x50 축소판을 만듭니다. PHP 이미지 업로드/크기 조정으로 검은 색 이미지 생성
if ($_FILES['image_file']['errors'] > 0) {
die("<br /><center><strong>Error uploading file:</strong> " . $_FILES['image_file']['error'] . "</center><br />");
}
elseif ($_FILES['image_file']['size'] > 2500000) { // 2.5 megabyte limit
die("<br />center><strong>Your file is too large. Please upload a smaller file.</strong></center><br />");
}
switch (strtolower($_FILES['image_file']['type'])) {
case 'image/jpeg':
case 'image/pjpeg':
$img = imagecreatefromjpeg($_FILES['image_file']['tmp_name']);
break;
case 'image/png':
$img = imagecreatefrompng($_FILES['image_file']['tmp_name']);
break;
case 'image/gif':
$img = imagecreatefromgif($_FILES['image_file']['tmp_name']);
break;
default:
die("<br /><center><strong>Image file must be JPEG, PNG, or GIF.</strong></center><br />");
}
$orig_height = imagesy($img);
$orig_width = imagesx($img);
// Resize the image for profile
$scale = min(300/$orig_width, 300/$orig_height);
$new_height = ceil($scale * $orig_height);
$new_width = ceil($scale * $orig_width);
$new_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_with, $new_height, $orig_width, $orig_height);
imagejpeg($new_img, "images/$uid.jpg", 85);
imagedestroy($new_img);
// Reize the image for thumbnails
$scale = min(50/$orig_width, 50/$orig_height);
$new_height = ceil($scale * $orig_height);
$new_width = ceil($scale * $orig_width);
$new_thmb = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_thmb, $img, 0, 0, 0, 0, $new_with, $new_height, $orig_width, $orig_height);
imagejpeg($new_thmb, 'images/' . $uid . '_thumb.jpg', 85);
imagedestroy($new_thmb);
imagedestroy($img);
나는 imagecopyresampled
및 imagejpeg
호출의 출력을 에코
imagesx
및
imagesy
에서 올바른 이미지 크기를 점점 새로운 높이와 너비뿐만 아니라 정확한지 확인할 수 있습니다. 근본적으로 내가 말할 수있는 한, 크기가 조정 된 원본 이미지가 아닌 정확한 크기로 두 개의 검정색 사각형을 얻는 것을 제외하고는 모든 것이 제대로 작동하고 있습니다.