在线服务器无法发现jpeg图像类型,而本地主机可以


online server can not discover jpeg image type while localhost does

我使用一个简单的脚本来上传和调整上传的图像,同时保持透明度的问题是服务器不识别jpeg图像,而localhost这里是代码

function image_resize($src, $dst, $width, $height, $crop=0){
if(!list($w, $h) = getimagesize($src)) return "Unsupported picture type 1!";
$type = strtolower(substr(strrchr($src,"."),1));
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 picture type 2!";
}
$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);
}
imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
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;
}    

这里是上传代码

require('_req/func.php');       
$img_name = mysql_real_escape_string($_FILES['main_img']['name']);
if(strstr($img_name," ")){
$img_name = str_replace(" ","_",$img_name); 
}
$num = substr(md5(mt_rand(1,999999)),0,4);
$new_name = $num.$img_name;
move_uploaded_file($_FILES['main_img']['tmp_name'], "../products_large/".$new_name);
$pic_type = strtolower(strrchr($_FILES['main_img']['name'],"."));
$pic_name = "../products_large/".$new_name;
if (true !== ($pic_error = @image_resize($pic_name, "../products_thumb/".$new_name, 180, 180, 1))) {
echo $pic_error;
unlink($pic_name);
 }
    else {
require_once('_req/base.php');
$addNameQ = "update products set Product_Img = '$new_name' where Product_ID = '$id'";
$addNameR = mysql_query($addNameQ);
mysql_close($connect);
}

我得到的错误是

Unsupported picture type 2!

在函数代码的第10行,但本地主机可以上传相同的图像没有任何问题,当返回$_FILES["main_img"]["type"]我得到图像/jpeg。有什么想法吗?

试试这个:

     $type = strtolower(pathinfo($src, PATHINFO_EXTENSION));

你也可以像这样存储pic类型(但你不使用它)

$pic_type = ["main_img"]["type"]

问题是在打开数据库连接之前使用mysql_real_escape_string,它应该在打开连接后使用