2017-04-30 7 views
1

HTMLとPHPで画像をアップロードしています。アップロードした後ではなく、アップロード時にイメージのサイズを変更しますか?

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

は、どのように私はそれが最初の、そして画像を常駐来る大きい方1500(幅)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); 
関連する問題