Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3760

Re: Images retrives

Message-ID <4774309.ypaU67uLZW@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-11-12 00:22 +0100
Subject Re: Images retrives
Newsgroups comp.lang.php
References <189232.705.1321011949235.JavaMail.geo-discussion-forums@prdy11>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


sritullimilli@gmail.com wrote:

> i didn't get the images from mysql database through php script
> i am get this error
> 
> <img src='���JFIF

You are trying to output the raw image data as a string (JFIF means "JPEG 
File Interchange Format" [1]).  This cannot work (at least not this way); 
the `src' attribute value must be a URI [2].

> ...
> plz suggest me

There are several alternatives.  For example:

A) Write and access a temporary file

  <?php
    $img_data = db_query(…);
    if ($img_data)
    {
      /*
       * Use the _correct_ filename suffix here; the server 
       * should take care of the proper Content-Type for that suffix
       */
      file_put_contents("{$tmp}/foo.jpg", $img_data);  // [3]
    }
  ?>
  …
  <img src="tmp/foo.jpg" alt="…" …>

B)

  <?php
    /* foo.php */

    $img_data = db_query(…);

    if ($img_data)
    {
      /* declare the _correct_ type here */
      header('Content-Type: image/jpeg');  // [4]

      echo $img_data;
    }
  ?>

  bar.html:

  <img src="foo.php" alt="…" …>

C)

  <?php
    $img_data = db_query(…);
    if ($img_data)
    {
  ?>
    <img src="data:image/jpeg;base64,<?php
           echo base64_encode($imgdata); // [5]
           ?>"
         alt="…" …>
  <?php
    }
  ?>

A) and B) are more compatible than C) [6].

A) works well for images that do not change often and need to be retrieved 
fast in short intervals (such as in an e-commerce shop); you only have to 
write the file once, after which accesses are cached by the Web server's 
filesystem, the server's HTTP server service/daemon and the browser's HTTP 
client.  There are no limits on the size of the file other than imposed by 
the server filesystem.

B) is more flexible than A) and requires no additional server filesystem 
resources, but is overall slower than A) even though it can be cached by the 
browser.  I am presently not aware of any limits to image file size.

C) requires fewer server filesystem resources than A) but it might not be 
cacheable and it requires more computational effort on the server and 
client-side instead.  It is limited by the maximum URI length accepted by a 
Web browser (2083 characters by Internet Explorer up to and including 
version 9.x, at the time of writing [7]); in particular, base64 encoding 
makes the encoded string about one third longer than the original.  (By 
contrast, URI percent-encoding can make the encoded string three times as 
long as the original.)

Use A) or B) unless there is a compelling reason to use C).

I also recommend not to store image data or other BLOBs in the database 
unless there is a compelling reason to do otherwise.  Store the filename in 
the database instead, and store the image data as a file in the filesystem;
that way, it is also easier to implement A).


PointedEars
___________
[1] <http://de.wikipedia.org/wiki/JPEG_File_Interchange_Format>
[2] <http://www.w3.org/TR/html401/struct/objects.html#edef-IMG>
[3] <http://php.net/file_put_contents>
[4] <http://php.net/header>
[5] <http://php.net/base64_encode>
[6] <http://en.wikipedia.org/wiki/Data_URI_scheme>
[7] <http://support.microsoft.com/kb/208427/en-us>
-- 
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
  -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Images retrives sritullimilli@gmail.com - 2011-11-11 03:45 -0800
  Re: Images retrives "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-11-11 13:19 +0100
  Re: Images retrives The Natural Philosopher <tnp@invalid.invalid> - 2011-11-11 13:02 +0000
  Re: Images retrives Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-11 09:10 -0500
  Re: Images retrives Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-12 00:22 +0100
    Re: Images retrives Robert Hairgrove <nobody@hogwash.com> - 2011-11-12 00:34 +0100
      Re: Images retrives Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-12 00:49 +0100
      Re: Images retrives Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-11 19:37 -0500

csiph-web