以下のスクリプトは、ユーザーが自分のアカウントにプロフィール写真をアップロードするためのものです。また、新しいプロフィール写真をアップロードしたい場合は、現在のプロフィール画像を削除します。イメージアップローダがInternet Explorerを除くすべてのブラウザで動作するのはなぜですか?
私が過去数ヶ月にわたって苦労してきたことは、すべてのブラウザで常に動作する画像アップロードフォームを構築することです。アップロードスクリプトをより堅牢にする方法について、より広い質問がありますが、特に大きな問題が1つあります。
なぜこのスクリプトはInternet Explorerで動作しませんか? Chrome、Firefox、Safariを介したアップロードはすべて正常に動作します。しかし、IEを使用して画像をアップロードすると、不正なファイルタイプであると拒否されます。
ここでは、この問題を解決しようとしている間に私が試した/調べたことがあります。私はまた私のphp.iniファイルでexif_imagetype()
を有効):
1)enctype="multipart/form-data"
が私のHTMLフォーム
2に含まれていたことを確認しましたが)(注意getimagesize()
とは対照的に、ファイルの種類を取得するためにexif_imagetype($file)
を使用してみました。私はこれがファイルタイプを決定するためのより信頼できる方法であると読んだ。この関数は他の3つのブラウザでも機能しますが、IEを使用する場合でもファイルの種類を判別できません。
3)私はvar_dump($_FILES)
を使用して、アップロードされているものを確認しました。これはIE以外のすべてのブラウザで名前、サイズ、タイプなどを表示します。 IE上には、アップロードされたファイルの名前がないようです。名前をエコー表示:"string '' (length=0)"
html形式、画像アップロードスクリプト、画像サイズ変更スクリプトはすべて下にあります。
FORM:
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="625000">
<input type="file" class="span2" name="image" id="image" size="20">
<input name="picture" type="submit" value="update" class="btn btn-primary" style="margin-bottom: -10px">
</form>
UPLOADのSCRIPT:
if (isset($_POST['picture'])){
//THIS SECTION IS FOR UPLOADING THE PROFILE PICTURE.
//It will create a standard profile image size 320 by 320.
//It also then creates a 'thumbnail' picture size 100 x 100
//delete the original profile image (if there was one)
$CurrentImage = getImage("profile",$id,FALSE);
$CurrentImageThumb = getImage("profile",$id,TRUE);
//if the current image is something other than the generic image, delete the current images
if($CurrentImage!="images/profile/generic.jpg")unlink($CurrentImage); if($CurrentImageThumb!="images/profile/genericthumb.jpg")unlink($CurrentImageThumb);
//get the type of the upload
$pic_type = ".".pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
//save the initial file
$saveto = "images/profile/$id"."$pic_type";
move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
//resize it and display the appropriate message if it works/doesn't work
//CREATE FULL SIZE
if (true === ($pic_error = image_resize($saveto,$saveto, 320, 320, 0))){
//CREATE THUMBNAIL
$saveto2 = "images/profile/$id"."thumb"."$pic_type";
if (true === ($pic_error = image_resize($saveto,$saveto2, 100, 100, 0))){
showAlert(2,"Your image has been uploaded!"," If the image did not change, <a href=\"./editprofile\">click here</a> to reload the page.");
}}else{
showAlert(3,"File Issue: ",$pic_error);
unlink($saveto);//delete the upload
unlink($saveto2);//delete the upload
}
}
画像リサイズSCRIPT:
function image_resize($src, $dst, $width, $height, $crop=0){
//if getimagesize doesn't output an array with the first two values being width and height, we reject the file
if(!list($w, $h) = getimagesize($src)) return "Unsupported file type. We only accept image files the extension .jpg, .jpeg, .png, .gif, or .bmp";
//get the image type based on the file extension
$type = strtolower(substr(strrchr($src,"."),1));
//based on file type, create a new image from the url
if($type == 'jpeg') $type = 'jpg';
switch($type){
case 'bmp': $img = imagecreatefromwbmp($src); break;
case 'gif': $img = imagecreatefromgif($src); break;
case 'jpg': $img = imagecreatefromjpeg($src); break;
case 'png': $img = imagecreatefrompng($src); break;
default : return "Unsupported file type. We only accept image files with the extension .jpg, .jpeg, .png, .gif, or .bmp";
}
// resize (get the dimensions for resize if $crop or not)
if($crop){
//if initial image is smaller than crop size display error
if($w < $width or $h < $height) return "Picture is too small. Your image must be LARGER than ".$width."pixels by ".$height." pixels.";
$ratio = max($width/$w, $height/$h);
$h = $height/$ratio;
$x = ($w - $width/$ratio)/2;
$w = $width/$ratio;
}
else{
if($w < $width and $h < $height) return "Picture is too small. Your image must be LARGER than ".$width."pixels by ".$height." pixels.";
$ratio = min($width/$w, $height/$h);
$width = $w * $ratio;
$height = $h * $ratio;
$x = 0;
}
//create a new true color image
$new = imagecreatetruecolor($width, $height);
// preserve transparency
if($type == "gif" or $type == "png"){
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}
//COPY AND RESIZE THE IMAGE
/**imagecopyresampled (resource $dst_image , resource $src_image , int $dst_x , int $dst_y ,
int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h)
imagecopyresampled() will take an rectangular area from src_image of width src_w and height src_h at
position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).
*/
imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
//then output the image to the file
switch($type){
case 'bmp': imagewbmp($new, $dst); break;
case 'gif': imagegif($new, $dst); break;
case 'jpg': imagejpeg($new, $dst); break;
case 'png': imagepng($new, $dst); break;
}
return true;
}
「なぜ動作しないのですか」という質問に答えるには、コードをデバッグする必要があります –
私のローカルサイトでエラー報告を有効にしました。 getimagesize(images/profile/151)[function.getimagesize]:ストリームを開くことに失敗しました:C:\ www \ local \ php \ image.phpの25行目にこのようなファイルやディレクトリはありません – Jake
もっと徹底的にデバッグする方法についての提案はありますか? – Jake