Php pdf image preview code - php

I try to generate php code in order to preview an image from a pdf file. Here is my code. There is an error but i cannot understand where it is. I would appreciate any help. Thank you.
<?php
//Get Menu Bar
include('navigate.php');
?>
<table cellspacing="4" width=100%><tr><?php if(!$vol){ ?><td bgcolor="eeeeee" width="50%" valign="top">
<h3 style=" text-shadow:#003">Latest News:</h3>
<?php
$latest=mysql_query("select * from articles ORDER BY id DESC LIMIT 1", $link);
while ($article = mysql_fetch_array($latest)) {
$thevolume= $article['volume'];
$title= $article['title'];
$author= $article['author_main'];
$abstract= $article['abstract'];
$pdf= $article['pdf'];
$im = new imagick('pdf');
$im->setImageFormat('jpg');
?>
<a href="<?php echo $pdf; ?> "PDF: ?php echo "$title - $author"; ?>
<img src="<?php echo $im; ?> " WIDTH="98%" border="1" title="<?php echo "$title - $author"; ?> " caption="Click here to open PDF" />
</a>

You must to pass as parameter a path to PDF file, not only the string 'pdf': http://php.net/imagick.construct
If $article['pdf'] contains it you must to use:
$im = new imagick(realpath($pdf) . '[0]');
$im still being a resource that contains a object instance of imagick, and not the image or pdf document itself, so you must to do this to inline image in HTML (if you want to use JPEG, I would use PNG):
<img src="data:image/jpg;base64,<?php
echo base64_encode($im->getImageBlob());
?> " WIDTH="98%" border="1" title="<?php
echo htmlspecialchars("$title - $author");
?> " caption="Click here to open PDF" />
Note the usage of htmlspecialchars ( http://php.net/htmlspecialchars ) to output strings to navigator and getImageBlob ( http://php.net/imagick.getimageblob ) to get image data (if available) and finally I will encode it in base64 to inline it in HTML document.
If you want to link to a cached version of image you must to save it in a temporary file or create another PHP script to generate the image from PDF file.
Best regards,
Edit 1: How to create a JPEG thumbnail, from PHP manual:
You should read this: http://php.net/imagick.setimageformat#89210
This example will help you:
// convert to JPEG
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(60);
$im->setImageFormat('jpeg');
Probably you will wish to reduce image size:
$im->resizeImage(290, 375, imagick::FILTER_LANCZOS, 1);
And remember to import the page number (beginning with 0, not 1) adding '[0]' to file name as edited.
Good luck!

Related

FPDF / How to make display correctly

I'm using the fpdf library to output a pdf from html. The .pdf is being created because I can email it to myself and it arrives in the correct format but if I want to download the .pdf as an option without emailing, the output is illegible. The output appears in a browser window(see attached screen shot) and I'm unsure how to fix this issue.
I've also attached a screenshot of how we have our report options set up. 1. HTML 2. PDF 3. Download 4. Email -- the HTML and Email options work, the PDF and Download options do not. I'm focusing on the Download option in this question.
This is the output code that I've tried to test out but no luck
//$pdf->Output("D","D:/example2.pdf");
//$content = $pdf->Output("","S");
//$pdf->Output(); //Outputs on browser screen
//Outputs on browser screen
$pdf->Output();
//echo file_get_contents($pdf);
//readfile($pdf);
the $pdf->Output(); is generating the illegible code the readfile
echo and file_get_contents throw errors
the $pdf->Output(... gives me an error that says Incorrect Output
Destination (see attached screenshot)
Need guidance -- thanks for any help.
here is the full code:
<?php
$m_header = '<link href="shared/report.css" rel=stylesheet type="text/css">';
$m_body_tag = ' scroll=no';
require_once($DOCUMENT_ROOT."inc/top-2.inc.php");
$i_get_sid = isset($_GET["sid"]) ? (int)$_GET["sid"] : $i_sid;
$i_get_pass = isset($_GET["a"]) ? $_GET["a"] : $_SESSION['r_pass'];
$i_get_pass = addslashes($i_get_pass);
$i_pdf_file_url = 'report.php?sid='.$i_get_sid.'&a='.urlencode($i_get_pass).'&b=/report.pdf';
echo '<table cellpadding=0 cellspacing=0 border=0 width="100%">';
echo '<tr vAlign=top><td height=7><img src="images/1x1.gif" width=1 height=7></td></tr>';
echo '<tr height=28 style="background: url(images/bookm-bg.gif) repeat-x"><td width="100%"><nobr>';
echo '<img src="images/1x1.gif" width=5 height=1><img src="images/bookm-42.gif" width=67 height=28 border=0><img src="images/bookm-s1.gif" width=10 height=28 border=0><img src="images/bookm-51.gif" width=65 height=28 border=0> <img src="images/button-downloadpdf.gif" width=80 height=28 border=0> <img src="images/button-emailpdf.gif" width=80 height=28 border=0>';
echo '</nobr></td><td><nobr><font style="font-size: 10px;">Close Window </font></nobr></td></tr></table>';
(This is what I added as a workaround)echo '<p><a href="'.$i_pdf_file_url.'" name="plugin" width=100% height=100% fullscreen=yes style="position: absolute;">Click Here to open the PDF</p>';
(This is what should display the PDF in the browser but wont' work)
echo '<p><embed type="application/pdf" src="'.$i_pdf_file_url.'" name="plugin" width=100% height=100% fullscreen=yes style="position: absolute;"></p>';
require_once($DOCUMENT_ROOT."inc/btm-2.inc.php");
?>
From documentation:
Destination where to send the document. It can be one of the following:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
The default value is I.
If you want to force download with given name, create a link that points to your pdf script and use this:
$pdf->Output("__name__","F");
If you want to display pdf for preview use this:
$pdf->Output("__name__","I");
and in your html inside PDF tab use iframe to embed pdf:
<iframe src="pdf_preview.php" frameborder="0"></iframe>
There are other ways to do this, but this should be the easest.

How to display Binary image in mpdf

I am saving image as binary to database. I want to display this image in MPDF. Normal image display is working properly but binary image can not displaying The code I have used is
<td><img style="vertical-align: top" src="'.$studphoto.'" width="80" /></td>
The code I have used while inserting to database is
$photo_get = file_get_contents($_FILES["photo"]["tmp_name"]);
list($width, $height, $image_type) = getimagesize($_FILES["photo"]["tmp_name"]); /* Here gets image type */
$mime_photo = image_type_to_mime_type($image_type); /* This function returns the mime type of the file when the image type got from getimagesize() is passed */
$photo_en = base64_encode($photo_get);
$photo = "data:" . $mime_photo . ";base64," . $photo_en;
The data getting from DB is data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAGUASgMBIgACEQEDEQH/xAAcAAABBQEBA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YdxLdgX1S4uDAe716zt8iNu+fyXp96oNR1e4veJGCRZztHT61DeMluAcelQLq0nvJFjJMcPiM8tQztc+jfQfY3w1GH/1CPKXsn2KC61m0lR0MAym4c9f/BWjWcC7QFYECsz0uGSzEYjxhByp/rRVo2sd5cvbv8A2Z8sUPZDmung1yYSi1J/l/oWbo4wSxUADqTiqDV9XjCtFbsHY8ZXwqj1W7e+nIZm7lThUz19arXs9s4kikdXxggHg/Oso1ryEVfH61Kb36JO0Dp1NOAwPWmgsv5lB9VrnL3kilVG0HqSea0Ga6LSEU78t4N0+VLsroFwOOgr2a4nihi8rzTHGHX516vVBD7HSRARnxpkYIm7xTglAPua9XqlNorZFSS3+yVtCcDw4zSUleqSyE8aXGTSV6oLC03Apa9UM4//Z
Please somebody help me to solve this :-(
Try like
<img src="<?php echo $studphoto;?>" />
And try to add the header with
header('Content-Type: image/jpeg;');

how to show blob image in html?

I have a image stored in mysql as mediumblob and now I want to show it in html page, so I am doing this with it:
$c = base64_encode($resu[0]->image);
$image = '<img src="data:image/jpeg;base64,'.$c.'" />';
echo $image;
But I am getting only half of original image, so am I missing something here ?
Just an idea, may be you can seperate the logic to display the image.
What I mean is that you can create a file like image.php that accepts the id or filename
of the image and then display the image. Then you can simply refer the image in your HTML
by, for example, doing something like this:
<img src="image.php?imgId=12547"/>
in image.php file something like the following
$imgId=isset(GET['imgId'])?GET['imgId']:0;
$sql = "SELECT * FROM theBlogs WHERE ID = $imgId;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $row['imageContent'];
Just perform a quick test with strlen on $resu[0]->image variable and check blob size in DB and check if they are different sizes for sure.
Display in li tag means use this
<li data-thumb='<?php echo "data:image/jpeg;base64,".base64_encode($img1 ); ?>'>
<div class="thumb-image">
<img <?php echo 'src="data:image/jpeg;base64,'.base64_encode( $img1 ).'"';?>
data-imagezoom="true" class="img-responsive" alt="">
</div>
</li>

to echo a <img > tag and send the code through email+ php

I have a php variable which contain the whole source code of image.What I want is to echo the tag with its attributes src,height & width and send the source code of image through mail (i.e ).
$imagename = abc.jpg;
$concatpath = SITE_URL."/uploads/affiliatesAdv/".$imagename;
$imagesrc = '<img src="'.$concatpath.'" height="200px" width="200px">';
Now when I echo $imagesrc it displays the image. How can I show the source code instead?
Try this,
echo "<pre>";
echo $imagesrc;
echo "</pre>";
You need to use html encoding.
Some thing like this
<img src="abc.jpg"/>
to
<img src="abc.jpg"/>
See the reference.
You can try this site to do it online..

Send image from client to database using sql

I'm trying to display an image that is stored in my database. I know it is not best practice to store an image in the database, but just for this purpose its what I need. My problem is when I go to display the image, all I get is a load of scrambled code, which I guess is the image in code but not visually what I want. When I use the header tag to identify the image all I get is a thumbnail to mean it isn't reading it in right. Any help would be great.
Below is code I'm using. I've tried it in both ways using it to display in either the php or in html but can't display the image:
while($row = mysql_fetch_array($result))
{
$Long = $row['Long'];
$Lat = $row['Lat'];
$img = $row['file'];
echo "<b><center>Database Output</b><br><br>";
echo "<td>" .$row['file']."</td>";
echo "----";
echo "<td>" .$row['Lat']."</td></center>";
//echo "<td>" .$row['file']."</td>";
}
?>
<div id="map">
<img src="<? $img?>" alt="">
Thanks for any input
Either you embed the image into your html page as a data URI
<img src="data:image/jpeg;base64,<?php echo base64_encode($img) ?>" />
which is hideously NASTILY horrible for 'large' files - you make it impossible for the browser to cache the image, forcing the user download the base64-encoded data EVERY time they load the page.
or you have a sub-script to serve up the actual image, and have something more like:
<img src="getimage.jpg?id=XXX">
and
<?php
$data = get_image_from_db($_GET['id']);
header('Content-type: image/jpeg');
echo $data;

Categories