.JPG 和 .jpg 之间有区别吗?
Is there a difference between .JPG and .jpg?
下面的代码生成缩略图并创建压缩图像和上传图像。
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; //3
define ("MAX_SIZE","400");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST") {
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
} else {
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024) {
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ) {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
} else if($extension=="png") {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
} else {
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=125;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$file_name = date('d') . date('M') . $_FILES['file']['name'];
$filename = "upload/gallery/". $file_name;
$filename1 = "upload/thumb/". $file_name;
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
}
结果是一些带有 .JPG 扩展名的图片存储或一些带有 .jpg 扩展名的图片存储
但是,主要问题是具有大写扩展名(特别是 .JPG)的文件无法在 Windows Live Photo Gallery 中打开或被识别。 .jpg 文件按预期工作。是什么导致了这个问题?
在对正确的扩展类型等进行所有测试之前,您将扩展名小写了,但是当您保存文件时,您使用的是原始 $_FILES['file']['name']
中的任何内容
$file_name = date('d') . date('M') . $_FILES['file']['name'];
所以在保存文件之前也对原始文件名执行 strtolower
$file_name = date('d') . date('M') . strtolower($_FILES['file']['name']);
下面的代码生成缩略图并创建压缩图像和上传图像。
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; //3
define ("MAX_SIZE","400");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST") {
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
} else {
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024) {
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ) {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
} else if($extension=="png") {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
} else {
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=125;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$file_name = date('d') . date('M') . $_FILES['file']['name'];
$filename = "upload/gallery/". $file_name;
$filename1 = "upload/thumb/". $file_name;
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
}
结果是一些带有 .JPG 扩展名的图片存储或一些带有 .jpg 扩展名的图片存储 但是,主要问题是具有大写扩展名(特别是 .JPG)的文件无法在 Windows Live Photo Gallery 中打开或被识别。 .jpg 文件按预期工作。是什么导致了这个问题?
在对正确的扩展类型等进行所有测试之前,您将扩展名小写了,但是当您保存文件时,您使用的是原始 $_FILES['file']['name']
$file_name = date('d') . date('M') . $_FILES['file']['name'];
所以在保存文件之前也对原始文件名执行 strtolower
$file_name = date('d') . date('M') . strtolower($_FILES['file']['name']);