저는 PHP 놉입니다. 업로드 된 이미지를 작은 크기로 축소하는 이미지 크기 조정 스크립트를 만들려고합니다. 아래의 현재 스크립트를 사용하면 원본 이미지가 표시됩니다. 새로운 너비와 높이가 등록되어 있지 않습니다. 어떤 도움을 주셔서 감사합니다.PHP 이미지가 다른 마임 유형에 맞게 크기 조정
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
if (isset($_FILES['image']['tmp_name'])) {
if ($_FILES['image']['error']==0){
$new_dir = '/PHP_MySQL_Practice/uploaded/images';
$fullpath = $_SERVER['DOCUMENT_ROOT'].$new_dir;
if(!is_dir($fullpath)) {
mkdir($fullpath, 0777, TRUE);
}
//get file name and type(extension)
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
//separate image name after first "dot"
$separated = explode(".", $name);
//get string before the dot which was stored as the 1st item in the separated array.
$first = $separated[0];
//use php function pathinfo to get extension of image file
$ext = pathinfo($name, PATHINFO_EXTENSION);
//concatenate current timestamp (to avoid file overwrites) with the extracted file 'firstname' and add the extension
$name = time().'_'.$first.'.'.$ext;
$target = $fullpath.'/'.$name;
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
resizeImage($target, $name);
echo '<img src="' . $target . '"/>';
}
}
else {
echo 'there was an error in saving ur image file.';
}
}
else {
echo 'ur image could not be uploaded.';
}
}
else {
?>
<form method="POST" enctype="multipart/form-data">
<label>upload image: </label>
<input type="file" name="image" />
<input type="submit" />
</form>
<?php
}
//function to resize image
function resizeImage($dir, $img) {
list($src_w, $src_h) = getimagesize($dir);
$max_w = 150;
$max_h = 150;
if ($src_w > $max_w) {
$ratio = $max_w/$src_w;
$new_img_w = $src_w * $ratio;
}
else if ($src_h > $max_h) {
$ratio = $max_h/$src_h;
$new_img_h = $src_h * $ratio;
}
$img_mime_type = getimagesize($dir);
switch ($img_mime_type['mime']) {
case 'image/jpeg':
case 'image/pjpeg':
$src = imagecreatefromjpeg($dir);
return $src;
break;
case 'image/gif':
$src = imagecreatefromgif($dir);
return $src;
break;
case 'image/png':
$src = imagecreatefrompng($dir);
return $src;
break;
default:
return FALSE;
break;
}
$new_img = imagecreatetruecolor($new_img_w, $new_img_h);
imagecopyresampled($new_img, $src, 0, 0, 0, 0, $new_img_w, $new_img_h, $max_w, $max_h);
switch ($img_mime_type['mime']) {
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($new_img, $dir, 100);
break;
case 'image/gif':
imagegif($new_img, $dir);
break;
case 'image/png':
imagepng($new_img, $dir);
break;
default:
return FALSE;
break;
}
imagedestroy($src);
imagedestroy($new_img);
}
?>
예입니다 주어진 혀를 좀 봐, 이것이 당신이 원하는 도움이되기를 바랍니다 http://blog.webtech11.com/2012/04/21/upload-and-resize-an-image-with-php.html –