Mixing Php's imagecopy and HTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ubantu Rococo

    Mixing Php's imagecopy and HTML

    Hi all,
    Sorry for this stupid question, but I am having trouble mixing imagecopy
    etc. with HTML.
    What I am trying to do is copy an image, and then obtain co-ordinates from a
    database which will then be drawn
    on the image (to create a clickable imagemap). The image will then be
    displayed as part of a webpage.

    I've included my code snippet below. It doesn't work, and I think I know
    why - the output on my screen
    grumbles about the header information already being sent. Is there any way
    to do what I want it do?
    Perhaps putting the image copy calls in a separate function call?

    Sorry for the stupid question, I am new to php.

    Best wishes

    Paul



    <HTML>

    <H1>Test...</H1>

    <?php

    // File and new size

    $filename = 'logo_i.JPG';

    // Content type

    header('Content-type: image/jpeg');

    // Get new sizes

    list($width, $height) = getimagesize($f ilename);

    // Load

    $thumb = imagecreatetrue color($width, $height);

    $source = imagecreatefrom jpeg($filename) ;

    // Resize

    imagecopy($thum b, $source, 0, 0, 0, 0, $width, $height);

    imagerectangle( $thumb, 10,10,50,50,450 0);

    // Output

    imagejpeg($thum b);

    ?>

    </HTML>


  • d

    #2
    Re: Mixing Php's imagecopy and HTML

    "Ubantu Rococo" <paul.x.lee@bae systems.com> wrote in message
    news:43d76123$1 _1@glkas0286.gr eenlnk.net...[color=blue]
    > Hi all,
    > Sorry for this stupid question, but I am having trouble mixing imagecopy
    > etc. with HTML.
    > What I am trying to do is copy an image, and then obtain co-ordinates from
    > a database which will then be drawn
    > on the image (to create a clickable imagemap). The image will then be
    > displayed as part of a webpage.
    >
    > I've included my code snippet below. It doesn't work, and I think I know
    > why - the output on my screen
    > grumbles about the header information already being sent. Is there any way
    > to do what I want it do?
    > Perhaps putting the image copy calls in a separate function call?
    >
    > Sorry for the stupid question, I am new to php.
    >
    > Best wishes
    >
    > Paul
    >
    >
    >
    > <HTML>
    >
    > <H1>Test...</H1>
    >
    > <?php
    >
    > // File and new size
    >
    > $filename = 'logo_i.JPG';
    >
    > // Content type
    >
    > header('Content-type: image/jpeg');
    >
    > // Get new sizes
    >
    > list($width, $height) = getimagesize($f ilename);
    >
    > // Load
    >
    > $thumb = imagecreatetrue color($width, $height);
    >
    > $source = imagecreatefrom jpeg($filename) ;
    >
    > // Resize
    >
    > imagecopy($thum b, $source, 0, 0, 0, 0, $width, $height);
    >
    > imagerectangle( $thumb, 10,10,50,50,450 0);
    >
    > // Output
    >
    > imagejpeg($thum b);
    >
    > ?>
    >
    > </HTML>
    >
    >[/color]

    I don't think imagejpeg works as you think it does. It outputs an image's
    actual binary data, so you're essentially trying to output a jpeg image with
    HTML tags before and after it - which won't work. What you need to do, is
    put the PHP code only into a file, called say image.php. Then, in your HTML
    document (the rest of the code above), just put an image tag in: <img
    src="image.php" >. That will display the image in the HTML page. You can
    even have javascript to work out where you click on the image, and then
    update its url like this:

    <img src="image.php? x=10&y=22">, and the image.php can then add something at
    those co-ordinates to demonstrate it has been selected.

    dave


    Comment

    • Ubantu Rococo

      #3
      Re: Mixing Php's imagecopy and HTML

      >[color=blue]
      > I don't think imagejpeg works as you think it does. It outputs an image's
      > actual binary data, so you're essentially trying to output a jpeg image
      > with HTML tags before and after it - which won't work. What you need to
      > do, is put the PHP code only into a file, called say image.php. Then, in
      > your HTML document (the rest of the code above), just put an image tag in:
      > <img src="image.php" >. That will display the image in the HTML page. You
      > can even have javascript to work out where you click on the image, and
      > then update its url like this:
      >
      > <img src="image.php? x=10&y=22">, and the image.php can then add something
      > at those co-ordinates to demonstrate it has been selected.[/color]


      Thanks, I understand now, I think....

      Could I just put the imagecopy routine in an external file (called
      getWorldMap.php , with the getWorldMap() function contained
      within) and then do:

      <?php include("getWor ldMap.php");

      $wm = getWorldMap(); // $wm is the resource,copied from WorldMap

      imagejpg($wm);

      ?>



      Comment

      • d

        #4
        Re: Mixing Php's imagecopy and HTML

        "Ubantu Rococo" <paul.x.lee@bae systems.com> wrote in message
        news:43d767e6$1 _1@glkas0286.gr eenlnk.net...[color=blue][color=green]
        > >
        >> I don't think imagejpeg works as you think it does. It outputs an
        >> image's actual binary data, so you're essentially trying to output a jpeg
        >> image with HTML tags before and after it - which won't work. What you
        >> need to do, is put the PHP code only into a file, called say image.php.
        >> Then, in your HTML document (the rest of the code above), just put an
        >> image tag in: <img src="image.php" >. That will display the image in the
        >> HTML page. You can even have javascript to work out where you click on
        >> the image, and then update its url like this:
        >>
        >> <img src="image.php? x=10&y=22">, and the image.php can then add something
        >> at those co-ordinates to demonstrate it has been selected.[/color]
        >
        >
        > Thanks, I understand now, I think....
        >
        > Could I just put the imagecopy routine in an external file (called
        > getWorldMap.php , with the getWorldMap() function contained
        > within) and then do:
        >
        > <?php include("getWor ldMap.php");
        >
        > $wm = getWorldMap(); // $wm is the resource,copied from WorldMap
        >
        > imagejpg($wm);
        >
        > ?>[/color]

        You could indeed. That script would output the jpeg, so it has to be on its
        own, and with the correct header sent. I think if you're not going to be
        using the code anywhere else, just keep it in one flat file. That would
        make it a lot easier to comprehend ;)

        Also, remember to call imagedestroy() on your image handles to free up the
        memory they use. Otherwise you might find yourself running out of resources
        on your webserver, if its not tidying up after itself.

        dave


        Comment

        • Ubantu Rococo

          #5
          Re: Mixing Php's imagecopy and HTML


          "d" <d@example.co m> wrote in message
          news:m5KBf.8888 $wl.8380@text.n ews.blueyonder. co.uk...[color=blue]
          > "Ubantu Rococo" <paul.x.lee@bae systems.com> wrote in message
          > news:43d767e6$1 _1@glkas0286.gr eenlnk.net...[color=green][color=darkred]
          >> >
          >>> I don't think imagejpeg works as you think it does. It outputs an
          >>> image's actual binary data, so you're essentially trying to output a
          >>> jpeg image with HTML tags before and after it - which won't work. What
          >>> you need to do, is put the PHP code only into a file, called say
          >>> image.php. Then, in your HTML document (the rest of the code above),
          >>> just put an image tag in: <img src="image.php" >. That will display the
          >>> image in the HTML page. You can even have javascript to work out where
          >>> you click on the image, and then update its url like this:
          >>>
          >>> <img src="image.php? x=10&y=22">, and the image.php can then add
          >>> something at those co-ordinates to demonstrate it has been selected.[/color]
          >>
          >>
          >> Thanks, I understand now, I think....
          >>
          >> Could I just put the imagecopy routine in an external file (called
          >> getWorldMap.php , with the getWorldMap() function contained
          >> within) and then do:
          >>
          >> <?php include("getWor ldMap.php");
          >>
          >> $wm = getWorldMap(); // $wm is the resource,copied from WorldMap
          >>
          >> imagejpg($wm);
          >>
          >> ?>[/color]
          >
          > You could indeed. That script would output the jpeg, so it has to be on
          > its own, and with the correct header sent. I think if you're not going to
          > be using the code anywhere else, just keep it in one flat file. That
          > would make it a lot easier to comprehend ;)
          >
          > Also, remember to call imagedestroy() on your image handles to free up the
          > memory they use. Otherwise you might find yourself running out of
          > resources on your webserver, if its not tidying up after itself.
          >
          > dave
          >
          >[/color]
          Thanks mate.
          I've tried it, but the getWorldMap() function doesn't seem to work! I either
          get the raw binary data of the jpg
          and an error about the header (if I leave the header(...) function in
          below), or just the raw data if I leave out the f
          function.

          This is my file getWorldMap.php
          <?php

          function getWorldMap()

          {

          // File and new size

          $filename = 'logo_i.JPG';

          // Content type

          header('Content-type: image/jpeg');

          // Get new sizes

          list($width, $height) = getimagesize($f ilename);

          // Load

          $thumb = imagecreatetrue color($width, $height);

          $source = imagecreatefrom jpeg($filename) ;

          // Resize

          imagecopy($thum b, $source, 0, 0, 0, 0, $width, $height);

          // Output

          return $thumb;

          }

          ?>


          Comment

          • David  Wahler

            #6
            Re: Mixing Php's imagecopy and HTML

            Ubantu Rococo wrote:[color=blue]
            > Thanks mate.
            > I've tried it, but the getWorldMap() function doesn't seem to work! I either
            > get the raw binary data of the jpg
            > and an error about the header (if I leave the header(...) function in
            > below), or just the raw data if I leave out the f
            > function.[/color]
            [snip]

            Are you trying to do this?

            <html>
            <h1>Test...</h1>
            <?php
            include("getWor ldMap.php");
            $wm = getWorldMap();
            imagejpg($wm);
            ?>
            </h1>

            As Dave already explained, this won't work. Here's what you can do:
            move the call to imagejpg() into getWorldMap.php , and then move the
            image generation code out of the getWorldMap() function and into the
            top level of getWorldMap.php . Then, in your main page, do something
            like this:

            <html>
            <h1>Test...</h1>
            <img src="getWorldMa p.php">
            </html>

            -- David

            Comment

            • NC

              #7
              Re: Mixing Php's imagecopy and HTML

              Ubantu Rococo wrote:[color=blue]
              >
              > I am having trouble mixing imagecopy etc. with HTML.[/color]

              For a good reason; you're not supposed to do that.

              You can have a script that generates HTML; if you want your script to
              output an image, you must make it separate from scripts that generate
              HTML. Something like this:

              <img src="picture.ph p?input1=abcd&i nput2=78">

              Then picture.php can generate the image you want based on inputs you
              specify...

              Cheers,
              PHP

              Comment

              • NC

                #8
                Re: Mixing Php's imagecopy and HTML

                Ubantu Rococo wrote:[color=blue]
                >
                > I am having trouble mixing imagecopy etc. with HTML.[/color]

                For a good reason; you're not supposed to do that.

                You can have a script that generates HTML; if you want your script to
                output an image, you must make it separate from scripts that generate
                HTML. Something like this:

                <img src="picture.ph p?input1=abcd&i nput2=78">

                Then picture.php can generate the image you want based on inputs you
                specify...

                Cheers,
                NC

                Comment

                Working...