2017-04-30 2 views
1

HTML과 PHP로 이미지를 업로드하고 있습니다.업로드 후 업로드하지 않고 이미지 크기를 조정 하시겠습니까?

<form action="" method="post"> 
    <input type="file" name="image" id="image"> 
</form> 

어떻게 먼저, 다음 이미지를 상주 오는 큰 중 1,500 (폭) X700 (높이)보다 큰 경우 이미지 크기를 조정하는 ImageMagick이를 사용할 수 있습니다.

내가 찾은만큼, imagemagick은 업로드 후에 이미지 크기를 조정할 수 있습니다. 업로드하는 동안 이미지의 크기를 조정 한 다음 디렉토리/폴더에 저장할 수 있습니까?

답변

1

임시 파일의 크기를 조정 한 다음 파일을 완료 한 후에 저장할 수 있습니다.

여기에 내가 일반적으로 다루는 방법이 나와 있습니다. 이보다 더 많은 것을 할 필요가 있습니다. .. 내가 크기를 조정하기 위해이 기능을 사용

..

function img_resize($target, $newcopy, $w, $h, $ext) { 
list($w_orig, $h_orig) = getimagesize($target); 
$scale_ratio = $w_orig/$h_orig; 
if (($w/$h) > $scale_ratio) { 
    $w = $h * $scale_ratio; 
} else { 
    $h = $w/$scale_ratio; 
} 
$img = ""; 
$ext = strtolower($ext); 
if ($ext == "gif"){ 
    $img = imagecreatefromgif($target); 
} else if($ext =="png"){ 
    $img = imagecreatefrompng($target); 
} else { 
    $img = imagecreatefromjpeg($target); 
} 
$tci = imagecreatetruecolor($w, $h); 
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, 
dst_h, src_w, src_h) 
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig); 
imagejpeg($tci, $newcopy, 80); 
} 

가 그럼 난 임시 파일로 함수를 호출을 .. 당신이 업로드 허용 유형, 크기 요법을 확인하고 있는지 확인

$fileName = $_FILES["image"]["name"]; // The file name 
$target_file = $_FILES["image"]["tmp_name"]; 
$kaboom = explode(".", $fileName); // Split file name into an array using the dot 
$fileExt = end($kaboom); // Now target the last array element to get the file extension 
$fname = $kaboom[0]; 
$exten = strtolower($fileExt); 

$resized_file = "uploads/newimagename.ext"; //need to change this make sure you set the extension and file name correct.. you will want to secure things up way more than this too.. 
$wmax = 1500; 
$hmax = 700; 
img_resize($target_file, $resized_file, $wmax, $hmax, $exten); 
관련 문제