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 114Warning: imagecopyresized() expects parameter 1 to be resource,
boolean given in … on line 115Warning: imagecopyresized() expects parameter 1 to be resource,
boolean given in … on line 116Warning: imagejpeg() expects parameter 1 to be resource, boolean given
in … on line 117Warning: imagejpeg() expects parameter 1 to be resource, boolean given
in … on line 118Warning: imagejpeg() expects parameter 1 to be resource, boolean given
in … on line 119Warning: imagedestroy() expects parameter 1 to be resource, boolean
given in … on line 120Warning: imagedestroy() expects parameter 1 to be resource, boolean
given … on line 121Warning: 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";}…
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 inimagecopyresized($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:
STEP BY STEP
I created a canvas with
then the white color with
and then just had to fill the canvas with the color using
Then I copy the uploaded image with
then copy the uploaded image onto the (now white colored) canvas
and save as jpg
and finally clear the cache
Hope it helps someone else too 🙂