Best way of storing image in datbase with respect to optimizing:

Discussion in 'PHP' started by Talat50, Oct 5, 2010.

  1. #1
    Here this is my code:
    do u think it is optimized with respect to maximum users and size.?


    <?php
    error_reporting(E_ALL);
    $change="";
    $abc="";

    $server = "localhost";
    $login = "root";
    $password = "";
    define ("MAX_SIZE","20");
    function getExtension($str) {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
    }

    $errors=0;

    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
    $image =$_FILES["file"]["name"];
    $uploadedfile = $_FILES['file']['tmp_name'];


    if ($image)
    {

    $filename = stripslashes($_FILES['file']['name']);

    $extension = getExtension($filename);
    $extension = strtolower($extension);


    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
    {

    $change='<div class="msgdiv">Unknown Image extension </div> ';
    $errors=1;
    }
    else
    {

    $size=filesize($_FILES['file']['tmp_name']);


    //if ($size > MAX_SIZE*2048)
    //{
    // $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
    // $errors=1;
    //}


    if($extension=="jpg" || $extension=="jpeg" )
    {
    $uploadedfile = $_FILES['file']['tmp_name'];
    $src = imagecreatefromjpeg($uploadedfile);

    }
    else if($extension=="png")
    {
    $uploadedfile = $_FILES['file']['tmp_name'];
    $src = imagecreatefrompng($uploadedfile);

    }
    else
    {
    $src = imagecreatefromgif($uploadedfile);
    }

    list($width,$height)=getimagesize($uploadedfile);


    $newwidth=60;
    $newheight=($height/$width)*$newwidth;
    $tmp=imagecreatetruecolor($newwidth,$newheight);


    $newwidth1=30;
    $newheight1=($height/$width)*$newwidth1;
    $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

    imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);


    //$filename = "images/". $_FILES['file']['name'];

    $filename1 = "images/small". $_FILES['file']['name'];
    $nsize=filesize($filename1);

    echo "\n<br>Width:$newwidth,, Height: $newheight1, Type: $extension ,Size= $nsize;, MIME: $extension";

    if(!$data = addslashes(@fread(@fopen($filename1, "r"), @filesize($filename1)))){
    die("\n<BR>Cannot read temp file: $filename1");
    }


    $link = mysql_connect($server, $login, $password);
    if (!$link) {
    die("\n<BR>Could not connect $server:" . mysql_error());
    }

    $db_selected = mysql_select_db("test");

    if (!$db_selected) {
    die ("\n<BR>Can\'t use Table : $db_selected" . mysql_error());
    }

    $query = "INSERT INTO tbl_imageb ";
    $query .= " (image_type, image_width, image_height,image_size, image_data) ";
    $query .= " values ";
    $query .= " ('$extension ', '$newwidth1', '$newheight',' $nsize ', '$data') ";

    $result = mysql_query($query);

    if (!$result) {
    $message = '<BR>Invalid query: ' . mysql_error() . "\n";
    die($message);
    }
    $image_id = mysql_insert_id() ;

    printf("\n<br>Last inserted record has id %d\n",$image_id);

    "\n<BR><BR>And The Photo is:";
    echo "\n<IMG SRC=images/?id=$image_id\" />";

    mysql_close($link);

    imagejpeg($tmp,$filename,50);

    imagejpeg($tmp1,$filename1,50);

    imagedestroy($src);
    imagedestroy($tmp);
    imagedestroy($tmp1);

    }}
    }


    //If no errors registred, print the success message
    if(isset($_POST['Submit']) && !$errors)
    {

    // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
    $change=' <div class="msgdiv">Image Uploaded Successfully!</div>';
    }


    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
    <meta content="en-us" http-equiv="Content-Language">

    <title>picture Resize</title>

    <link href=".css" media="screen, projection" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <script type="text/javascript" src="js/jquery_002.js"></script>
    <script type="text/javascript" src="js/displaymsg.js"></script>
    <script type="text/javascript" src="js/ajaxdelete.js"></script>


    <style type="text/css">
    .help
    {
    font-size:11px; color:#006600;
    }
    body {
    color: #000000;
    background-color:#999999 ;
    background:#999999 url(<?php echo $user_row['img_src']; ?>) fixed repeat top left;


    font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;

    }
    .msgdiv{
    width:759px;
    padding-top:8px;
    padding-bottom:8px;
    background-color: #fff;
    font-weight:bold;
    font-size:18px;-moz-border-radius: 6px;-webkit-border-radius: 6px;
    }
    #container{width:763px;margin:0 auto;padding:3px 0;text-align:left;position:relative; -moz-border-radius: 6px;-webkit-border-radius: 6px; background-color:#FFFFFF }
    </style>

    </head><body>
    <div align="center" id="err">
    <?php echo $change; ?> </div>
    <div id="space"></div>

    <div id="container" >

    <div id="con">





    <div id="posts">
    &nbsp;&nbsp;&nbsp;&nbsp;<img src="<?php echo $filename; ?>" /> &nbsp;&nbsp;&nbsp;&nbsp;<img src="<?php echo $filename1; ?>" />
    <form method="post" action="" enctype="multipart/form-data" name="form1">

    <input size="25" name="file" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/>

    <input type="submit" id="mybut" value="Upload" name="Submit"/>


    </form>

    </body></html>
     
    Talat50, Oct 5, 2010 IP
  2. ilyakar

    ilyakar Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i think if you post something that long, that not many people will read the whole thing, thus wont answer the question :)
     
    ilyakar, Oct 7, 2010 IP
  3. goldenretriever

    goldenretriever Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Honestly, I wouldn't actually store an image in a database.

    Just save it on the filesystem instead. You can, for instance, store it based on the ID of the SQL row and, in addition, save the image filename as a VARCHAR which in turn can be sent in headers() along with its content type and streamed much same as you would in your case as well. There will be many benefits to doing it this way.

    1) Your database footprint will become much smaller/faster.
    2) Your file operations will become exponentially easier (simple file operations versus an array of sql statements).
    3) You will set good standards for future developments within this project and others.
     
    goldenretriever, Oct 21, 2010 IP
  4. scriptinstaller

    scriptinstaller Peon

    Messages:
    109
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    above post is correct, do NOT store images in database, kinda silly
     
    scriptinstaller, Oct 23, 2010 IP