Serve an image with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dolittle
    New Member
    • Sep 2007
    • 54

    Serve an image with php

    Hi,

    I have an image tag in my html with a src pointing to a php script.
    The php script finds the url of the image from a paramater in the query string.
    How can I return the image data to the html page?

    [HTML]<img src="phpScript. php?id=12321321/>[/HTML]

    [PHP]<php?
    $id = $_REQUEST["id"];
    $img_url = someFunction($i d);
    echo imageData????
    [/PHP]

    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Serve an image with php

    Assuming that the picture is stored somewhere (so you only need the path to it).
    Then it would be enough to echo the file path.
    [PHP]$id = $_GET["id"];
    $img_url = someFunction($i d);
    echo $img_url;[/PHP]
    but why calling another script file... since you're starting from a php file anyway you could do also
    [PHP]include_once("p hpScript.php");
    echo '<img src="' . someFunction(12 321321) . '" alt=""/>';[/PHP]
    regards

    Comment

    • dolittle
      New Member
      • Sep 2007
      • 54

      #3
      Your first solution doesn't work because the html image tag expect the image data and not the image url.

      I can't use your second solution because I'm generating the image tag dynamicaly on the client side.

      I finally used the header function:
      [PHP]header('Locatio n:'.$image_url) ;[/PHP]

      The problem is that I need to cache the image.

      Can I load the image data in the php script, add expire header and then echo it?

      Thanks

      Originally posted by Dormilich
      Assuming that the picture is stored somewhere (so you only need the path to it).
      Then it would be enough to echo the file path.
      [PHP]$id = $_GET["id"];
      $img_url = someFunction($i d);
      echo $img_url;[/PHP]
      but why calling another script file... since you're starting from a php file anyway you could do also
      [PHP]include_once("p hpScript.php");
      echo '<img src="' . someFunction(12 321321) . '" alt=""/>';[/PHP]
      regards

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Generally when people link to a php file in an image URL use header()s to output the image. Something like this:

        [php]

        $image = $_GET['id'];

        header("Content-type: image/{$img_ext}");
        readfile("/path/to/my/images/{$image}");
        exit(0);

        [/php]

        Obviously you would have to sanitize the input coming from the URL (google: mysql injection). And then you would have to use the correct 'img_ext' - perhaps you will store this in a database and use the image name to locate it in the database,

        Comment

        • bnashenas1984
          Contributor
          • Sep 2007
          • 257

          #5
          Hi
          I'm not sure about your issue but I know that it's possible to output pictures with PHP GD.
          You should have seen many websites using (turing numbers). When you register on some websites . On the registration page they ask you to put a number (shown on an image) in a text field.. The format of the image could be JPG, PNG or GIf but when you right click on the image and go to properties you see the address is a .php file.
          PHP GD is a library built in PHP to work with images. It could easily open any photo and resize or edit edit and then save it to a file or just output it as it is.

          You can have an image tag in your HTML like this:
          <img src="www.yourdo main.com/anyfile.php">

          and then use PHP GD to output an image.

          Let me know if it's what you need. I can help you more with it. Or you can go to www.php.net and search for GD.

          Good luck

          Comment

          • dolittle
            New Member
            • Sep 2007
            • 54

            #6
            I manged to get the image using curl and then send it to the user.
            The problem is I can't get IE to cache the image.
            FF works fine.

            Any ideas?

            [PHP]// Allocate a new cURL handle
            $ch = curl_init($user _thumb);
            if (! $ch) {
            die( "Cannot allocate a new PHP-CURL handle" );
            }

            // We'll be returning this transfer, and the data is binary
            // so we don't want to NULL terminate
            curl_setopt($ch , CURLOPT_RETURNT RANSFER, 1);
            curl_setopt($ch , CURLOPT_BINARYT RANSFER, 1);

            // Grab the jpg and save the contents in the $data variable
            $data = curl_exec($ch);

            // close the connection
            curl_close($ch) ;

            // Set the header to type image/jpeg, since that's what we're
            // displaying
            header("Content-type: image/jpeg");

            header('Expires : '.gmdate('D, d M Y H:i:s', time()+100000). 'GMT'); */

            Header("Cache-Control: max-age=3600, must-revalidate");
            [/PHP]

            Comment

            • bnashenas1984
              Contributor
              • Sep 2007
              • 257

              #7
              Hi dolittle

              Here is what you can do.

              Put the code bellow in a php file. in you HTML code use this PHP file as your SRC for your IMG tag

              Code:
                 <?PHP
              	// use this file as it is if your image is a JPG
              	// if the image in a GIF use imagecreatefromgif
              	// if the image in a PNG use imagecreatefrompng
              
              	$img=imagecreatefromjpeg("yourImage.jpg");
              	imagePNG($img);
              	imagedestroy($img);
                 ?>

              and the HTML code will be:
              Code:
              <img src="phpfile.php">

              But before you use this code. Go to your php settings and make sure GD is enabled. use phpinfo()

              Hope this helps

              Comment

              • bnashenas1984
                Contributor
                • Sep 2007
                • 257

                #8
                I forgot to say.
                There are many websites using this method to output their pictures. It usualy is a good choice for websites with user profiles because this method helps them hide the real address of the image . one of the best examples is Facebook.com

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by bnashenas1984
                  Hi dolittle

                  Here is what you can do.

                  Put the code bellow in a php file. in you HTML code use this PHP file as your SRC for your IMG tag

                  Code:
                     <?PHP
                  	// use this file as it is if your image is a JPG
                  	// if the image in a GIF use imagecreatefromgif
                  	// if the image in a PNG use imagecreatefrompng
                  
                  	$img=imagecreatefromjpeg("yourImage.jpg");
                  	imagePNG($img);
                  	imagedestroy($img);
                     ?>

                  and the HTML code will be:
                  Code:
                  <img src="phpfile.php">

                  But before you use this code. Go to your php settings and make sure GD is enabled. use phpinfo()

                  Hope this helps
                  Your solution isn't too helpful. What if GD isn't enabled? What if the image isn't a gif or png?

                  Comment

                  • bnashenas1984
                    Contributor
                    • Sep 2007
                    • 257

                    #10
                    Originally posted by markusn00b
                    Your solution isn't too helpful. What if GD isn't enabled? What if the image isn't a gif or png?
                    As you can see in the script it even works with JPG. And about enabling GD it usualy is enabled on every servers with php version older than 4.0.

                    I'm not the one telling you to use GD but there are millions of websites out there which use GD to output, resize or edit photos dinamicaly.

                    what would happen if websites like Facebook, Hi5, ebay, Flikr etc had to manualy open uploaded files (over thousands an hour) and resize or edit them?


                    It's your choice

                    good luck.

                    Comment

                    Working...