上传图片后无效

时间:2011-06-14 20:56:22

标签: php gd

我遇到网络上传问题。它如下:

我上传了一张图片,我查找了类型(允许jpeg,jpg,gif和png)。现在我从中删除了一部分并将其保存在由类型信息创建的临时资源上(如果类型为jpg或jpeg,我使用imagejpeg(),使用PNG我使用imagepng()和gif我使用imagegif())。现在这个工作。然后我再次保存图像。

然后我通过imagecreatefromjpeg / -png / -gif重新打开它们。然后我收到错误

Warning: imagecreatefromgif() [function.imagecreatefromgif]: 'uploads/gif/test.gif' is not a valid GIF file in /home/blabla/sliceit.php on line 88

第88行看起来如下:

$org_img = 'uploads/' . $name . "/" . $rand . (substr($type,0,1) != "." ? "." . $type : $type);

...

87: elseif ($type == ".gif") {
88:     $src_img = imagecreatefromgif($org_img);
89: }

同样的错误也发生在png上,但不是jpeg(因为我在开头写了下面的语句:

ini_set('gd.jpeg_ignore_warning', 1);

)。 Jpeg警告似乎已停用,但不是png和gif的警告。我用mspaint创建了图像,所以它们实际上必须是有效的。

感谢您的帮助。

弗洛

编辑:一些代码:

$name = 'something';
$filetype = substr($_FILES['datei']['name'],-4,4);
$filetype = strtolower($filetype);
$randomsessid = randomstring(60);
mkdir('uploads/' . $name);
move_uploaded_file($_FILES['datei']['tmp_name'],'uploads/' . $name . '/' . $randomsessid . (substr($filetype,0,1) == "." ? $filetype : "." . $filetype));
mysql_query("INSERT INTO SESSIONS VALUES('','" . $name . "','" . $randomsessid . "','" . strtolower($filetype) . "'," . time() . ")");

所以现在我保存了文件和表格中的信息。

现在我正在链接到另一个文件......

$id = mysql_real_escape_string($_GET["randid"]); //here I get the randomstring
if ($id == "") {
    exit;
} 
$unf = mysql_query("SELECT NAME, TYP FROM SESSIONS WHERE RANDOM = '" . $id . "'");
if (mysql_num_rows($unf) == 1) {
    $f = mysql_fetch_object($unf);
    $name = $f->NAME;
    $filetype = $f->TYP;
}
else {
    exit;
}
$image_resize = new image_resize; //this is a very useful class to resize images

$size = $_GET["size"]; //here is 'auto' inside
$log->debug('size: ' . $size);
if ($size == "custom" and isset($_GET["x"]) and isset($_GET["y"])) {
    //blabla some code...
}
else {
    $image_resize->load("uploads/" . $name . "/" . $id . (substr($filetype,0,1) == "." ? $filetype : "." . $filetype));    
    $image_resize->resize(600,600);
    $image_resize->save("uploads/" . $name . "/" . $id . (substr($filetype,0,1) == "." ? $filetype : "." . $filetype));
}

现在另一个重定向......

ini_set('gd.jpeg_ignore_warning', 1);
$id = $_GET["randid"];
if ($id == "") {
    exit;
}
$tempsel = "SELECT * FROM SESSIONS WHERE RANDOM = '" . $id . "'";
$unf = mysql_query($tempsel);
if (mysql_num_rows($unf) != 1) {
    $log->debug('tempsel: ' . $tempsel);
    exit;
}
$f = mysql_fetch_object($unf);
$name = $f->NAME;
$type = $f->TYP;
for ($i = 1; $i <= 9; $i++) {
    createImagePart($i,$name,$type,$id,$log); //$i = for loop, $name = the name from the beginning, $type defined, $id = random id, $log = a previously defined log class.
}

调用函数createImagePartI():

function createImagePart($nr,$name,$type,$id,$log) {
    if (!isFolderSet($id . "/parts/")) {
        mkdir("uploads/" . $id );
        mkdir("uploads/" . $id . "/parts"); 
    }
    //prepare params....
    $org_img = 'uploads/' . $name . "/" . $id . (substr($type,0,1) != "." ? "." . $type : $type);
    $dst_img = 'uploads/' . $id . "/parts/" . $nr .  (substr($type,0,1) != "." ? "." . $type : $type);
    $tmp_img = imagecreatetruecolor(200, 200);
    if ($type == ".jpg" or $type == "jpeg") {
        $src_img = imagecreatefromjpeg($org_img);
    }
    elseif ($type == ".png") {
        $src_img = imagecreatefrompng($org_img);
    }
    elseif ($type == ".gif") {
        $src_img = imagecreatefromgif($org_img);
    }
    else {
        exit;
    }
    $sX = ($nr-1)%3 * 200;          //// watch this question:
    $sY = floor(($nr-1)/3) * 200;   //// http://stackoverflow.com/questions/6325169/variable-has-unexpected-value
    imagecopy($tmp_img, $src_img, 0,0, $sX, $sY, 200, 200);
    if ($type == ".jpg" or $type == "jpeg") {
        imagejpeg($tmp_img, $dst_img,100);  // because of ini_set i dont get an error here
    }
    elseif ($type == ".png") {
        imagepng($tmp_img, $dst_img, 0);    //on these functions, I get the errors
    }
    else {
        imagegif($tmp_img, $dst_img);       //also here i get an error
    }
    imagedestroy($tmp_img);
}

1 个答案:

答案 0 :(得分:1)

没有太多工作,但有时当您使用像PHP一样的过程时,PHP将图像标题更改为JFIF(JPEG)而不是GIF98a(GIF),因此在使用imagecreatefrom之前对标题运行检查。 .. 希望这有点帮助,也许更多的代码可以帮助我们所有人?