Sometimes, i get tired of my butt-lazy codestyle!

Post anything on anything here

Moderator: Moderators

Sometimes, i get tired of my butt-lazy codestyle!

Postby tChr » Tue Jul 25, 2006 11:39 pm

I invite you all to find the error here:

Code: Select all
   // find the resize factor.
   // This heavily depends on what method we choose for image overflow
   // in the future this will also depend on weither we resize up, down or both
   $dostretch = false;
   if ($settings["overflow"] == "stretch") {
      $dostretch = true;
   } elseif ($settings["overflow"] == "cut") {
      if (((($ox = imagesx($oim)) / $settings["width"]) > (($oy = imagesy($oim)) / $settings["height"]))) {
         $resizefactor = $oy / $settings["height"];
      } else {
         $resizefactor = $ox / $settings["width"];
      }
   } else {
      if (((($ox = imagesx($oim)) / $settings["width"]) > (($oy = imagesy($oim)) / $settings["height"]))) {
         $resizefactor = $ox / $settings["width"];
      } else {
         $resizefactor = $oy / $settings["height"];
      }
   }

   // do the image resize
   if ($dostretch) {
         imagecopyresampled($nim, $oim, 0, 0, 0, 0, $settings["width"], $settings["height"], $ox, $oy);
   } else {
         imagecopyresampled($nim, $oim, (($settings["width"] - ($ox/$resizefactor)) /2), (($settings["height"] - ($oy/$resizefactor)) /2), 0, 0, ($ox/$resizefactor), ($oy/$resizefactor), $ox, $oy);
   }

   // lay over text
   if ($settings["textoverlay"]["text"]) {
      if ($dostretch) {
         $ctx = 4;
         $cty = $settings["height"] - 17;
      } else {
         $ctx = ((($settings["width"] - ($ox/$resizefactor)) /2) + 4);
         $cty = ($settings["height"] - ((($settings["height"] - ($oy/$resizefactor)) /2)) - 17);
      }
      imagestring($nim, 2, $ctx, $cty, $settings["textoverlay"]["text"], $nimcolor["white"]);
   }



This is some PGP/GD code for resizing an image.

The resize makes sure that an image is resized onto a new canvas (an image with a set height and width) but contains its proportions, either by cutting the overflow, or by resizing the image to fint, then adding a border around it.

The new goal here is that if $settings["overflow"] == "stretch", the image should be stretched instead of maintaining its proportions, then the text from $settings["textoverlay"]["test"] should be added to the bottom corner of the image, no metther where the image is placed, cause if the image does not fill the new image out, the text overlay should follow the placement of the resized image, not the canvas. This all works, so dont mind that. (teh fallback for neither cut nor stretch is "fill" btw, thats the default fallback)

The error is as this: If you do a "stretch" the image is just black.

Why? (i know it.. its just a mind-challenge)

and btw: this code is copyright apario my emplyer and blahbalhblah.
the spice extend life!
the spice expand conciousness!
the spice is vital to space travel!
sooooo.. tell me what you want, waht you really-really want
I will proceed directly to the intravenous injection of hard drugs, please.
tChr
Forum addon
 
Posts: 1501
Joined: Tue Feb 28, 2006 9:11 pm
Location: Trondheim, Norway

Postby tChr » Tue Jul 25, 2006 11:44 pm

a hint:

If I add the "find resize factor" code in the
Code: Select all
if ($settings["overflow"] == "stretch") {
  $dostretch = true;
}

like this:
Code: Select all
if ($settings["overflow"] == "stretch") {
  $dostretch = true;
  if (((($ox = imagesx($oim)) / $settings["width"]) > (($oy = imagesy($oim)) / $settings["height"]))) {
    $resizefactor = $oy / $settings["height"];
  } else {
    $resizefactor = $ox / $settings["width"];
  }

}

it works.. but thats weird, cause you see later that you dont use those valuse if you have a $dostretch
the spice extend life!
the spice expand conciousness!
the spice is vital to space travel!
sooooo.. tell me what you want, waht you really-really want
I will proceed directly to the intravenous injection of hard drugs, please.
tChr
Forum addon
 
Posts: 1501
Joined: Tue Feb 28, 2006 9:11 pm
Location: Trondheim, Norway

Postby divVerent » Wed Jul 26, 2006 6:43 am

PHP doesn't spit out an error or at least a warning when you use the undefined variables $ox or $oy? Strange language. Even Perl does that.
1. Open Notepad
2. Paste: ÿþMSMSMS
3. Save
4. Open the file in Notepad again

You can vary the number of "MS", so you can clearly see it's MS which is causing it.
divVerent
Site admin and keyboard killer
 
Posts: 3809
Joined: Thu Mar 02, 2006 4:46 pm
Location: BRLOGENSHFEGLE

Postby obi_wan » Wed Jul 26, 2006 7:45 am

ok i'm not a php specialist, i'm more at ease with ecmascript languages, but if you assume the first conditional tests is passed :

