PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Aug 09, 2020 6:32 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Oct 07, 2005 8:49 am 
Offline
Forum Contributor
User avatar

Joined: Wed Nov 06, 2002 10:18 am
Posts: 389
Location: Kent, UK
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:
Syntax: [ Download ] [ Hide ]
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.

Syntax: [ Download ] [ Hide ]
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));

      }

    }

  }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 9:09 am 
Offline
Forum Contributor
User avatar

Joined: Wed Nov 06, 2002 10:18 am
Posts: 389
Location: Kent, UK


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 9:52 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 11:03 am 
Offline
Forum Contributor
User avatar

Joined: Wed Nov 06, 2002 10:18 am
Posts: 389
Location: Kent, UK
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 11:08 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
yeah, it sounds like it was trying to support an older build.. using imagecreate() back then makes sense then too :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group