Whenever I crop my image, it has a black background, even those I add the

$bg = imagecolorallocate ( $tci, 255, 255, 255 );
imagefill ( $tci, 0, 0, $bg );

I want a white background. Please help!

$tci = imagecreatetruecolor($w, $h);
$bg = imagecolorallocate ( $tci, 255, 255, 255 );
imagefill ( $tci, 0, 0, $bg );
imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h);
imagejpeg($tci, $newcopy, 84);

a sample image below

thumb_newegg3423423.jpg

    What is $img, seeing as you're completely covering the background with it? (Both source and background images are given as being w×h pixels, and the source image is being positioned with its top-left corner coinciding with the top-left corner of the background.)

      Weedpacket;11010003 wrote:

      What is $img, seeing as you're completely covering the background with it? (Both source and background images are given as being w×h pixels, and the source image is being positioned with its top-left corner coinciding with the top-left corner of the background.)

      Thank you for reply.
      img is the orginal img created, maybe let me post the code.
      actually I think that the problem is on crop image function. I check the resized image, and it is fine. However, after the image is cropped, it gives me a black background image. I have no idea why. 😕

      function ak_img_thumb($target, $newcopy, $w, $h, $ext) {
          list($w_orig, $h_orig) = getimagesize($target);
          $src_x = ($w_orig / 2) - ($w / 2);
          $src_y = ($h_orig / 2) - ($h / 2);
          $ext = strtolower($ext);
          $img = "";
          if ($ext == "gif"){
          $img = imagecreatefromgif($target);
          } else if($ext =="png"){ 
          $img = imagecreatefrompng($target);
          } else { 
          $img = imagecreatefromjpeg($target);
          }
          $tci = imagecreatetruecolor($w, $h);
      
                                      $bg = imagecolorallocate($tci, 255, 255, 255);
                                      imagefill($tci, 0, 0, $bg);
      
      
      imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h);
      
      
      
      if ($ext == "gif"){
          imagegif($tci, $newcopy);
      } else if($ext =="png"){ 
          imagepng($tci, $newcopy);
      } else {
          imagejpeg($tci, $newcopy, 90);
      }
      }
        bbmak wrote:

        I have no idea why.

        It might have something to do with what I said in my post from the fourth word on: when you copy the image over your background, you completely cover the background.

        actually I think that the problem is on crop image function.

        Well yes; that's because you're wanting to pad the image (with a white border) - the complete opposite of cropping.

        I think you've managed to get yourself confused about your sizes. Have you noticed, for example, that if the source image (the you're calling [font=monospace]$target[/font]) is smaller than [font=monospace]$w[/font]×[font=monospace]$h[/font] - the dimensions of the destination image (the one you're calling [font=monospace]$newcopy[/font]) - then [font=monospace]$src_x[/font] and [font=monospace]$src_y[/font] are negative? Or that you're trying destination-image-sized piece of the source image (which presumably is smaller, otherwise it wouldn't need padding)? Either way, you're trying to copy pixels from the source image that lie outside the source image. Those are the black ones you're getting that cover all your background padding.

          I think after I resize some of the image, the size of the sources are smaller than the new image.

          any suggestion for fixing it? I am totally confusing now. 😕

            First, try and state exactly what you want without any vagueness or sloppiness about what you're talking about. If you can't do that you can't write a program to do it.

            bbmak wrote:

            I think after I resize some of the image, the size of the sources are smaller than the new image.

            I don't understand what you mean. "Resize some of the image"? How do you resize "some" of an image? Do you mean you create a resized copy of part of an image? Which part? What image?

            bbmak wrote:

            I am totally confusing now.

            You're certainly starting to confuse me. All I can make out is that you want to copy an existing image into a new image. I know why you get a black background, but you haven't explained what your function is supposed to do well enough.

              Thank you for you advice.
              I took your suggestion and worked on the image size. Now it is working. I got white background.

                Write a Reply...