0

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);
}
7
  • Are you sure that the image exists?
    – Dr.Molle
    Jun 14, 2011 at 21:02
  • Is $org_img really pointing to an existing image file? (Maybe there is a typo or something?); Also, it's better to use getimagesize() for type detection, as it both helps with case sensitivity and checks for the actual content rather than filename extension.
    – mkilmanas
    Jun 14, 2011 at 21:03
  • I would recommend to make a clear example with one type of images. In your code example $type looks a little bit confusing to me.
    – powtac
    Jun 14, 2011 at 21:04
  • You can open the image file with a text editor and check if there is a (PHP) error at the beginning.
    – powtac
    Jun 14, 2011 at 21:05
  • So, I am sure that the image exists. I'm writing the type like this because in this field is a string with four characters long, and it may be'.jpg' but it may also be 'jpeg' so i have to be sure a point is made. Jun 14, 2011 at 21:05

1 Answer 1

1

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?

3
  • I think it won't help to post ALL of my code but I will try to post all of this which gets executed in this configuration. Jun 14, 2011 at 21:09
  • That is more what I meant, a bit more that is relevant to what is happening Jun 14, 2011 at 21:11
  • Just posted all relevant ways Jun 14, 2011 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.