Problems with images in MySQL database

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Problems with images in MySQL database

    I'm having problem with images becoming corrupted after loading them into a
    MySQL database and extracting them again. I'm pretty sure it's todo with
    escaping slashes but I'm at a loss as to how to confirm this and find a
    solution.

    A piece of test code is below, the import script displays the image before
    it goes in and then uses a second script to display it straight from the
    database as well as extracting it to a file and displaying that. It's
    scruffy as it's stripped from a larger system but it works (or more
    accurately breaks!) Imagemagick is used for the scaling operation.

    The script can be seen at [url]http://geoffsoper.co.uk/test/admin/import_test.php[/url]

    Some configuration settings that may be relevant are:
    Local Master
    magic_quotes_gpc Off On
    magic_quotes_runtime Off Off
    magic_quotes_sybase Off Off

    Please ask if you need to know any other PHP configuration settings.

    I'd be very grateful for any clues as to what is going on.
    Many thanks,
    Geoff



    <?php
    $new_display_size = 640;
    include('xxxxxxxx/test.cfg');
    if ($config_file_included != true)
    {
    die('Configuration file not found.');
    }
    include($admin_inc_dir . 'db.inc');
    include($admin_inc_dir . 'misc.inc');

    /* Connecting, selecting database */
    $link = mysql_connect($db_host, $db_user, $db_password)
    or die("Could not connect : " . mysql_error());
    mysql_select_db($db_database) or die("Could not select database");

    $input_path = "xxxxxxxxx/039_35a.jpg";
    $display_path = "xxxxxxxxx/display.jpg";
    $output_path = "xxxxxxxxx/output.jpg";

    echo "convert -sample 640x427 $input_path $display_path<br>";
    exec ("convert -sample 640x427 $input_path $display_path");

    echo"Original:<br><img
    src=\"http://geoffsoper.co.uk/test/upload/039_35a.jpg\"><br>";
    echo"Imagemagick raw:<br><img
    src=\"http://geoffsoper.co.uk/test/upload/test/raw.jpg\"><br>";

    $display_s = filesize($display_path);
    $display_handle = fopen($display_path, "r");
    $display_content = fread($display_handle, $display_s);
    $display_content = addslashes($display_content);

    $sql = "INSERT INTO `t_photos` (`type`, `display_content`) ";
    $sql .= "VALUES ('image/jpeg', '$display_content');";
    // echo "sql = $sql<br>";
    db_query($sql, __LINE__, __FILE__);
    $id = mysql_insert_id($link);

    echo"Database: <br><img src=\"get_photo_test.php?id=$id\"><br>";

    $sql = "SELECT `display_content`";
    $sql .= "FROM `t_photos` ";
    $sql .= "WHERE `id` = $id";
    $photo_result = db_query($sql, __LINE__, __FILE__);
    $photo_row = mysql_fetch_assoc($photo_result);

    //extract raw from database save as id
    $handle = fopen($output_path, 'w');
    fwrite($handle, $photo_row['display_content']);
    fclose($handle);

    echo"Database -> file: <br><img
    src=\"http://geoffsoper.co.uk/test/upload/output.jpg\"><br>";

    ?>

    <?php
    include('xxxxxxx/test.cfg');
    if ($config_file_included != true)
    {
    die('Configuration file not found.');
    }
    include($admin_inc_dir . 'db.inc');

    // Connecting, selecting database
    db_start($db_host, $db_user, $db_password, $db_database);

    $sql = "SELECT type, display_content as output ";
    $sql .= "FROM t_photos ";
    $sql .= "WHERE id = {$_REQUEST['id']}";
    $result = db_query($sql, __LINE__, __FILE__);

    if(mysql_num_rows($result) == 1)
    {
    $fileType = @mysql_result($result, 0, "type");
    $fileContent = @mysql_result($result, 0, "output");
    }
    else
    {
    echo "Record doesn't exist.";
    }

    $img = imagecreatefromstring($fileContent);
    imagejpeg($img);
    ?>

    --
    Remove nospam in email address to reply


    Geoff Soper Guest

  2. Similar Questions and Discussions

    1. MySQL Database not retrieving the full database
      Hi, I am using MySQL and PHP for my repository. It has 500+ records. till now it was displayiing all records in the database, but since from one...
    2. Images and mysql
      Hi all, I was wondering If anyone knows how to insert images into MySQL and how to retrieve and display the images... VIA php. Thanks
    3. Displaying Images from the Mysql Database
      Hi! everyone, i need help with displaying the images from the database. I successfully inserted an image to mysql database by converting it to...
    4. Displaying images from a MySQL Database
      Hi I'm have difficulty displaying images that are stored in a MySQL database. I've tried using the odbc.net provider, the ByteFX provider and the...
    5. Images from MySQL
      Hi All I'm trying to store images in a MYSQL db, the images at being uploaded for an external program in a blob field. I have had a look round...
  3. #2

    Default Re: Problems with images in MySQL database

    "Geoff Soper" <news.nospam@alphaworks.co.uk> wrote in message
    news:3fa3ee39$0$250$cc9e4d1f@news.dial.pipex.com.. .
    > I'm having problem with images becoming corrupted after loading them into
    a
    > MySQL database and extracting them again. I'm pretty sure it's todo with
    > escaping slashes but I'm at a loss as to how to confirm this and find a
    > solution.
    >
    > A piece of test code is below, the import script displays the image before
    > it goes in and then uses a second script to display it straight from the
    > database as well as extracting it to a file and displaying that. It's
    > scruffy as it's stripped from a larger system but it works (or more
    > accurately breaks!) Imagemagick is used for the scaling operation.
    >
    After finally posting to usenet after ages of puzzling over this the answer
    strikes me! I was using a BLOB which has a size limit of 64k! Now I've
    changed to a MEDIUMBLOB all is OK!

    Sorry,
    Geoff

    --
    Remove nospam in email address to reply


    Geoff Soper Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139