0
votes

I use php imagecolorallocate() and imagefill() in an image upload to also let png files have a white background (like in this post: imagescreatetruecolor with a white background)

Here is the part of the code it concerns:

    //create new images
    $nimgac_0=imagecreatetruecolor($maxw_img0,$maxh_img0); //img1
    $nimgac_1=imagecreatetruecolor($maxw_img1,$maxh_img1); //img2
    $nimgac_2=imagecreatetruecolor($maxw_img2,$maxh_img2); //img3

    $nimgaa_0=imagecolorallocate($nimgac_0,255,255,255);
    $nimgaa_1=imagecolorallocate($nimgac_1,255,255,255);
    $nimgaa_2=imagecolorallocate($nimgac_2,255,255,255);

    $nimga_0=imagefill($nimgac_0,0,0,$nimgaa_0);
    $nimga_1=imagefill($nimgac_1,0,0,$nimgaa_1);
    $nimga_2=imagefill($nimgac_2,0,0,$nimgaa_2);

    //create images from temp folder
        if ($type=="jpg") {
            $nimgb_0=imagecreatefromjpeg("../imga/".$_FILES['file']['name']);
            $nimgb_1=imagecreatefromjpeg("../imga/".$_FILES['file']['name']);
            $nimgb_2=imagecreatefromjpeg("../imga/".$_FILES['file']['name']);
        }

        if ($type=="png") {
            $nimgb_0=imagecreatefrompng("../imga/".$_FILES['file']['name']);
            $nimgb_1=imagecreatefrompng("../imga/".$_FILES['file']['name']);
            $nimgb_2=imagecreatefrompng("../imga/".$_FILES['file']['name']);
        }


    imagecopyresized($nimga_0,$nimgb_0,0,0,0,0,$nwidth_0,$nheight_0,$width,$height);
    imagecopyresized($nimga_1,$nimgb_1,0,0,0,0,$nwidth_1,$nheight_1,$width,$height);
    imagecopyresized($nimga_2,$nimgb_2,0,0,0,0,$nwidth_2,$nheight_2,$width,$height);

    imagejpeg($nimga_0,"../imga/".$_FILES['file']['name'],80);
    imagejpeg($nimga_1,"../imga/".$imgname_1,80);
    imagejpeg($nimga_2,"../imga/".$imgname_2,80);

but I get this warnings:

Warning: imagecopyresized() expects parameter 1 to be resource, boolean given in ... on line 114

Warning: imagecopyresized() expects parameter 1 to be resource, boolean given in ... on line 115

Warning: imagecopyresized() expects parameter 1 to be resource, boolean given in ... on line 116

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in ... on line 117

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in ... on line 118

Warning: imagejpeg() expects parameter 1 to be resource, boolean given in ... on line 119

Warning: imagedestroy() expects parameter 1 to be resource, boolean given in ... on line 120

Warning: imagedestroy() expects parameter 1 to be resource, boolean given ... on line 121

Warning: imagedestroy() expects parameter 1 to be resource, boolean given in ... on line 122

Same code without the immagecolorallocate() and imagefill() works perfectly fine. However, I cannot find any error or any difference to the above posted code.

Anyone any ideas? Thank you in advance!

PS: I want to save all images as jpg, that's why I convert pngs to jpg too.

EDIT 3 (sorry, I got confused):

print_r(getimagesize($_FILES['file']['tmp_name'])); returns

Array ( [0] => 354 [1] => 332 [2] => 2 [3] => width="354" height="332" [bits] => 8 [channels] => 3 [mime] => image/jpeg )

so, everything is fine with if($size['2']==3) {$type="jpg";}...

2

2 Answers

1
votes

"Boolean given" usually means that an operation failed, and instead of returning a GD handle, returned a boolean false. You then use this false value elsewhere in another GD call, and get that warning.

So look at whatever your line 114 is in that code, figure out where the GD handle you're using there came from, and then figure out why the operation that should've generated that handle produced a false value instead.


ok, with what's come out of the comments below, here's your problem:

$size = getimagesize($_FILES[ blah blah blah]);

returns an array. You then compare this array against an int:

$size == 2

which will fail. So $type comes out to be blank. That means NONE of the imagecreatefromXXX() functions are called, and your $nimgb_XXX image handles never get defined.

However, you pass those undefined handle variables into the various other GD functions, and then you get your warning. Somehow the auto-created null values that PHP puts into those variables is being reported as a FALSE by the GD library.

Your code should be this:

$info = getimagesize(...);
switch($info['type']) {
    case IMGTYPE_JPG:
         $nimgb_0=imagecreatefromjpeg("../imga/".$_FILES['file']['name']);
         $nimgb_1=imagecreatefromjpeg("../imga/".$_FILES['file']['name']);
         $nimgb_2=imagecreatefromjpeg("../imga/".$_FILES['file']['name']);
         break;
    case IMGTYPE_PNG:
         etc....
    default:
         die("Unsupported file type");
}

The IMGTYPE_... constants are documented here: http://php.net/manual/en/image.constants.php

1
votes

I have finally found the answer to my problem and thought I should post it here step by step so if someone should have the same problem one day, here is what I figured out:

Basically, I had $nimga_0=imagefill($nimgac_0,0,0,$nimgaa_0); in my code, whereas imagefill is a bool. So, imagefill was successful and I assigned its value 1 to $nimga_0, and then wanted to use 1 as a resource in imagecopyresized($nimga_0,$nimgb_0,0,0,0,0,$nwidth_0,$nheight_0,$width,$height);.

Of course, this cannot be a valid source. All I had to change was leaving out the vars and just do:

imagefill($nimgac_0,0,0,$nimgaa_0);

STEP BY STEP

I created a canvas with

$nimgac_0=imagecreatetruecolor($maxw_img0,$maxh_img0);

then the white color with

$nimgaa_0=imagecolorallocate($nimgac_0,255,255,255);

and then just had to fill the canvas with the color using

imagefill($nimgac_0,0,0,$nimgaa_0);

Then I copy the uploaded image with

$nimgb_0=imagecreatefromjpeg("../imga/".$_FILES['file']['name']);

then copy the uploaded image onto the (now white colored) canvas

imagecopyresized($nimgac_0,$nimgb_0,0,0,0,0,$nwidth_0,$nheight_0,$width,$height);

and save as jpg

imagejpeg($nimgac_0,"../imga/".$_FILES['file']['name'],80); 

and finally clear the cache

imagedestroy($nimgac_0);

Hope it helps someone else too :)