The source below works fine if I call it from a page that just
outputs the code, but if I try to output this in a site where
output has already started prior to this code, I get garbble.

function create_thumb() {
	global $filename;

$imgfile = $filename;
Header("Content-type: image/jpeg");

list($width, $height) = getimagesize($imgfile);
$newheight = 80;
$newwidth = (int) (($width*$newheight)/$height);

$thumb = imagecreatetruecolor($newwidth,$newheight);
$source = imagecreatefromjpeg($imgfile);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb);
}

I get the following:

ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC    $.' ",#(7),01444'9=82<.342ÿÛC  2!!22222222222222222222222222222222222222222222222222ÿÀPj"ÿÄ ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚâãäåæçèéêòóôõö÷øùúÿÚ

Here is the accual output: http://206.124.138.203/gallery/index.php?page=thumbs&catID=1
How do I get around this issue?
Thanks in advance.

    Originally posted by Frederick
    The source below works fine if I call it from a page that just
    outputs the code, but if I try to output this in a site where
    output has already started prior to this code, I get garbble.

    This is probably because once you've already started output, you can't call the header() function:

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

    If you need to have this code output something on a page that has started output, then one solution would be to do this:

    //Assume the code in create_thumb() is placed inside thumb.php, not as a function but as 'the' code
    <img src="thumb.php?catID=1">
    

    I'm almost 100% I used this sort of method before on having a dynamic signature on some forums (i.e. linked to my .php script and the image was output just fine in my signature).

    Hope this helps 😉

      That woked as far as a single image name, but how do I pass the image names in a loop. It works when I hard code the image name in the code.

        have you tried turning output buffering on?

        www.php.net/ob_start

        this should hold sending anything to the browser before the script finishes.
        when the script has run, the buffer is flushed to the screen and headers are always sent first.

        It's worth a try

          Originally posted by Frederick
          but how do I pass the image names in a loop.

          You could try passing the filename as an item in the $_GET array.

          On the calling page (the one with <img src> etc):

          $images = //Array containing the image names
          foreach($images as $image)
          {
              echo "<img src=\"thumb.php?file=$image\">";
          }
          

          And in your create_thumb() function:

          function create_thumb() {
              $imgfile = $_GET['file'];	//Here you get the filename instead of from the global $filename
              Header("Content-type: image/jpeg");
          
          list($width, $height) = getimagesize($imgfile);
          $newheight = 80;
          $newwidth = (int) (($width*$newheight)/$height);
          
          $thumb = imagecreatetruecolor($newwidth,$newheight);
          $source = imagecreatefromjpeg($imgfile);
          
          imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
          
          imagejpeg($thumb);
          }
          

            ScubbaKing22, your suggestion hit the spot.
            Thanks to all.
            If you think about it, that $_GET idea was pretty easy. Can't believe I did not think of it myself.

              Write a Reply...