Good day!
Could anyone help me, there is a system where users do register via their desktop in a database hosted on the web, we are now developing the web interface of this system, then it has a certain functionality in the system where I have to display the photo user.
I do what normal SELECT in SQL Server, but upon the imagejpeg ($ img); it does not show the whole picture, just a piece of the picture. Could anyone help me? I'm looking for some tutorials on the web and they speak it is because of the size of the field. If the field is of type (image) and the return is in hexadecimal.
Below I tried to do a function with the help of a friend, but she also did not work:
<php
$id = (int)$_GET['id'];
$qryimg = mssql_query(gimage SELECT FROM user WHERE id = {$ id});
$resimg = mssql_fetch_array($qryimg);
$im1 = $resimg['gimage'];
header("Content-type: image/jpg");
$image='';
for($i=2; $i<strlen($im1); $i+=2)
{
$hex = $im1{$i} . $im1{($i + 1)};
$cod = hexdec( $hex );
$image .= chr( $cod );
}
echo $image;
#echo imagejpeg($image);
?>
Why doesn't the following code work? Can't you just echo the image.
<php
$id = (int)$_GET['id'];
$qryimg = mssql_query(gimage SELECT FROM user WHERE id = {$ id});
$resimg = mssql_fetch_array($qryimg);
$im1 = $resimg['gimage'];
header("Content-type: image/jpg");
print $im1;
exit;
What field type is gimage?
Related
I created a php page that print the barcode. Just to view it before i print it on an A4. Still in testing phase. The codes are as below.
<?php
include('include/conn.php');
include('include/Barcode39.php');
$sql="select * from barcode where b_status = 'NOT-PRINTED'";
$result=mysqli_query($conn,$sql);
echo mysqli_num_rows($result);
$i=0;
while($row=mysqli_fetch_assoc($result)){
$acc_no = $row["b_acc_no_code"];
$bc = new Barcode39($row["b_acc_no_code"]);
echo $bc->draw();
$bc->draw($acc_no.$i.".jpg");
echo '<br /><br />';
$i++;
}
?>
Without the while loop, it can be printed, but only one barcode. How to make it generate, for example in the database have 5 values, it will print 5 barcode in the same page. Thanks in advance
Try to use another bar code source. Because It is generate only one bar code per page. Can't able to create multiple bar code per page.
I know this is an older post but comes up in searches so is probably worth replying to.
I have successfully used the Barcode39 to display multiple barcodes. The trick is to get base64 data from the class and then display the barcodes in separate HTML tags.
The quickest way to do this is to add a $base64 parameter to the draw() method:
public function draw($filename = null, $base64 = false) {
Then, near the end of the draw() method, modify to buffer the imagegif() call and return the output in base64:
// check if writing image
if ($filename) {
imagegif($img, $filename);
}
// NEW: Return base 64 for the barcode image
else if ($base64) {
ob_start();
imagegif($img);
$image_data = ob_get_clean();
imagedestroy($img);
return base64_encode($image_data);
}
// display image
else {
header("Content-type: image/gif");
imagegif($img);
}
Finally, to display multiples from the calling procedure, construct the image HTML in the loop and display:
// assuming everything else has been set up, end with this...
$base64 = $barcode->draw('', true); // Note the second param is set for base64
$html = '';
for ($i = 0; $i < $numBarcodes; $i++) {
$html .= '<img src="data:image/gif;base64,'.$base64.'">';
}
die('<html><body>' . $html . '</body></html>');
I hope this helps anyone else facing this challenge.
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
i am trying to get a Minecraft player's skin via PHP, and it all works, until the user enteres an invalid username, techniclly, if that happenes the script should replace the not found image with an alternative image, but for some reason it doesn't.
full script:
<?php
//initial settings
header("Content-type: image/png");
//declere values
$name = $_GET['n'];
//get the image from Minecraft main servers
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/{$name}.png");
//if not found, use an alternative image
if(!$src){
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/char.png");
}
//display the skin
imagepng($src);
?>
Any help will be appriciated, thank you.
try
<?php
//initial settings
header("Content-type: image/png");
//declere values
$name = $_GET['n'];
//get the image from Minecraft main servers
if(file_exists("http://s3.amazonaws.com/MinecraftSkins/{$name}.png")) {
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/{$name}.png");
//if not found, use an alternative image
else {
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/char.png");
}
//display the skin
imagepng($src);
?>
I am refactoring some old code, including rewriting basic mysql queries to use PDO.
The following works brilliantly in all browsers and for all image types:
$query = 'SELECT image FROM image WHERE imageid=' . $image_id;
$result = mysql_query($query, $db_conn); querycheck($result);
header("Content-type: image");
echo mysql_result($result, 0);
Unfortunately, however I rewrite it using PDO, it doesn't work. I've been through the entire PDO documentation and the standard web search, but none of the advice/solutions work.
How can one easily fetch and image from MySQL using PDO and display it?
Edit 1:
Matthew Ratzloff gives what should be the obvious answer below, but it does not work.
Here is the actual code that I test using PDO (and I have tried many variants/parameters):
$connectstring_temp = 'mysql:host=' . $A . ';dbname=' .$B;
$dbh_temp = new PDO($connectstring_temp, $login, $password);
#$dbh_temp->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
#$dbh_temp->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
$sql = "SELECT image FROM image WHERE imageid=" . $image_id;
$query = $dbh_temp->prepare($sql);
$query->execute();
$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image");
echo $image;
I've kept the same syntax, although for the final code $image_id needs to be passed as a parameter. The code above does NOT work. PDO works fine for all other queries of all types.
You need to paramaterize the imageid value and bind the parameter to PDO::PARAM_LOB:
$sql = "SELECT image FROM image WHERE imageid=:id";
$query = $db_conn->prepare($sql);
$query->execute(array(':id' => $image_id));
$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image");
echo $image;
Of course, you'll also want to specify the complete, correct content type (e.g., image/png).
My personal approach to Image Blobs is using a php file to serve the image, in combination with a GET argument... It's 100% effective and it doesn't involve file creation... For instance, the file to serve the image from the blob would be:
image.php:
<?php
$db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
if (isset($_GET['imageid'])) {
$result = $db->prepare("SELECT image FROM image where imageid = ?");
if ($result->execute(array($_GET['imageid']))) {
$row=$result->fetch();
echo $row['pic']; //this prints the image data, transforming the image.php to an image
}
} // you can put an "else" here to do something on error...
?>
This can be called from you main script... For instance:
<?php
$db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
//... some code to do your job, where you get your imageid from
$imageid=...
?>
<img src="./image.php?imageid=<?php echo $imageid;?>">
You can use this code to get image from database using PDO:
public function getImage($id){
$sql = "SELECT * FROM images WHERE id = ?";
$sth = $this->dbh->prepare($sql);
$sth->bindParam(1,$id);
$sth->execute();
$num = $sth->rowCount();
if( $num ){
$row = $sth->fetch(PDO::FETCH_ASSOC);
header("Content-type: ".$row['type']);
print $row['image'];
exit;
}else{
return null;
}
}
type - data type(column name) such as image/png or image/gif
image - image data(column name) stored in table as LOB
$this->dbh connection handler
It works for me but now I need to find out how to use it with JS because result of this function is passed to JavaScript code - so called ajax
This code displays all the images in the dabase so you can change it and make it do what you want it to do
getMessage();
}
?>
<?php
#the folder where the images are saved
$target = "image_uploads/";
$image_name = (isset($_POST['image_name']));
$query = ("SELECT user_id ,image_name FROM tish_images");
$image_show= $con-> prepare($query);
$image_show->execute();
while($record =$image_show->fetch(PDO::FETCH_ASSOC)) {
#this is the Tannery operator to replace a pic when an id do not have one
$photo = ($record['image_name']== null)? "me.png":$record['image_name'];
#display image
echo '<img src="'.$target.$photo.'">';
echo $record['user_id'];
}
?>
I'm trying to develop a captcha class for my website everything was doing fine until I tried to embed the image generated with PHP GD inside my subscription form!
Here is the code of my class:
<?php
//captcha.php
header("Content-type: image/png");
class Captcha {
// some attributes bla bla
public function __construct($new_string_length,$new_width_picture,
$new_height_picture,$new_string_color) {
$this->string_length = $new_string_length;
$this->width_picture = $new_width_picture;
$this->height_picture = $new_height_picture;
$this->string_color = $new_string_color;
}
public function getString() {
return $this->string;
}
public function generateRandomString() {
$str = "";
$basket = "abcdefghijklmnopqrstuvwxyz0123456789";
$basket_length = strlen($basket);
srand ((double) microtime() * 1000000);
for($i=0;$i<$this->string_length;$i++) {
$generated_pos = rand(0,$basket_length);
$str_substr = substr($basket,$generated_pos-1,1);
if(!is_numeric($str_substr)) {
// if the character picked up isn't numeric
if(rand(0,1)==1) {
// we randomly upper the character
$str_substr = strtoupper($str_substr);
}
}
$str = $str.$str_substr;
}
$this->string = $str;
}
**public function generatePictureFromString($new_string) {
$root_fonts = '../fonts/';
srand ((double) microtime() * 1000000);
$list_fonts = array('ABODE.ttf','acme.ttf','Alcohole.ttf',
'Anarchistica.ttf','AMERIKAA.ttf');
$image = #imagecreatetruecolor($this->width_picture,$this->height_picture);
$noir = imagecolorallocate($image,0,0,0);
$clr = explode('/',$this->string_color);
$clr = imagecolorallocate($image,$clr[0],$clr[1],$clr[2]);
for($i=0;$i<strlen($new_string);$i++) {
imagettftext($image,rand(($this->height_picture/4.3),($this->height_picture)/4.2),
rand(-45,45),($this->width_picture)/(5*$this->string_length)+($this->width_picture)/($this->string_length)*$i,0.6*($this->height_picture),$clr,
$root_fonts.$list_fonts[rand(0,count($list_fonts)-1)],substr($new_string,$i,1));
}
imagepng($image);
imagedestroy($image);
}**
}
I willingly avoided to show some useless part of the class. The class itself works perfectly when I call the generatePictureFromString(..) method like this:
<?php
//testeur_classe.php
require_once '../classes/captcha.php';
$captcha = new Captcha(5,200,80,"255/255/255");
$captcha->generateRandomString();
$str = $captcha->getString();
$captcha->generatePictureFromString($str);
?>
But when I try to insert the picture generated in my form using:
<img src="<?php echo PATH_ROOT.'classes\testeur_classe.php'; ?>"/>
nothing is displayed!
How am I supposed to do that ?
Thank you!
OK: What do you see if you open classes\testeur_classe.php in the browser?
(p.s. the same question as Ryan Graham asked you in question comment)
OK: I think you must set correct headers before picture output like:
header('Content-type: image/png');
p.s.
This code works, just tried it on my machine. You must have bug on <img src="..." or <base href="" if you have one. Could you show us your html output so we can see what could be the problem?
You need to make sure that the image src is a valid URL to the script. Looking at the backslash in there my guess would be that that is in fact a filesystem path.