LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-09-2007, 03:06 PM   #1
ilpadrino
Member
 
Registered: Oct 2003
Location: Spain
Distribution: Fedora
Posts: 104

Rep: Reputation: 15
php image blob managing cannot get image from html


Here is my problem. I am trying to upload an image and store it in mysql. So I have created a new table (called 'main') with four fields (id, short, historia, contenido (this last as mediumblob type)).

So my script to upload the image is as follow (called insert.htm):

PHP Code:
   <html>
    <
head>
      <
title>Nuevo registro</title>
    </
head>
    <
body bgcolor="silver">
    <
form method="post" action="insert2.php" enctype="multipart/form-data">
    <
h1>Introducir nuevo registro</h1
    <
h3>Rellene los detalles descriptivos
    
Los campos marcados en <font color="navy" size=5><b>colores</b></fontson obligatorios.</h3>
    <
table>
    <
col span="1" align="right">
    <
tr>

    <
td><font color=green size=5><b>NumHistoria</b></font></td>
        <
td><input type="text" name="historia" size=7></td>

    <
tr>
       <
td><font color="blue" size=5><b>Descripcion breve:</b></font></td>
       <
td><input type="text" name="short" size=50></td>
    </
tr>

    <
tr>    
       <
td><font color="black" size=4><b>Imagen:</b></font></td>
       <
td><input name="userfile" type="file"></td>
    </
tr>

    <
tr>
          <
td><input type="submit" value="Guardar"></td>
    </
tr>
    </
table>
    <
input type="hidden" name="MAX_FILE_SIZE" value="30000">
    
</
form>
</
body>
</
html
This script select the image from a browsed directory and send it to another script which store the image into the database. This script is 'insert2.php' and this is the code:

PHP Code:
<?php


$tmp_name
$_FILES['userfile']['tmp_name'];
$fp fopen($tmp_name"rb");
  
$tfoto fread($fpfilesize($tmp_name));
  
$tfoto addslashes($tfoto);
  
fclose($fp);


$historia=$_POST['historia'];
$short=$_POST['short'];
$link=mysql_connect() 
or die(
mysql_error($link));



mysql_select_db('test',$link) or die(mysql_error($link));
$sql"INSERT INTO main (historia,short,contenido)

       VALUES
      ('$historia','$short','$tfoto')"
;

mysql_query($sql,$link) or die(mysql_error($link));

unlink($tmp_name);

?>
<html>

<h1><center>DATOS GUARDADOS CORRECTAMENTE COMO NO PODIA SER DE OTRA FORMA</center></h1>

</html>
As I can check in phpMyAdmin, the image is stored properly. So now I want to get the image to show it in my browser as a thumbnail. I select the image by the 'historia' field, so for selecting the image in the database, first I choose the 'historia' number with the following script (consulta.php):
PHP Code:
<html>
<
head>
<
title>Consulta</title>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</
head>
<
body>
<
form name="frmimage" id="frmimage" method="post"
    
enctype="multipart/form-data" action="recibe.php">
    
Historia: <input type="text" id="historia" name="historia" /><br />
    <
input type="submit" name="enviar" id="enviar" value="Consultar" />
        
</
form>
</
body>
</
html
This script send the 'historia' number to another which open the database and get the image selected by 'historia'. This script also convert the image into a thumbnail and show it. The code is the following (recibe.php):
PHP Code:
<?php
$historia
=$_POST['historia'];
$link=mysql_connect() or die(mysql_error($link));
mysql_select_db(test,$link) or die(mysql_error($link));
$sql="SELECT historia,short,contenido

             FROM main
             WHERE historia=$historia"

$conn=mysql_query($sql,$link) or die(mysql_error($link));
$datos=mysql_fetch_array($conn);
$image=imagecreatefromstring($datos[2]);
$width imagesx($image);
$height imagesy($image);
$imgw=100;
$imgh=$height/$width $imgw;

$thumbnail imagecreatetruecolor($imgw,$imgh);

imagecopyresampled ($thumbnail,$image,0,0,0,0,$imgw,$imgh,$width,$height);

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

$rv=imagejpeg($thumbnail,'',100)."<br/>";
imagedestroy($thumbnail);
?>
Everything is going OK .But if I try to print the thumbnail into html code (for example to insert it into a table), the result is not as expected. I modify the last code as follow:

PHP Code:
<?php
$historia
=$_POST['historia'];
$link=mysql_connect() or die(mysql_error($link));
mysql_select_db(test,$link) or die(mysql_error($link));
$sql="SELECT historia,short,contenido

             FROM main
             WHERE historia=$historia"

