开发者

After upload pictures are invalid

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->deb开发者_运维问答ug('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);
}


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