<?php
$dir_pdf = "http://example.com/test/uploads/test_file.pdf";
$dir_thumbnail = "http://example.com/test/uploads/test_file.jpg"
$im = new Imagick();
$im->readImage($dir_pdf);
header("Content-Type: image/" . $im->getImageFormat());
echo $im;
?>
In folder "uploads" i have a pdf file
My server installed Imagick & Ghostscript
Why this code can't run :)
Thank you .
<?php
$im = new Imagick("uploads/test_file.pdf[0]");
$im->setImageFormat("jpg");
$type=$im->getFormat();
header("Content-type: $type");
echo $im->getimageblob();
?>
this is my code :)
I put test_file.pdf in folder uploads
it work ^^!
Related
I'm using "pdfcrowd HTML to PDF API for PHP" to generate PDF files from the PHP and smarty file. I'm able to generate the PDF but not able to put the background watermark image to the PDF I'm printing out. I tried everything but still couldn't put the background watermark image. Can anyone please guide me regarding this? Where I'm going wrong? Any kind of help would be greatly appreciated. Following is my code for your reference:
<?php
set_time_limit(0);
require_once("../../includes/application-header.php");
require_once OCN_ROOT."/core/extention/pdfcrowd/pdfcrowd_config.php";
require_once OCN_ROOT."/core/extention/pdfcrowd/pdfcrowd.php";
try
{
// create an API client instance
$client = new Pdfcrowd($username, $apikey);
$objPracticeSheet = new PracticeSheet();
$practice_sheet_id = $_GET['practice_sheet_id'];
$bread_crumbs_text = 'View Practice Sheet';
$practice_details = $objPracticeSheet->ViewPracticeSheet($practice_sheet_id);
$practice_sheet_details = $objPracticeSheet->GetPracticeSheetDetailsById($practice_sheet_id);
$smarty->assign('bread_crumbs_text', $bread_crumbs_text);
$smarty->assign('practice_sheet_name', $practice_sheet_details['practice_sheet_name']);
$smarty->assign("practice_details", $practice_details);
$smarty->assign("practice_sheet_questions", $practice_sheet_details['practice_sheet_total_questions']);
$smarty->assign("practice_sheet_display_date", $practice_sheet_details['practice_sheet_display_date']);
$smarty->assign("practice_sheet_for", $practice_sheet_details['practice_sheet_for']);
$file_to_show = "pdf-practice-sheet.tpl";
$final_data = $smarty->fetch($file_to_show);
$file = fopen(ADMIN_ROOT."temp/pdf_practice_sheet.html", "w+");
file_put_contents($file, "");
fwrite($file, $final_data);
fclose($file);
$client->setHeaderHtml($header_html);
$client->setFooterHtml("<table border='0' width='100%' cellpadding='5' cellspacing='0' style='font-size:10px;font-family:verdana;background:#EFBC8F;margin-top:10px;'><tr><td align='left'>Powered by EntrancePrime.com :
India's Favourite Online Test Series for JEE, NEET, & CA-CPT</td><td align='right'>Page %p/%n</td></tr></table>");
$client->setPageWidth("210mm");
$client->setPageHeight("298mm");
$client->setNoCopy('True');
//$client->setVerticalMargin("0.8in");
$client->setPageMargins("1.1in","0.2in","0.4in","0.2in");
//Set watermark
$client->setWatermarkInBackground(True);
$client->setWatermark("'".ADMIN_SITE_URL."assets/img/watermark.png'", -28, 406);
$pdf = $client->convertFile(ADMIN_ROOT."temp/pdf_practice_sheet.html");
$filename = $practice_sheet_details['practice_sheet_name'];
// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"" . $filename . ".pdf\"");
// send the generated PDF
echo $pdf;
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}
?>
Looking forward to the reply.
Try using an absolute URL(http://www.yoursite.com/image.png) in this $client->setWatermark("'".ADMIN_SITE_URL."assets/img/watermark.png'", -28, 406);
And see what you get. It looks like a path issue to me mate. Good luck.
I'm working on a PHP app that converts a self portrait into a pencil drawing and part of the app requires a desaturated/inverted image similar to the example below:
before and after invert:
It's easy creating a desaturated image, but how would I achieve this invert using imagemagick? I tried using negate, but failed miserably.
Here's what I have so far:
<?php
$img = new Imagick();
$img->readImage($image);
$img->modulateImage(100, 0, 100);
$img->writeImage('image.png');
ob_clean();
header('Content-Type: image/png');
echo $img;
$img->destroy();
?>
According this page, you can call imagemagick filter by PHP functions :
function negateImage($imagePath, $grayOnly, $channel) {
$imagick = new Imagick(realpath($imagePath));
$imagick->negateImage($grayOnly, $channel);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
}
Here's a jpg: http://i.stack.imgur.com/PIFN0.jpg
Let's say I'd like this rendered from /img.php?file_name=PIFN0.jpg
Here's how I'm trying to make this work:
/sample.php
<p>Here's my image:</p>
<img src="/img.php?file_name=PIFN0.jpg">
/img.php
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
imagejpeg($url);
?>
I would expect /sample.php to show the image. But this doesn't work. All I get is a broken image. What am I doing wrong?
Use imagecreatefromjpeg:
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
imagejpeg(imagecreatefromjpeg($url));
?>
Reference: http://php.net/manual/en/function.imagecreatefromjpeg.php
Here is an working example:
<?php
function img_create($filename, $mime_type)
{
$content = file_get_contents($filename);
$base64 = base64_encode($content);
return ('data:' . $mime_type . ';base64,' . $base64);
}
?>
<img src="<?php print img_create('http://tuxpaint.org/stamps/stamps/animals/birds/cartoon/tux.png','image/png'); ?>" alt="random logo" />
No need to use the GD functions:
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
readfile($url);
?>
<?php
header("Content-Type: image/jpeg");
$url = "http://i.stack.imgur.com/PIFN0.jpg";
$imgContents = file_get_contents($url);
$image = #imagecreatefromstring($imgContents);
imagejpeg($image);
?>
header('Content-type: image/jpeg');
readfile($_GET['id']);
but in the new server, only can read the local images, the remote images is error.
I am trying to use header('Content-type: image/jpeg') to edit and display jpeg photos as follows.
header('Content-type: image/jpeg')
$filename = 'aaa.jpg';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_CONTRAST,50);
imagejpeg($im);
imagedestroy($im);
In the same file,I also have other simple codes like $abc = $_POST['abc'].
After I put the header, code before the header and code after image destroy($im) no longer work. And when I put any code such as $_post['abc'] before the header, both header and code doesn't work. All codes were fine before I included header and code to manipulate and output image. It is my first time using header('Content-type: image/jpeg') and I cannot find the answer after trying for so long. Please help. Thank you.
If you want to output html page, do not send image header. But instead, at first output the transformed image to a file on your server and add the <img> or <a>nchor tag in your html page:
<html><body>
<?php
$output_dir = 'images';
if (!file_exists($output_dir)) {
mkdir($output_dir, 0777);
}
$filename = 'aaa.jpg';
$filename2 = 'aaa2.jpg';
if (!file_exists($filename)) {
echo 'Input image not exists!'; exit;
}
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_CONTRAST, 50);
imagejpeg($im, $output_dir.'/'.$filename2);
imagedestroy($im);
echo 'Original image:<br/><img src="'.$filename.'" /><br/>';
echo 'Transformed image:<br/><img src="'.$output_dir.'/'.$filename2.'" />';
?>
</body></html>
That image header send in case, you want to output it as standalone image. For more examples have a look at php.net.
if (file_exists($path)) {
$fileContent = id3_getImage($path);
$fileMime = id3_getMimeOfImage($path); // is set to image/jpeg
if ($fileMime != "") {
header('Content-Type: '.$fileMime);
print($fileContent);
die;
}
}
So the above code does not work in the browser, however when I make a image with image
$img = imagecreatefromstring($fileContent);
imagejpeg($img, 'test.jpg');die;
the image is created and I can view it on my computer. So I have no clue what I am doing wrong :(
*Could it be a setting in the apache conf? I honestly have no clue on this one
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($img);
You need send header before. But if you want to create the image and show, you need create, and read this for the browser.
readfile($filename);
You can read the man here:
http://php.net/manual/en/function.readfile.php
I use smartReadFile.php from Google Groups to display any type of file.
https://jplayer.googlegroups.com/attach/f308294ddea52f6c/smartReadFile.php?view=1&part=4
It's really smart. =)
<?php
require "smartReadFile.php";
$dir = 'images/';
$filename = 'someimage' . '.jpg';
$location = $dir.$filename;
smartReadFile($location, $filename);
?>
This one is working for me when reading from DB
$image = imagecreatefromstring([path/DBfiels]);
header('content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);