When resizing an image with PHP, the page goes blank - php

I'm fairly new to PHP. I have a function written to scale an image and save a thumbnail. The function is working (thumbnails being created) but any time it runs the the page loads a blank page with only an empty image tag - I can't view the source of the page (because there isn't one?).
If the function doesn't run the page loads fine.
function scaleImage($id)
{
header("Content-Type: image/jpeg");
$si = imagecreatefromjpeg("img/dribbble/standard_resolution/{$id}.jpg");
$si_x = imagesx($si);
$si_y = imagesy($si);
$di_x = 210;
$di_y = 158;
$di = imagecreatetruecolor($di_x, $di_y);
imagecopyresampled($di, $si, 0, 0, 0, 0, $di_x,
$di_y, $si_x, $si_y);
imagejpeg($di,"img/dribbble/low_resolution/{$id}.jpg",90);
};
Why is the page blank when the function runs and how can I fix this?

Try outputting image without $filename argument: imagejpeg($di, null, 90);

Related

How can I scroll page in php-webdriver?

I'm trying to get some screenshot using php-driver. And it appears that despite taking the picture of the entire web page, it just taking the picture that appears on the monitor/screen (that's why we call it screenshot).
So my question is how to capture a picture that located in the bottom of the page? Do we scroll the page to the specified element? or there is a way to take the picture of the entire page?
This is my screenshot code:
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::firefox();
$webdriver = RemoteWebDriver::create($host, $capabilities, 5000);
function find_image($url) {
//Screenshot
$GLOBALS["webdriver"]->get($url);
$element = $GLOBALS["webdriver"]->findElement(WebDriverBy::cssSelector('#law > p > img'));
$element_width = $element->getSize()->getWidth();
$element_height = $element->getSize()->getHeight();
$element_x = $element->getLocation()->getX();
$element_y = $element->getLocation()->getY();
$screenshot = __DIR__ . "/number/" . count($GLOBALS["data"]) . ".png";
$GLOBALS["webdriver"]->takeScreenshot($screenshot);
$src = imagecreatefrompng($screenshot);
$dest = imagecreatetruecolor($element_width, $element_height);
imagecopy($dest, $src, 0, 0, $element_x, $element_y, $element_width, $element_height);
imagepng($dest, $screenshot);
return convert_image($screenshot);
}
As you say, you can't take a screenshot of anything more than is on the current screen. But you can scroll the window to the bottom of the page.
$this->webDriver->executeScript('window.scrollTo(0,document.body.scrollHeight);');
$this->webDriver->takeScreenshot("a.png");
I use an instance variable for the webDriver, using GLOBALS isn't a particularly good way of using it (IMHO).
You can pass it in as a variable, or use it as a parameter to a constructor if using a class.

Create QR Codes by Function or Class

