Image upload and resize producing a black image.

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Image upload and resize producing a black image.

Post 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));
      }
    }
  }
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

ignore me, I hadn't spotted it was using imagecreate instead of imagecreatetruecolor.

Thank goodness its Friday....
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Just curious - any idea why imageCopyResampleBicubic() would be created rather than just using imagecopyresampled()?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yeah, it sounds like it was trying to support an older build.. using imagecreate() back then makes sense then too :)
Post Reply