上传后图片无效

发布于 2024-11-15 11:00:14 字数 4407 浏览 5 评论 0原文

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

我上传一张图片,然后寻找类型(允许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 创建了图像,所以它们实际上必须是有效的。

感谢您的帮助。

Flo

编辑:一些代码:

$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);
}

I got a problem with a web upload. It is as follows:

I upload a picture and I look for the type (allowed jpeg, jpg, gif and png). Now I cut a part out of it and save it on a temporary resource which is created by the information of the type (if the type is jpg or jpeg, I use imagejpeg(), with PNG i use imagepng() and with gif I use imagegif()). Now this works. Then I save the images again.

And then I re-open them by imagecreatefromjpeg/-png/-gif. And then I get the error

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

Line 88 looks as follows:

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

...

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

The same error happens also with png, but not with jpeg (because I wrote the following statement at the beginning:

ini_set('gd.jpeg_ignore_warning', 1);

). Jpeg warnings seem to be deactivated, but not the warnings for png and gif. And I created the image with mspaint, so they actually have to be valid.

Thanks for help.

Flo

EDIT: some code:

$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() . ")");

So now I got the file saved and the information in my table.

Now I'm linking to another file...

$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));
}

And now another redirect ....

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.
}

And the called function 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住我的手 2024-11-22 11:00:14

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

There is not too much to work from but sometimes when you use a process like you are PHP changes the image header to JFIF (JPEG) instead of GIF98a (GIF) so run a check on the header before you use imagecreatefrom....
Hope this helps a little bit, maybe more code would help us all?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文