Welcome Guest, Not a member yet? Register   Sign In
displaying BLOB data from DB
#1

[eluser]junaids[/eluser]
hi.
i successfully stored images into the DB, but still have problem with max size of images upload.

now i want to retrieve images from the DB. any idea?
#2

[eluser]Phil Sturgeon[/eluser]
If you are using MySQL and want to make the image sizes slightly smaller you can use COMPRESS() and UNCOMPRESS().

Then you want to make a controller like this:

Code:
<?php
class Media extends Controller

  function image($image_id)
  {
    // This will be image string
    $image_data = $this->images->get($image_id);
    
    $image = imagecreatefromstring($image_data);
  
    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);
    exit;
  }

}
?>

That should do the job.
#3

[eluser]umefarooq[/eluser]
to make it more general also save content-type of the image in database also so its easy for you which type of image you have to create, either jpg,png or gif or while saving only take specific type of images
#4

[eluser]Phil Sturgeon[/eluser]
I was going to write in a switch for that but decided it would take 1 minute too long. Storing the content type is always a good idea with images. :-)

Don't just save the extension either cause the variations can cause complications through your code. When you save it use a switch like:

Code:
switch($ext) {
  case 'jpg':
  case 'jpe':
  case 'jpeg':
    $type = 'jpeg';
  break;

  case 'xbmp':
    $type = 'bmp';
  break;

  case 'png':
  case 'gif':
    $type = $ext;
  break;

}

Etc. You can also add a default in there to use whatever $ext is, or fail with an error/validation message.
#5

[eluser]junaids[/eluser]
should i call this function in the view where i have to show the image?
#6

[eluser]Colin Williams[/eluser]
It will be the src attribute of an img tag. The controller shows the image. It is the image.
#7

[eluser]junaids[/eluser]
here my controller
Code:
function image()
          {
             $u = $this->session->userdata('username');
             $this->db->where('username', $u);
            $image_data = $this->images->get($Profile_picture);
            $image = imagecreatefromstring($image_data);
              header('Content-Type: image/png');
            imagepng($image);
            imagedestroy($image);
            exit;
          }

and here the view where i m displaying the image.
<img >

no image is displayed. any idea?
#8

[eluser]junaids[/eluser]
sorry
the image code in the view is,
Code:
<img >
#9

[eluser]junaids[/eluser]
[code]img src = &lt;?=base_url()."user/image"?&gt;
#10

[eluser]junaids[/eluser]
anybody home!!!!!




Theme © iAndrew 2016 - Forum software by © MyBB