0

我正在使用一个简单的脚本来上传和调整上传图像的大小,同时保持透明度问题是服务器无法识别 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。请问有什么想法吗?

4

2 回答 2

1

尝试这个:

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

您也可以像这样存储图片类型(但无论如何您都不会使用它)

$pic_type = ["main_img"]["type"]
于 2013-05-29T09:37:46.420 回答
0

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

于 2013-05-29T12:56:32.957 回答