$conn=mysql_query($sql,$link) or die(mysql_error($link));
$datos=mysql_fetch_array($conn);
$image=imagecreatefromstring($datos[2]);
$width imagesx($image);
$height imagesy($image);
$imgw=100;
$imgh=$height/$width $imgw;

$thumbnail imagecreatetruecolor($imgw,$imgh);

imagecopyresampled ($thumbnail,$image,0,0,0,0,$imgw,$imgh,$width,$height);

?>

<html>

<h1>Consult:</h1>


<?php
header 
('Content-Type: image/jpg');
imagejpeg ($thumbnail,'',100);
?>
</html>
But the result is a lot of strange symbols instead of the image. If I substitute the line 'imagejpeg ...' for 'echo $thumbnail;' I get a 'image resource #4' instead of the image.

In summary, if I put the php code to print the image outside the
html tags, the image can be output correctly, but if I put the same code embedded into html, only strange symbols are output.


Have anybody any idea what I am doing bad?

Thanks in advance
 
Old 04-09-2007, 04:11 PM   #2
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
Just a simple idea, try changing the content type from
Code:
header ('Content-Type: image/jpg');
to
Code:
header ('Content-Type: image/jpeg');
I don't think that image/jpg is valid but I could easily be wrong.
 
Old 04-09-2007, 04:56 PM   #3
ilpadrino
Member
 
Registered: Oct 2003
Location: Spain
Distribution: Fedora
Posts: 104

Original Poster
Rep: Reputation: 15
Thanks for the advice, but unfortunately the same results.
 
Old 04-09-2007, 06:50 PM   #4
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
The only other thing I could suggest is defining the <body></body> tags and putting it inside them, may help. If not, I am out of ideas.
 
Old 04-10-2007, 03:03 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,159

Rep: Reputation: 258Reputation: 258Reputation: 258
Hi

You can't just put image data inside HTML - it get's interpreted as data - remember an image is just a bunch of numbers. The browser thinks it is text.

If you delete all HTML from the last page, and call it for example "showimage.php" and change the parameter from $_POST to $_GET, you can show it like this:
<img src="showimage.php?historia=$historia">

There is also a trick you can use, but it doesn't work in all browsers:
echo "<IMG SRC=\"data:image/jpg;base64,$data\">";

Notice the strange URL? It's basically saying that the image is included in the URL (Base 64 encoded).

However PHP lacks a function like ImageJPG that doesn't output to the browser. But there is a trick. You can output it to an output buffer and grab it to a string:

ob_start();
ImageJPEG($new_image);
$data = ob_get_contents();
ob_end_clean();
ImageDestroy($new_image);
$data = base64_encode($data);
echo "<IMG SRC=\"data:image/jpg;base64,$data\">";

But as I said, not all browsers will show this correctly.
 
Old 04-10-2007, 02:41 PM   #6
ilpadrino
Member
 
Registered: Oct 2003
Location: Spain
Distribution: Fedora
Posts: 104

Original Poster
Rep: Reputation: 15
OK, but this stament : "<img src="showimage.php?historia=$historia">" must be in a html file or in the same "showimage.php" ?, cause $historia is only defined in this last file. In tha case of this statement must be in a html file, then I have to pass the value of $historia using php code?

Thanks in advance
 
Old 04-15-2007, 10:05 AM   #7
ilpadrino
Member
 
Registered: Oct 2003
Location: Spain
Distribution: Fedora
Posts: 104

Original Poster
Rep: Reputation: 15
OK, I have found out that is not a problem related with GET or POST statement. It is clear that the image must be output into the php code and not between the html tags. The only possibility is to use the GET statement and call the php file which convert the image, BUT the output cannot be showed in a table as the php command 'imagejpeg' cannot do that. It only prints on the left upper corner of the screen, so I guess that the solution could be to save the thumbnail also in the database.

Someone who could confirm me this?

Thanks in advance
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
html image load ygloo Programming 3 09-16-2006 02:04 PM
Few questions, HTML image links Taigrr Programming 2 02-28-2006 06:50 PM
Image slicer and HTML cleaner remi Linux - Software 0 12-01-2005 04:53 PM
HTML & Image rickycen Linux - General 8 08-15-2005 01:30 AM
HTML code for an image? chadm1967 General 4 09-04-2001 05:41 PM


All times are GMT -5. The time now is 12:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration