Page 1 of 1

Image upload and resize producing a black image.

Posted: Fri Oct 07, 2005 8:49 am
by Skittlewidth
We've moved a website over to a different server and now the following bit of code which uploads and resizes an image is failing somewhere. It works apart from producing a black image as a result, a problem which I know is common but conveniently can't find any posts on when I need to!

I'm not even sure if this script worked fully on its previous server as we only recently inherited this from the previous company so I didn't code it to begin with.
I think the problem is to do with the ImagePaletteCopy ($dimg, $simg) bit but I'm not sure. Can someone help? The server is running PHP 4.3.10.

Thanks

This is part of the the form handling script:

Code: Select all

if ($_FILES['newSectionImage']['tmp_name'])
        {
          list($width, $height, $type, $attr) = getimagesize($_FILES['newSectionImage']['tmp_name']);

          switch($type)
          {
            case 1:
              $extension = '.gif';
              $tempImage = imagecreatefromgif($_FILES['newSectionImage']['tmp_name']);
              break;
            case 2:
              $extension = '.jpg';
              $tempImage = imagecreatefromjpeg($_FILES['newSectionImage']['tmp_name']);
              break;
            case 3:
              $extension = '.png';
              $tempImage = imagecreatefrompng($_FILES['newSectionImage']['tmp_name']);
              break;
            default:
              die("<B>Error:</B> This image type is not supported, you must use a gif, jpeg or PNG file.");
          }

          // Set new image's height and width
          $new_width = 60;
          $new_height = round($height/($width/60));

          // Resample
          $newImage  = imagecreate($new_width, $new_height)
            or die("<B>Error: New image could not be created.</B> ");

          // Do a bicubic resample
          imageCopyResampleBicubic($newImage, $tempImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

          // Output
          $img = "../images/sections/section_$section$extension";
          $sectionImage = "section_$section$extension";
          imagejpeg($newImage, $img, 95)

            or die("<B>Error:</B> Image could not be saved.");

          // Destroy Images
          imagedestroy($newImage);
          imagedestroy($tempImage);
        }

        // Do MySQL to retrieve section data.
        $dbQuery = "UPDATE sections
                    SET sectionImage = '$sectionImage',
                        sectionTitle = '$sectionTitle',
                        sectionText = '$sectionText'
                    WHERE sectionID = $section";

        // Execute query
        $dbResult = mysql_query($dbQuery, $dbLink)
          or die("$dbQuery: The section data could not be updated.");
And this is the custom function that gets called.

Code: Select all

function imageCopyResampleBicubic (&$dimg, &$simg, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh)
  {
    ImagePaletteCopy ($dimg, $simg);
    $rX = $sw / $dw;
    $rY = $sh / $dh;
    $nY = 0;
    for ($y=$dy; $y<$dh; $y++)
    {
      $oY = $nY;
      $nY = round(($y + 1) * $rY);
      $nX = 0;

      for ($x=$dx; $x<$dw; $x++)
      {
        $oX = $nX;
        $nX = round(($x + 1) * $rX);
        $r = $g = $b = $a = 0;

        for ($i=$nY; --$i>=$oY;)
        {
          for ($j=$nX; --$j>=$oX;)
          {
            $c = ImageColorsForIndex ($simg, ImageColorAt ($simg, $j, $i));
            $r += $c['red'];
            $g += $c['green'];
            $b += $c['blue'];
            $a++;
          }
        }
        ImageSetPixel ($dimg, $x, $y, ImageColorClosest ($dimg, $r/$a, $g/$a, $b/$a));
      }
    }
  }

Posted: Fri Oct 07, 2005 9:09 am
by Skittlewidth
ignore me, I hadn't spotted it was using imagecreate instead of imagecreatetruecolor.

Thank goodness its Friday....

Posted: Fri Oct 07, 2005 9:52 am
by pickle
Just curious - any idea why imageCopyResampleBicubic() would be created rather than just using imagecopyresampled()?

Posted: Fri Oct 07, 2005 11:03 am
by Skittlewidth
No idea, but the same developer was doing something strange with the way he populated $PHP_SELF and also explicitly defined an existing php function. I think he may have been running PHP 4.0 or something.

Posted: Fri Oct 07, 2005 11:08 am
by feyd
yeah, it sounds like it was trying to support an older build.. using imagecreate() back then makes sense then too :)