I'm using QRCode from Google API and I put this code in the function.
Now I want to show two images, for example: Two images with different sizes or datas!
It does not matter if you use class or function, I just want to get different output on the page.
This is code:
<?php
function CreateQRCode($data, $size, $logo) {
header('Content-type: image/png');
// Get QR Code image from Google Chart API
// http://code.google.com/apis/chart/infographics/docs/qr_codes.html
$QR = imagecreatefrompng('https://chart.googleapis.com/chart?cht=qr&chld=H|1&chs='.$size.'&chl='.urlencode($data));
if($logo !== FALSE){
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width/3;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
imagecopyresampled($QR, $logo, $QR_width/3, $QR_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
}
imagepng($QR);
imagedestroy($QR);
}
CreateQRCode('http://google.com', '200x200', FALSE);
?>
Like this:
example 2
example 1

FPDF not writing on all pages

Hi I'm using FPDF and FPDI, I'm using FPDI to concatenate several PDFs then using FPDF to fill in the information based on a form that is filled out, I've setup a SetPage method within FPDF to be able to set the page on which I'm working on, I'm able to write on the first file completely fine (first 3 pages). However, when I'm trying to write on the second file (4th and continuing pages), I use the SetXY and Write but nothing is written, I am able to add an image (barcode at the bottom of the page) but no text, any ideas as to what I'm doing wrong?
This is the code that I've got to concatenate the files:
<?php
session_start();
require_once('lib/pdf/fpdf.php');
require_once('lib/pdi/fpdi.php');
require_once('lib/barcode/class/BCGFontFile.php');
require_once('lib/barcode/class/BCGColor.php');
require_once('lib/barcode/class/BCGDrawing.php');
require_once('lib/barcode/class/BCGcode39extended.barcode.php');
$contractType = $_SESSION['addition'];
require_once('barcode.php');
if(isset($contractType))
{
$files = array('lib/blank/NDA.pdf');
if($contractType = 'artist')
{
array_push ($files,
'lib/blank/Distro.pdf',
'lib/blank/Management-Trial.pdf'
);
} else {
echo "Whoops! Something must've happened when you were filling out your contracts! Please try filling them out again. Sorry!";
}
}
$pdf = new FPDI();
foreach ($files AS $file) {
$pageCount = $pdf->setSourceFile($file);
for($n = 1; $n <= $pageCount; $n++) {
$tmpIdx = $pdf->importPage($n);
$size = $pdf->getTemplateSize($tmpIdx);
if($size['w'] > $size['h']) {
$pdf->AddPage('L', array($size['w'], $size['h']));
} else {
$pdf->AddPage('P', array($size['w'], $size['h']));
}
$pdf->useTemplate($tmpIdx);
}
}
//NDA FILLER
include('lib/filler/NDA.php');
//Distro Contract Filler
include('lib/filler/Distro.php');
//session_unset();
$pdf->Output();
?>
This is the code for filling out the first PDF (which works completely fine):
NDA.php
<?php
//ID No.
$idcoded = 'idbars/'.$_SESSION['name'].'.png';
/*
for($p = 2; $p <= $pages; $p++)
{
$pdf->Image($idcoded,0,350);
$pdf->setPage($p);
}
*/
$pdf->SetPage(1);
$pdf->Image($idcoded,0,350);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
//NDA DATE
$pdf->SetXY(51, 109.5);
$pdf->Write(0, date(d));
$pdf->SetXY(72, 109.5);
$pdf->Write(0, date(F));
//Legal Name
$pdf->SetXY(72, 114.5);
$pdf->Write(0, $_SESSION['name']);
//stage Name
$pdf->SetXY(80, 119.5);
$pdf->Write(0, $_SESSION['sname']);
$pdf->setPage(2);
$pdf->Image($idcoded,0,350);
$pdf->setPage(3);
$pdf->Image($idcoded,0,350);
$signature = 'idbars/'.$_SESSION['name'].'_sig.png';
$pdf->Image($signature,20,105,100);
?>
This is what I'm using to try to write on the second PDF, I've tried combining the NDA.php and Distro.php into one file and that makes no difference
Distro.php
<?php
$pdf->SetPage(4);
$pdf->SetXY(10,10);
$pdf->Cell(0, $_SESSION['name']);
$pdf->Write(0, $_SESSION['name']);
$pdf->Image($idcoded,0,350);
?>
The page that this is building works off of this form:
https://secure.gr8label.com/sign/artist/Dev%20Test/
FPDF "caches" the font information that is currently used. As you jump back to another page FPDF "thinks" that the font is already defined/set but in the PDF file itself it isn't. You should set your font and size in your import loop, to ensure that the font is available on all pages (I think it also could work, by defining it only on the first one).
Anyhow you should have seen that jumping between written pages results in problems and you should create a logic which creates the file from top to bottom without using things like "SetPage()" at all.

How do I print a barcode using barcode generator for PHP onto a pdf formatted page where I want it?

Alright so first things first:
I've searched over this site for 6+ hours and I keep coming up with the same results. The main answer I keep getting is: How to Generate Barcode using PHP and Display it as an Image on the same page
But this is not working for me. Even the answer on that page that was accepted ends with "After you have added all the codes, you will get this way:" which is so vague I feel like I'm supposed to already be an expert to understand it. I'm getting frustrated with this problem because I cannot seem to find any "moron directions" that can help me understand how everything works in this library for barcode generator for php.
Here is what I have:
I'm using fpdf to print a pdf file which works great!
Page Name: PrintMyPDF.php
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
$thisorderID = $_GET['Xort'];
require ('UFunctions.php');
if (trim($thisorderID) == ""){
$value = '0';
}
if (!is_digit($thisorderID) || $thisorderID < 0)
{
header('Location:ErrorInt.php');
exit;
}
//Database connection established
require_once('DBASEConnector.php');
$sql2 = "SELECT users.name, users.storenum, users.storename, Orders.OrderID, Orders.name
FROM users, Orders
WHERE Orders.name = users.name
AND Orders.OrderID = '$thisorderID'";
$result = $db->query($sql2);
$row = $result->fetch_assoc();
$ThisStoreNum = $row['storenum'];
$ThisStoreName = $row['storename'];
require('fpdf.php');
$pdf = new FPDF();
//$fpdf->SetMargins(0, 0, 0);
//$fpdf->SetAutoPageBreak(true, 0);
$pdf->SetAuthor('Walter Ballsbig');
$pdf->SetTitle('Order Form');
$pdf->SetFont('Helvetica','B',16);
$pdf->SetTextColor(0,0,0);
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');
$pdf->SetXY(50,20);
$pdf->SetDrawColor(0,0,0);
$pdf->Cell(100,10,'Order Form',1,1,'C',0);
$pdf->SetFontSize(10);
$pdf->SetX(50);
$pdf->Cell(100,10, 'Order: '.$thisorderID.' | Store: '.$ThisStoreNum.'-'.$ThisStoreName,1,1,'C',0);
$pdf->SetXY(10,50);
$pdf->SetFontSize(12);
$pdf->Cell(6,6,'X',1,0,'C',0);
$pdf->Cell(14,6,'QTY',1,0,'C',0);
$pdf->Cell(130,6, 'ITEM',1,0,'C',0);
$pdf->Cell(30,6, 'UNIT',1,1,'C',0);
$query = "SELECT Inventory.ProductI, Inventory.ProductName, Inventory.CurrentQty, Inventory.Pull, Inventory.Unit, OrderItems.ProductI, OrderItems.QtyO, OrderItems.OrderI
FROM Inventory, OrderItems
WHERE OrderItems.OrderI = '$thisorderID'
AND OrderItems.ProductI = Inventory.ProductI
ORDER BY Inventory.Pull, Inventory.ProductName";
$result = $db->query($query);
$num_results = $result->num_rows;
for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
$pdf->SetFontSize(12);
IF ($row['CurrentQty'] <=0)
{
$pdf->SetFontSize(10);
$pdf->Cell(6,6,'BO',1,0,'C',0);
$pdf->SetFontSize(12);
}else{
$pdf->Cell(6,6,' ',1,0,'C',0);
}
$pdf->Cell(14,6, $row['QtyO'],1,0,'C',0);
$pdf->Cell(130,6, $row['ProductName'],1,0,'L',0);
$pdf->Cell(30,6, $row['Unit'],1,1,'C',0);
}
$pdf->Output();
$db->close();
?>
This prints up my pdf beautifully! Now I wanted to add a barcode on the page that will represent the order number for scanning purposes.
Now here is what I have for my code that contains the barcode... code.
Name of barcode page: BarCodeIt.php
<?php
function BarCodeIt($MyID) {
// Including all required classes
require_once('./class/BCGFontFile.php');
require_once('./class/BCGColor.php');
require_once('./class/BCGDrawing.php');
// Including the barcode technology
require_once('./class/BCGcode39.barcode.php');
// Loading Font
$font = new BCGFontFile('./font/Arial.ttf', 18);
// Don't forget to sanitize user inputs
$text = isset($_GET['text']) ? $_GET['text'] : $MyID;
// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);
$drawException = null;
try {
$code = new BCGcode39();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$code->parse($text); // Text
} catch(Exception $exception) {
$drawException = $exception;
}
/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}
//Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
header('Content-Disposition: inline; filename="barcode.png"');
// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
}
?>
Now in my PDF file just before this line:
$pdf->Output();
I have added this:
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');
require('/BarCodeIt.php');
$MyBarCode = BarCodeIt($thisorderID);
echo $MyBarCode;
But what it does is all of my other pdf elements disappear and I'm left with only a big barcode (the right one! that part works) but that's all that is on the screen. It's like when the barcode section runs it negates everything else and just prints the barcode. I want to print just the barcode where I want it on the PDF but I'm not clever enough to figure out what I'm doing wrong. Any help on this would be greatly appreciated.
In $pdf->SetDisplayMode(real,'default');, real is not an identifier. I believe you've forgotten the $ prefix.
Have you warnings reporting at maximum level? If not, include:
error_reporting(E_ALL);
in your script and see that it shows additional issues.
I'm not familiar with fpdf, but what you are doing seems wrong just by looking at it: Everywhere you add elements to your pdf by using methods on your $pdf object like $pdf->... and when you want to add the barcode, you echo it out directly.
Don't echo your image out. Instead get rid of the header() calls in your barcode script, save your image and look for the right method to add an image to the $pdf object.
Here is a question with answers that deals with adding an image: Inserting an image with PHP and FPDF

Taking screen shot of webpage in php

I am using following function to take screenshot of a webpage.
function my2()
{
$Browser = new COM('InternetExplorer.Application');
$Browserhandle = $Browser->HWND;
$Browser->Visible = true;
$Browser->Fullscreen = true;
$Browser->Navigate('http://www.tatvic.com');
while ($Browser->Busy)
{
com_message_pump(4000);
}
$img = imagegrabwindow($Browserhandle, 0);
$Browser->Quit();
imagepng($img, 'screenshot.png');
}
This works fine. But as it is a screen shot, it is not taking the whole page. I mean it is not taking the parts of page which we can see by scrolling .
What i can do so the script first zoom out the page to 25% or 35% or converting it to a4 size and then take the screen shot so that image of whole page can be stored ??? .
Thank you.
Take a look at PhantomJS. It is a headlesss WebKit API with JavaScript support. This means, you do not need a GUI/Browser to view.
It allows you to grab the full page, save to PNG, SVG, etc.

Categories