@sultankhan
and my code is the follwing but instead of iamge it show garbage value
What do you mean garbage value?
LastMitch
Industrious Poster
4,212 posts since Mar 2012
Reputation Points: 134
Solved Threads: 336
Skill Endorsements: 45
<img src="<?php echo $dnn['content']; ?>" />
You can't display the content like this. Your page seems to be doing quite a lot of work. It may be better to have it chunked into smaller files.
You could do this:
header("Content-type: image/gif"); // but get the right type
echo $dnn['image'];
Or:
<img src="data:image/gif;base64,<?php echo base64_encode($dnn['contents']);?>" />
The disadvantage of the above AFAIK is that it can't be cached.
If I were to do this, I'd probably have an image.php file with the code above in it (and some filename parsing to get the mime type), so something like this:
<img src="image.php?id=<?php echo $dnn['id'];?>" />
The image.php file then does something like:
//check get id exists then:
$id = $_GET['id'];
//clean for DB if using mysql
//then get the content from the DB via query
//state the mime type
//echo the contents
diafol
Keep Smiling
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57
Storing an image in a varchar column is probably causing the issue, use a binary column type.
pritaeas
Posting Prodigy
9,316 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86
Have you tried diafol's suggestion (using a separate image script)?
pritaeas
Posting Prodigy
9,316 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86
I was talking about the separate script for showing your image. Show the code you have for that one (what diafol said, not with all the markup you posted in your question).
pritaeas
Posting Prodigy
9,316 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86
Don't use a DOCTYPE for an image generator. The mime type is there so that an image is displayed - there should be no other output before of after. So the way you've gone about this is wrong. As you're using mysql_*, I'll do so too, but moving to mysqli_* or PDO functions would be better.
Your image.php file:
$getDefault = true;
//assume mysql details included here somewhere
if(isset($_GET)){
$id = intval($_GET);
$r = mysql_query("SELECT type, content FROM upload WHERE id = $id");
if(mysql_num_rows($r)){
$dnn = mysql_fetch_assoc($r);
$getDefault = false;
}
}
if($getDefault){
$r = mysql_query("SELECT type, content FROM upload WHERE id = 1"); //this is the default image
$dnn = mysql_fetch_assoc($r);
}
$mimeType = $dnn['type'];
$content = $dnn['content'];
header("Content-type: $mimeType");
echo $content;
That's it. Maybe some more error handling for production code. But along those lines.
You just call it from links - which can be static:
<img src="image.php?id=74" />
Or dynamic:
<img src="image.php?id=<?php echo $someIDVar;?>" />
Or:
echo "<img src='image.php?id=$someIDVar' />";
As I said in my previous post - your files are trying to do too much. You can certainly include files, and these help to compartmentalize functions.
@pritaeas please solve this for me...........
Oops. Sorry p, this was for your eyes only, by the sounds of it. :)
diafol
Keep Smiling
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57
You need to use a JOIN:
e.g.
SELECT i.*, u.username FROM image AS i INNER JOIN users AS u ON i.user1 = u.user_id
Now you get the 'username' into the $row array too. Note - I don't know what your users table is called nor the primary key for that table - so you may need to change 'users' and 'user_id'
diafol
Keep Smiling
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57
Question Answered as of 5 Months Ago by
pritaeas,
diafol
and
LastMitch