mysql image

hello guys!

thank you very much for this awesome framework! i allready did buy the book last week from amazon to learn from you and to support you…

the ar thing is a little bit kind of magic for me but i’ll get used to it…

anyway i want to show an image from mysql-db.

here is the standalone example:




<?php


$username = "root";

$password = "root";

$host = "localhost";

$database = "viralsnack";


@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());


@mysql_select_db($database) or die("Can not select the database: ".mysql_error());


$id = $_GET['id'];


if(!isset($id) || empty($id)){

die("Please select your image!");

}else{


$query = mysql_query("SELECT * FROM tbl_post WHERE id='".$id."'");

$row = mysql_fetch_array($query);

$img = $row['img'];


header('Content-type: image/gif');

echo $img;


}


?>



or simple:




header('Content-type: image/gif');

echo $img;



But how with Yii!?

I know for shure it has to do with the header line above, but i am trying and trying hard to do this with the blog demo example and i just can’t. I have putted it already in every method but still no improvements.

here is one of the many variations did try with yii:




// in protected/views/post/_view.php in some div class

// yes i did try with ".gif" after it too

// yes i did try with print too

// yes i did try with setting another variable

// yes i did try with another css

// yes i did try within the widget and without

// yes i did try on a single page like the about page

// yes i did try put header('Content-type: image/gif'); on every single method of the controller all together and one by one

// yes i am getting crazy

echo '<img src ="'.$data->img.'">';



i already found some posts about header stuff but it was too generic or to simple, anyway i don’t get it. i google and searched alot, but nothing helped.

and no, i would dont want any extensions. i would to hold this very simple.

thank you for your time!

I’m not sure I really understood what do you try to do. You should create a new action which should display your image content, and then in src attribute of your image tag you should create link on this action.




// In your PostController

public function actionShowimage()

{


    $image = Post::model()->findByPk($_GET['id']);


    header('Content-type: image/gif');


    echo $image->img;

    exit();


}


// In view

<?php echo CHtml::image($this->createUrl('post/showimage', array('id' => $imageId))); ?>




thanks! i am trying that out, but the image is in the db.

i am not trying to get the path of it, or trying to store it some where and then showing it.

i normally can access the img row from the db. with this: $data->img, but…




<div class="post">

    <div class="title">

        <?php echo CHtml::link(CHtml::encode($data->title), $data->url); ?>

    </div>

    <div class="author">

		posted by <?php echo $data->author->username . ' on ' . date('F j, Y', $data->create_time); ?>

    </div>

    <div class="content">

        <?php

        $this->beginWidget('CMarkdown', array('purifyOutput' => true));

        echo $data->description;

        [b]echo '<img src ="'.$data->img.'">';[/b]

        $this->endWidget();

        ?>

    </div>


    <div class="nav">

        <b>Tags:</b>

        <?php echo implode(', ', $data->tagLinks); ?>

        <br/>

        <?php echo CHtml::link('Permalink', $data->url); ?> |

        <?php echo CHtml::link("Comments ({$data->commentCount})", $data->url . '#comments'); ?> |

		Last updated on <?php echo date('F j, Y', $data->update_time); ?>

    </div>

</div>




because of the wrong header the browser only interpreted this:




<img src ="GIF89a23÷™ #$#&&,)003/454787;:7:;:?A>BB>D |...| DZžß·’ݼåºÔº¡À¾¾ØÀªÆ¿ãȯæÌ´æÑ¿ÅÄÄÈÆÅÍÌÌÐÊÇÒÎËÔÐÏÕÔÔÝ×ÔÜØÖÛÚÚäÒÂâÕÌ



frantic is right nothing else to add here but I would add that you double check whether that image in your DB is of the exact mime type.

the mime type is correct. i am beeing able to show it already with a normal single php script.

with frantic method i got this:


<img src="/yiiblog/index.php/post/showimage?id=2" alt="">

and it would be great if there were any files.

as i sad i wannt to display it directly from the db.

actionShowimage method takes the image directly from DB as you want:


$image = Post::model()->findByPk($_GET['id']);

I can’t understand why it doesn’t working. Try to run /yiiblog/index.php/post/showimage?id=2 separately ( not in src attribute ), what is it returning?

in my view i have this:


<div class="content">

        <?php

        $this->beginWidget('CMarkdown', array('purifyOutput' => true));

        echo $data->description;

        echo CHtml::image($this->createUrl('post/showimage', array('id' => $data->id)));

        $this->endWidget();

        ?>

    </div>

And this is coming up:

url:

http://localhost/yiiblog/index.php/post/1/Welcome

http://localhost/yiiblog/index.php/post/showimage?id=1


<img src="/yiiblog/index.php/post/showimage?id=1" alt="">

http://localhost/yiiblog/index.php/post/2/Test

http://localhost/yiiblog/index.php/post/showimage?id=2


<img src="/yiiblog/index.php/post/showimage?id=2" alt="">

Your view is good. I mean what the script index.php/post/showimage?id=1 are returning? Is it returning anything at all? Maybe exceptions?

that’s what’s coming out:

877

01.png

878

02.png

i can’t understand it.

this couldn’t help me either: http://www.yiiframew…l__Content-type

Strange. Try out to comment header(‘Content-type: image/gif’);

What it will render then?

Check out this thread, maybe it helps.

thanks alot guys!

it’s working now!

but it’s kind of wierd…

only working separetly at: http://localhost/yii.../showimage?id=1

so i can’t use it as:


echo CHtml::image('http://localhost'.$this->createUrl('post/showimage', array('id' => $data->id)));

with this function:




 public function actionShowimage() {

    	$data = Post::model()->findByPk($_GET['id']);

    	echo '<img src="data:image/png;base64,' . chunk_split(base64_encode($data->img)) . '" />';

    	exit();

	}



on my view.

to get it to work i have to use blob. more infos here: http://www.phpriot.c…ages-in-mysql/6

and using this on my view but not between the widget:




<div class="content">

    	<?php

    	$this->beginWidget('CMarkdown', array('purifyOutput' => true));

    	echo $data->description;

    	$this->endWidget();

    	echo '<img src="data:image/png;base64,' . chunk_split(base64_encode($data->img)) . '" />';

        ?>

 </div>

amazing support here!

love you guys!

Hey is good news that you got it rolling. Nevertheless, I think your only problem was the HTML purifier.

Good work man

when i try this code for displaying my image from database to view it is prompting an error

undefined variable:data

if i change it to model and give column name besides no image is displayed