Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7657925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:07:46+00:00 2026-05-31T13:07:46+00:00

I use php imagecolorallocate() and imagefill() in an image upload to also let png

  • 0

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";}…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:07:48+00:00Added an answer on May 31, 2026 at 1:07 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use PHP and have an array that looks like this: Array ( [34]
I need to do use php/regex to replace this: {{image-4-m}} to this: <img src=4.jpg
I use PHP's PDO to connect to MySQL. I have this code for connection:
I have php code to trim white outer border and resize. When I use
use PHP and MySQL. Want my website to have the feature of image uploading
I use PHP and jQuery. I'm trying to do something like this: $(this +
I didn't use PHP in a while, but I've tried something like this: <?php
I'm trying to use PHP's split() (preg_split() is also an option if your answer
Use PHP and MySQL. I have an array, P, which contains the value of
I have a PHP script that creates a very tall image and draws a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.