Image displaying

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

    Image displaying

    I'm trying to display resized images. Locations of images are fetched from
    database. The problem is that with the following code, I get only the first
    image displayed:

    [PHP]
    <?php
    header('Content-type: image/jpeg');

    $link = mysql_connect(' localhost', 'root', '');
    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db ("proba1");
    $query='select lokacija from galerija';
    $result=mysql_q uery($query);
    $new_width = 200;
    $new_height = 150;


    while($filename =mysql_fetch_ar ray($result)){

    for($i=0;$i<=co unt($filename); $i++){

    list($width, $height) = getimagesize($f ilename[$i]);
    $image_p = imagecreatetrue color($new_widt h, $new_height);
    $image = imagecreatefrom jpeg($filename[$i]);
    imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $new_width,
    $new_height, $width, $height);
    imagejpeg($imag e_p, null, 100)."\n";
    imagedestroy($i mage);
    imagedestroy($i mage_p);
    }
    }
    [/PHP]

    With the next code in the same for loop I get all of the images, but
    ofcourse not resized:

    [PHP]
    ....
    echo "<table>";
    echo"<tr><td><i mg src=$filename[$i]></td></tr>";
    echo "</table>";
    ....
    [/PHP]

    So I guess the problem is in image functions, but I don't know how to solve
    it. Any suggestion is welcome :)

    Tnx,
    Dejan


  • ZabMilenko

    #2
    Re: Image displaying

    It looks as if you are creating the second image, but not saving it anywhere.

    This generates an image: imagejpeg($imag e_p, null, 100)."\n";

    But what about $image? You do a copyresampled then destroy it. If you want
    to save the image, pass the second argument to imagejpeg.


    ljuljacka wrote:
    I'm trying to display resized images. Locations of images are fetched from
    database. The problem is that with the following code, I get only the first
    image displayed:
    >
    [PHP]
    <?php
    header('Content-type: image/jpeg');
    >
    $link = mysql_connect(' localhost', 'root', '');
    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }
    >
    mysql_select_db ("proba1");
    $query='select lokacija from galerija';
    $result=mysql_q uery($query);
    $new_width = 200;
    $new_height = 150;
    >
    >
    while($filename =mysql_fetch_ar ray($result)){
    >
    for($i=0;$i<=co unt($filename); $i++){
    >
    list($width, $height) = getimagesize($f ilename[$i]);
    $image_p = imagecreatetrue color($new_widt h, $new_height);
    $image = imagecreatefrom jpeg($filename[$i]);
    imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $new_width,
    $new_height, $width, $height);
    imagejpeg($imag e_p, null, 100)."\n";
    imagedestroy($i mage);
    imagedestroy($i mage_p);
    }
    }
    [/PHP]
    >
    With the next code in the same for loop I get all of the images, but
    ofcourse not resized:
    >
    [PHP]
    ...
    echo "<table>";
    echo"<tr><td><i mg src=$filename[$i]></td></tr>";
    echo "</table>";
    ...
    [/PHP]
    >
    So I guess the problem is in image functions, but I don't know how to solve
    it. Any suggestion is welcome :)
    >
    Tnx,
    Dejan
    >
    >

    Comment

    • ljuljacka

      #3
      Re: Image displaying

      The thing I'm trying to get is to display images as thumbnails without
      actually saving them, and the script does that but only with the first
      image.
      When I echo $filename[$i] inside the for loop it displays all file
      locations.
      When I removed imagedestroy() from script, it still does the same...
      So the problem is displaying other images.

      If this answer is funny it's because I don't know if I understood you
      correctly :)

      "ZabMilenko " wrote in message :
      >It looks as if you are creating the second image, but not saving it
      >anywhere.
      >
      This generates an image: imagejpeg($imag e_p, null, 100)."\n";
      >
      But what about $image? You do a copyresampled then destroy it. If you
      want to save the image, pass the second argument to imagejpeg.
      >
      >

      Comment

      • ZabMilenko

        #4
        Re: Image displaying

        Here is what I am seeing:


        while($filename =mysql_fetch_ar ray($result))
        {

        for($i=0;$i<=co unt($filename); $i++)
        {
        // loop thru a bunch of files

        list($width, $height) = getimagesize($f ilename[$i]);
        // find out how big it is

        // Make an new image IN MEMORY
        $image_p = imagecreatetrue color($new_widt h, $new_height);

        // make an image IN MEMORY from the original
        $image = imagecreatefrom jpeg($filename[$i]);


        // Paint the image as a thumbnail IN MEMORY
        imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $new_width,
        $new_height, $width, $height);


        // Send the new image to the browser
        imagejpeg($imag e_p, null, 100)."\n";
        imagedestroy($i mage);
        imagedestroy($i mage_p);
        }

        // Repeat
        }

        The problem is you can only send one image per request. That is ONE image
        between header("content-type ...") and imagejpeg()

        You can make all you want, but only the first will go to the browser. The
        rest is as useless as spam.

        So you need to dump the thumbnails to disk and load them another way.


        ljuljacka wrote:
        The thing I'm trying to get is to display images as thumbnails without
        actually saving them, and the script does that but only with the first
        image.
        When I echo $filename[$i] inside the for loop it displays all file
        locations.
        When I removed imagedestroy() from script, it still does the same...
        So the problem is displaying other images.
        >
        If this answer is funny it's because I don't know if I understood you
        correctly :)
        >
        "ZabMilenko " wrote in message :
        >It looks as if you are creating the second image, but not saving it
        >anywhere.
        >>
        >This generates an image: imagejpeg($imag e_p, null, 100)."\n";
        >>
        >But what about $image? You do a copyresampled then destroy it. If you
        >want to save the image, pass the second argument to imagejpeg.
        >>
        >>
        >
        >

        Comment

        • ljuljacka

          #5
          Re: Image displaying

          Yes, I get it now...tnx for your help. I guess I have some work to do :)
          "ZabMilenko " <zabmilenko@hot mail.comwrote in message
          news:Sr7Wg.3$NT 2.0@newsfe03.lg a...
          Here is what I am seeing:
          >
          >
          while($filename =mysql_fetch_ar ray($result))
          {
          >
          for($i=0;$i<=co unt($filename); $i++)
          {
          // loop thru a bunch of files
          >
          list($width, $height) = getimagesize($f ilename[$i]);
          // find out how big it is
          >
          // Make an new image IN MEMORY
          $image_p = imagecreatetrue color($new_widt h, $new_height);
          >
          // make an image IN MEMORY from the original
          $image = imagecreatefrom jpeg($filename[$i]);
          >
          >
          // Paint the image as a thumbnail IN MEMORY
          imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $new_width,
          $new_height, $width, $height);
          >
          >
          // Send the new image to the browser
          imagejpeg($imag e_p, null, 100)."\n";
          imagedestroy($i mage);
          imagedestroy($i mage_p);
          }
          >
          // Repeat
          }
          >
          The problem is you can only send one image per request. That is ONE image
          between header("content-type ...") and imagejpeg()
          >
          You can make all you want, but only the first will go to the browser. The
          rest is as useless as spam.
          >
          So you need to dump the thumbnails to disk and load them another way.
          >
          >
          ljuljacka wrote:
          >The thing I'm trying to get is to display images as thumbnails without
          >actually saving them, and the script does that but only with the first
          >image.
          >When I echo $filename[$i] inside the for loop it displays all file
          >locations.
          >When I removed imagedestroy() from script, it still does the same...
          >So the problem is displaying other images.
          >>
          >If this answer is funny it's because I don't know if I understood you
          >correctly :)
          >>
          >"ZabMilenko " wrote in message :
          >>It looks as if you are creating the second image, but not saving it
          >>anywhere.
          >>>
          >>This generates an image: imagejpeg($imag e_p, null, 100)."\n";
          >>>
          >>But what about $image? You do a copyresampled then destroy it. If you
          >>want to save the image, pass the second argument to imagejpeg.
          >>>
          >>>
          >>

          Comment

          • Oli Charlesworth

            #6
            Re: Image displaying

            ZabMilenko said the following on 08/10/2006 14:55:
            Here is what I am seeing:
            >
            >
            while($filename =mysql_fetch_ar ray($result))
            {
            >
            for($i=0;$i<=co unt($filename); $i++)
            {
            // loop thru a bunch of files
            >
            list($width, $height) = getimagesize($f ilename[$i]);
            // find out how big it is
            >
            // Make an new image IN MEMORY
            $image_p = imagecreatetrue color($new_widt h, $new_height);
            >
            // make an image IN MEMORY from the original
            $image = imagecreatefrom jpeg($filename[$i]);
            >
            >
            // Paint the image as a thumbnail IN MEMORY
            imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $new_width,
            $new_height, $width, $height);
            >
            >
            // Send the new image to the browser
            imagejpeg($imag e_p, null, 100)."\n";
            imagedestroy($i mage);
            imagedestroy($i mage_p);
            }
            >
            // Repeat
            }
            >
            The problem is you can only send one image per request. That is ONE image
            between header("content-type ...") and imagejpeg()
            >
            You can make all you want, but only the first will go to the browser.
            The rest is as useless as spam.
            >
            So you need to dump the thumbnails to disk and load them another way.
            Or have the script only output one image, and call it N times, i.e.:


            <img src="script.php ?img=1">
            <img src="script.php ?img=2">
            <img src="script.php ?img=3">
            <img src="script.php ?img=4">

            Comment

            Working...