i'm just feeling that my head will explode unless someone help me with this problem:
I have stored a pair of TIFF images (related by a common key) for each one of almos 100.000 registries. And I create a PHP script that receives a key and echo the tiff image, so the browser return the tiff image:
<?php
// Determine the primary key to relate with the image table
$numero_registro = $_GET['numero_registro'];
$imagen = $_GET['imagen'];
if ($numero_registro != "")
{
$con = mysql_connect("localhost","XXXXX","XXXXXX");
if (!$con)
{
die('Problems with db: ' . mysql_error());
}
mysql_select_db("XXXXX", $con);
$result = mysql_query("SELECT img FROM image_table i WHERE i.fk_civil_registry_code = $numero_registro");
$i = 1;
while($row = mysql_fetch_array($result) )
{
if ( $imagen == $i )
{
#img is a long blob field
$ext="tiff";
header("Content-type: image/{$ext}");
echo $row['img'];
}
$i++;
}
mysql_close($con);
}
?>
This just works and the tiff image is displayed by the browser. But, this is a tiff image, so is displayed lonely (and viewed using alternaTiff). Until know this was no problem, cause I just needed to print a single image. But now my boss buy a big automatic duplex printer, and put it on his office, so I need a way to generate a pdf (of two pages) and put both images (echo $row['img'];) each one on a single page, so they can print the PDF.
Can anyone help me to do that?
Thank you very much.
So you want to generate a 2-page PDF which consists of a tiff image on each page?
Perhaps the following links will be of interest:
http://www.fpdf.org/
http://kevin.vanzonneveld.net/techblog/article/php_tiff2pdf/
Then you can just flush the PDF to the browser.
Are you stuck with PHP? If you can work with ASP.NET, my company has a set of tools that will display and print TIFF images from AJAX controls as well as code that will generate self-printing PDF files. If you did the latter, you could keep your web work in PHP and hook up to a .NET service that takes N tiff files and generates a single printable PDF.
To give you a sense of what that would look like, the C# code to take two tiff images and convert to PDF would be:
FileSystemImageSource images = new FileSystemImageSource(pathToTiff1, pathToTiff2);
PdfEncoder encoder = new PdfEncoder();
encoder.CreateSelfPrintingPdf = true;
encoder.Save(outputStream, images, null);
Instead of a pdf document you could also use an html document with page-break-before css properties. e.g. try
<html>
<head><title>...</title></head>
<body>
<div><img src="http://sstatic.net/so/img/logo.png" /></div>
<div style="page-break-before:always;"><img src="http://sstatic.net/so/img/logo.png" /></div>
</body>
</html>
and then go to the print preview in your browser.
Related
This is an odd question but I'm stuck on how I would achieve this and I am unable to find any methods of doing so.
I have a simple php script that takes variables (containing file names) from the URL, cleans then and then uses them to generate a single image from the inputted values. This works fine and outputs a new png to the webpage using:
imagepng($img);
I also have a facebook sharing script in PHP that takes a filepath as an input and then shares the image on the users feed where this statement is used to define the image variable:
$photo = './mypic.png'; // Path to the photo on the local filesystem
I don't know how I can link these two together though. I would like to use my generation script as the image to share.
Can anyone point me in the right direction of how to do this? I am not the master of PHP so go easy please.
-Tim
UPDATE
If it helps, here are the links to the two pages on my website containing the outputs. They are very ruff mind you:
The php script generating the image:
http://the8bitman.herobo.com/Download/download.php?face=a.png&color=b.png&hat=c.png
The html page with the img tag:
http://the8bitman.herobo.com/Share.html
Treat it as a simple image:
<img src="http://yourserve/yourscript.php?onlyImage=1" />
yourscript.php
if($_GET['onlyimage']) {
header('Content-type:image/png'); //or your image content type
//print only image
} else {
//print image and text too
}
I want to create collage of photos with PHP. I created a test code on my localhost as follows:
<style type="text/css">
body{background:url('images/Winter.jpg'); }
#collage:after{content:"";clear:both;display:block;}
}
</style>
<?php
ob_start();
$dir = "images";
if($fp = opendir($dir))
{
while($file = readdir($fp))
{
if('jpg'==(pathinfo($file, PATHINFO_EXTENSION)))
{
$style = "style='float:left; -webkit-transform:rotate(".mt_rand(2, 30)."deg); border:solid 5px #eee;'";
$ht = "height='".mt_rand(100, 300)."'";
echo "<div class='img_div' $style>";
echo "<img src='$dir/$file' $ht >";
echo "<div style='background:#eee;font-size:20px;'>hi</div>";
echo '</div>';
}
}
}
closedir($fp);
?>
It generated the output which I want but now I want the user to be able to download it as an image file. How do I do it?
If you want that you will have to create a new blank image using something like php's GD library and position your images into that instead of printing and positioning them using html/css
(I believe I read somewhere that in the future you will be able to display html/css on a html5 canvas, if that's true you'd be able to extract an image using that, but for now I don't think that will be possible)
Short of using the new HTML5 canvas feature, that will let the browser do all the work of merging the mosaic into one single image for you (but is it supported by many browsers yet ? I honestly don't know.),
you can also create that image on the server's side, using php's GD library. That was, I think, the only way before HTML5 and may still be the best way for a while (as I said, I don't know exactly what HTML5 implementations are worth to this day)
A good place to start would be there: http://php.net/manual/en/ref.image.php (read the many examples by visitors, on that page and on the function-specific pages, they can be a valuable education).
Now, this method has one inconvenient (in addition to you having to do the whole work): it taxes your server's CPU and slows down its response. That's ok if you have only a few visitors at a time. But otherwise, your collage should be pre-calculated once and for all, so the server doesn't have to redo the job for each visit.
I've create a table where I've saved images through "BLOB". I need to show those images along with other items. But I don't know how to show those images together in the same page.
Here's my php code that displays other things in form of a table. Similarily, I wanted to display images accordingly. Any help?
<?php
$display_query = mysql_query("SELECT * FROM eportal");
echo "<table id='pageTable'><thead><tr><th>Item code</th><th>Description</th><th>Cost</th></tr></thead>";
echo "<tbody>";
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td></tr>";
}
echo "</tbody>";
echo "</table>";
mysql_close($connection);
?>
Saving images to the DB is not a good idea but if You think You need to it this way, then You can retrieve the data from DB table, encode it to base64 (http://php.net/base64_encode) and then in HTML print it in this way:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
Using PHP You would write:
echo '<img src="data:'.$image_mime_type.';base64,'.base64_encode($image_data_from_db).'" alt="My image alt" />';
As other people mentioned, storing the images in the database is usually a bad idea.
Images are not transmitted in the same HTTP response with another page data.
To show images from the database, you would need to implement a script which, given the item id, would read the field and send the image's binary data; and provide the path to the script in your form's <img src="">:
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td><td>";
print "<img src=\"image.php?id=".$row['id']."\"></td></tr>";
}
image.php is another script which outputs the value of image_blob given the eportal.id. You would also need to provide correct size and mime type in the headers.
You better just store the images in a file (accessible by the web server) and store the path fo the file in the database.
Read the Blob data and write it into the file with header type image.. and try to print it, It should display the image file.
And yes saving image or any file in DB is really a bad habit as you are increasing DB size and it slowdown the performance also.. I suggest you to just try to convert you Blob into Image but don't apply in your work. Just save the image at desired location and keep its location path into DB to fetch and save next time.
The debate of storing blobs versus storing a path to the image file on disk has been debated over and over again. Microsoft provides a research paper comparing the pros and cons of each here. With that said, to display a blob as an image you need to make a call to a separate page and output header information that tells the browser what type of image is stored.
For example:
connectToDatabase();
$sql = "SELECT image_blob FROM eportal;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $row['image_blob'];
$db->close();
In case you still want to save your images in database, you will need second script which will get those images from database and pass them to browser with correct headers.
header("Content-type: image/jpeg");
#
# Replace this with database read:
# readfile('myimage.jpg');
Also, you will need to store what kind of image u use. There will be different header for JPEG, GIF or PNG file.
I have a table in my mysql db that contains path for images.
I want to display thumbnails of this images.
Can you tell me what is the best solution for that.
This is how i display the full size images from db:
<?php for($i=1; $i<=5; $i++)
{$query = mysql_query("SELECT * FROM photos WHERE product_id='".$i."'");
$row = mysql_fetch_array($query);
$t="img/";
$file = $t .$row[1];
echo "<li><img src = " . $file . "></li>";
} ?>
You can use this http://mightystuff.net/php-thumbnail-script
Either resize the images in css/html (bad) or make a php script that gets a filename as input then returns the scaled down version as output (better) or have thumbnails stored on your server (best)
You should avoid having to resize the images using CSS or HTML. Before saving the images onto the server, you should use ImageMagick to resize it. Do you really want to store a bunch of 6MB images? If that's not feasible, then saving the images as is and pointing to the path would suffice. However, if you end up with hundreds or thousands of images, you'll want to explore a better option.
I agree with the approach of storing the file path (instead of the binary file itself).
So your question is "how do you create a thumbnail?"
The best answer is ImageMagick:
http://www.imagemagick.org/
http://valokuva.org/?p=45
http://php.net/manual/en/book.imagick.php
I'm trying to display multiple images using PHP and MySql database, even if using the while loop I don't get all of the images, I only get one, I mean the first one in the table. What's the problem ?
I'm using a table ID_IMAGE (int, pk, auto increment) and myImage (blob)
$query = mysql_query("SELECT myImage FROM image");
while($data=mysql_fetch_array($query)) {
header('Content-type: image/jpg');
echo $data['myImage'];
}
A possible way to solve this problem is to have a separate script to dynamically output the contents of the image eg. :
image.php
header('Content-type: image/jpg');
// DataBase query and processing here...
echo $data['myImage'];
and call it whenever you need to show images stored in your DB eg. inside your loop:
echo '<img src="image.php?id=' . $data['id'] . '">';
But storing images in the database will take a toll on your server and unless they're really small or you have a good reason to do so, you should only store their physical location on the disk.
You can also use this approach if you wish to hide image location from your users, or control access, but there are better and faster alternatives for that case.
Just to mention the possibility to embed the images directly in html by encoding them, you can use this:
$query = "SELECT myImage FROM image";
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<img src="data:image/jpg;base64,' . base64_encode($row['myImage']) . '">';
}
}
Pro:
The browser does not need to load the images over an additional network connection
You can query and display multiple images in one request
Con:
If the image(s) are big and/or there will be many images, the page will be load slow
Encoding an image to base64 will make it about 30% bigger.
For more information about base encode images:
http://davidbcalhoun.com/2011/when-to-base64-encode-images-and-when-not-to/
base64 encoded image size