Help translating fread to substr

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

    Help translating fread to substr

    Dear All

    Im having some dificulty here:

    I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair
    Spectrum $SCREEN Image Format) and tranforms it into PNG format to show
    onscreen by the PHP gd2 Library.

    The problem is that im trying to translate the 'fread' structure into a
    MySQL blob reading, as follows:

    Original file access code:

    $zxscr = fread($handle, 6144); // first 6144 bytes of file
    $attrs = fread($handle, 768); // last 768 bytes of file

    My actual MySQL access code:

    $zxscr = substr($row[Image], 0, 6144); // first 6144 bytes
    of MySQL field "Imagem" inside table "Perguntas"
    $attrs = substr($row[Image], -768); // last 768 bytes of
    MySQL field "Imagem" inside table "Perguntas"

    Here's the original full CODE (for reading the file from servers directory):

    $filename=$_GET['scrimg'];
    if(!$filename) error("Invalid filename");
    $handle = fopen($filename , "r");
    if(filesize($fi lename)<>6912) { fclose($handle) ; error("Size
    of file must be 6912 bytes"); }
    $zxscr = fread($handle, 6144); // first 6144 bytes of file
    $attrs = fread($handle, 768); // last 768 bytes of file
    fclose($handle) ;

    And here's what i changed it into (for reading from a blob field inside
    MySQL db):

    mysql_connect(" localhost", "user", "password") or die("Could
    not connect: " . mysql_error());
    mysql_select_db ("jb-ntg");
    $result = mysql_query("SE LECT Numero, Nome, Imagem FROM
    Perguntas WHERE Numero = jogo");
    $zxscr = substr($row[Image], 0, 6144); // first 6144 bytes
    of MySQL field "Imagem" inside table "Perguntas"
    $attrs = substr($row[Image], -768); // last 768 bytes of
    MySQL field "Imagem" inside table "Perguntas"

    Here's the structure of the DB "jb-ntg"

    CREATE TABLE `perguntas` (
    `Numero` int(11) NOT NULL auto_increment,
    `Nome` mediumtext NOT NULL,
    `Imagem` blob NOT NULL,
    `Acessos` int(11) NOT NULL default '0',
    `Certas` int(11) NOT NULL default '0',
    PRIMARY KEY (`Numero`)
    ) TYPE=MyISAM AUTO_INCREMENT= 3 ;

    But i get a blank image (with the right dimensions, so the script must be
    ok) the only problem i think is the fact that in the original code i could
    get the first 6144 bytes and last 768 bytes of the file, but using the MySQL
    blob i apparently cant at least using the substr, and i sincerely have no
    idea of the binary equivalent of the fread for blob files.

    Anyone can help?

    I never coded in PHP and this is my first try at this new language, as you
    should remember im trying to make a "Name the Game" PHP game devoted to ZX
    Spectrum, and i will, i just need a few guiding lines over here and there.

    Thanks for any support in advance,
    RootShell






  • Pedro Graca

    #2
    Re: Help translating fread to substr

    RootShell wrote:

    (snipped)
    [color=blue]
    > And here's what i changed it into (for reading from a blob field inside
    > MySQL db):
    >
    > mysql_connect(" localhost", "user", "password") or die("Could
    > not connect: " . mysql_error());
    > mysql_select_db ("jb-ntg");
    > $result = mysql_query("SE LECT Numero, Nome, Imagem FROM
    > Perguntas WHERE Numero = jogo");[/color]


    $result is a resource "pointing" to a set of rows.
    You're not using this resource in your code.

    You need to do

    $row = mysql_fetch_ass oc($result);

    to fill $row with data from the database.

    [color=blue]
    > $zxscr = substr($row[Image], 0, 6144); // first 6144 bytes
    > of MySQL field "Imagem" inside table "Perguntas"[/color]


    $row[Image] (probably you mean $row["Imagem"], right?) has not been
    filled with data from the database;
    so now the value of $zxscr is the empty string.

    [color=blue]
    > $attrs = substr($row[Image], -768); // last 768 bytes of
    > MySQL field "Imagem" inside table "Perguntas"[/color]


    $attrs = '';

    [color=blue]
    > Here's the structure of the DB "jb-ntg"[/color]
    (snipped)[color=blue]
    > But i get a blank image (with the right dimensions, so the script must be
    > ok)[/color]


    Maybe the right dimensions come from your HTML tags?
    <img src="whatever" width="80" height="24" alt="ZX Spectrum screen shot"/>
    [color=blue]
    > the only problem i think is the fact that in the original code i could
    > get the first 6144 bytes and last 768 bytes of the file, but using the MySQL
    > blob i apparently cant at least using the substr, and i sincerely have no
    > idea of the binary equivalent of the fread for blob files.
    >
    > Anyone can help?
    >
    > I never coded in PHP and this is my first try at this new language, as you
    > should remember im trying to make a "Name the Game" PHP game devoted to ZX
    > Spectrum, and i will, i just need a few guiding lines over here and there.[/color]

    Bem vindo ao PHP e boa-noite :)

    [Translation: welcome to PHP and good night :)]

    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: Help translating fread to substr

      "RootShell" <rootshell@netc abo.pt> wrote in message news:<4098150f$ 0$3543$a729d347 @news.telepac.p t>...[color=blue]
      > Dear All
      >
      > Im having some dificulty here:
      >
      > I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair
      > Spectrum $SCREEN Image Format) and tranforms it into PNG format to show
      > onscreen by the PHP gd2 Library.
      >
      > The problem is that im trying to translate the 'fread' structure into a
      > MySQL blob reading, as follows:[/color]

      <snip>

      Not a direct answer to your question...

      If you want to read the header info from the file use unpack()
      <http://in2.php.net/unpack>

      A good example is found at
      <http://fooassociates.c om/phpfer/html/rn45re878.html> and it was
      suggested as a nice workaround for "struct" (C's) by Andy and Chung.


      --
      | Just another PHP saint |
      Email: rrjanbiah-at-Y!com

      Comment

      Working...