Code: Select all
if ($settings["overflow"] == "stretch") {
   $dostretch = true;
}


, the "else if" and the "else" aren't executed.

Code: Select all
elseif ($settings["overflow"] == "cut") {
   if (((($ox = imagesx($oim)) / $settings["width"]) > (($oy = imagesy($oim)) / $settings["height"]))) {
      $resizefactor = $oy / $settings["height"];
   } else {
      $resizefactor = $ox / $settings["width"];
   }
} else {
   if (((($ox = imagesx($oim)) / $settings["width"]) > (($oy = imagesy($oim)) / $settings["height"]))) {
      $resizefactor = $ox / $settings["width"];
   } else {
      $resizefactor = $oy / $settings["height"];
   }
}


So that is not done :
Code: Select all
$oy = imagesy($oim)




Now in that part :
Code: Select all
if ($dostretch) {
         imagecopyresampled($nim, $oim, 0, 0, 0, 0, $settings["width"], $settings["height"], $ox, $oy);
}


You use the $ox variable, which is not defined if the first conditional test was true (value = stretch)

so i think its why it doesn't work, and also why your 'weird fix' fixes the problem ;)
[NSB] ppwer !
obi_wan
Alien trapper
 
Posts: 256
Joined: Mon Mar 13, 2006 9:24 am
Location: France

Postby parksto » Wed Jul 26, 2006 7:50 am

divVerent wrote:PHP doesn't spit out an error or at least a warning when you use the undefined variables $ox or $oy? Strange language. Even Perl does that.


with php, you can define the display error level with the simple function error_reporting(E_ALL) (example for displaying all errors) or in php.ini file.
Code: Select all
; - error_reporting = E_ALL        [Code Cleanliness, Security(?)]
;     By default, PHP surpresses errors of type E_NOTICE.  These error messages
;     are emitted for non-critical errors, but that could be a symptom of a bigger
;     problem.  Most notably, this will cause error messages about the use
;     of uninitialized variables to be displayed.



php is a nice language ;)
parksto
Member
 
Posts: 25
Joined: Sun May 07, 2006 1:30 pm

Re: Sometimes, i get tired of my butt-lazy codestyle!

Postby obi_wan » Wed Jul 26, 2006 7:51 am

that should actually work : (didn't test :p )

Code: Select all
   // find the resize factor.
   // This heavily depends on what method we choose for image overflow
   // in the future this will also depend on weither we resize up, down or both
$dostretch = false;
$ox = imagesx($oim);
$oy = imagesy($oim);
if ($settings["overflow"] == "stretch") {
   $dostretch = true;
} elseif ($settings["overflow"] == "cut") {
   if (($ox / $settings["width"]) > ($oy / $settings["height"])) {
      $resizefactor = $oy / $settings["height"];
   } else {
      $resizefactor = $ox / $settings["width"];
   }
} else {
   if (($ox / $settings["width"]) > ($oy / $settings["height"])) {
      $resizefactor = $ox / $settings["width"];
   } else {
      $resizefactor = $oy / $settings["height"];
   }
}
[NSB] ppwer !
obi_wan
Alien trapper
 
Posts: 256
Joined: Mon Mar 13, 2006 9:24 am
Location: France

Postby tChr » Wed Jul 26, 2006 1:47 pm

Excalty.. this is, as I said a code style problem.

I do, _often_ initialize variables at the same time as I test them. This is allowed in PHP, its also allowed in C, (well, not initialzing, but setting value, while testing).

So.. Obi is completely correct.

that was quick :)

the working code is as follows:
Code: Select all

    // find the resize factor.
    // This heavily depends on what method we choose for image overflow
    // in the future this will also depend on weither we resize up, down or both
    $dostretch = false;
    if ($settings["overflow"] == "stretch") {
        $ox = imagesx($oim);
        $oy = imagesy($oim);
        $dostretch = true;
    } elseif ($settings["overflow"] == "cut") {
        if (((($ox = imagesx($oim)) / $settings["width"]) > (($oy = imagesy($oim)) / $settings["height"]))) {
            $resizefactor = $oy / $settings["height"];
        } else {
            $resizefactor = $ox / $settings["width"];
        }
    } else {
        if (((($ox = imagesx($oim)) / $settings["width"]) > (($oy = imagesy($oim)) / $settings["height"]))) {
            $resizefactor = $ox / $settings["width"];
        } else {
            $resizefactor = $oy / $settings["height"];
        }
    }


obis way is a lil cleaner, but the fetaures of this lib has ben extended gradually, so there is a bit of CP coding in there :)
the spice extend life!
the spice expand conciousness!
the spice is vital to space travel!
sooooo.. tell me what you want, waht you really-really want
I will proceed directly to the intravenous injection of hard drugs, please.
tChr
Forum addon
 
Posts: 1501
Joined: Tue Feb 28, 2006 9:11 pm
Location: Trondheim, Norway


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron