Jump to content

QR image generator questions...(uploading the image after its created)


cdoggg94

Recommended Posts

So I have been given some code to generate a QR code image so what ever you want the QR to display and its working fine now.

 

What I want to do, is take the file it creates and upload it to my /images/ folder, and ALSO take some of the form information and INSERT it into my database.

 

This is the QR Generator Code:


<?php

   echo "<h1>PHP QR Code</h1><hr/>";

   //set it to writable location, a place for temp generated PNG files
   $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;

   //html PNG location prefix
   $PNG_WEB_DIR = 'temp/';


   include "qrlib.php";    

   //ofcourse we need rights to create temp dir
   if (!file_exists($PNG_TEMP_DIR))
       mkdir($PNG_TEMP_DIR);


   $filename = $PNG_TEMP_DIR.'test.png';

   //processing form input
   //remember to sanitize user input in real-life solution !!!
   $errorCorrectionLevel = 'L';
   if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
       $errorCorrectionLevel = $_REQUEST['level'];    


   $matrixPointSize = 4;
   if (isset($_REQUEST['size']))
       $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);



   if (isset($_REQUEST['data'])) { 

       //it's very important!
       if (trim($_REQUEST['data']) == '')
           die('data cannot be empty! <a href="?">back</a>');

       // user data
       $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
       QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);    

   } else {    

       //default data
       echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';    
       QRcode::png('PHP QR Code ', $filename, $errorCorrectionLevel, $matrixPointSize, 2);    

   }    

   //display generated file
   echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>';  





   //config form
   echo '<form action="index.php" method="post">
<table border="0">
<tr>
<td>Name: </td><td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td>Content: </td><td><textarea name="content" cols="45" rows="15"></textarea></td>
</tr>
<tr>
   <td>Data: </td><td><input name="data" value="'.(isset($_REQUEST['data'])?htmlspecialchars($_REQUEST['data']):'PHP QR Code ').'" /></td>
</tr>
<tr>
   <td>ECC: </td><td><select name="level">
           <option value="L"'.(($errorCorrectionLevel=='L')?' selected':'').'>L - smallest</option>
           <option value="M"'.(($errorCorrectionLevel=='M')?' selected':'').'>M</option>
           <option value="Q"'.(($errorCorrectionLevel=='Q')?' selected':'').'>Q</option>
           <option value="H"'.(($errorCorrectionLevel=='H')?' selected':'').'>H - best</option>
       </select></td>
</tr>
<tr>
<td>Size: </td><td><select name="size">';

   for($i=1;$i<=10;$i++)
       echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>';

   echo '</select></td></tr>
<tr>
<td> </td>
<td><input type="submit" value="GENERATE"></td>
 </tr>

 </table>
 </form>

 <hr/>';

   // benchmark
   QRtools::timeBenchmark(); 

?>

 

Below was my idea of how to upload and insert...but i dont know what value to give the $showname because it usually comes from a form.

 

<?php  


   echo "<h1>PHP QR Code</h1><hr/>";

   //set it to writable location, a place for temp generated PNG files
   $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;

   //html PNG location prefix
   $PNG_WEB_DIR = 'temp/';

   include "qrlib.php";    

   //ofcourse we need rights to create temp dir
   if (!file_exists($PNG_TEMP_DIR))
       mkdir($PNG_TEMP_DIR);


   $filename = $PNG_TEMP_DIR.'test.png';

   //processing form input
   //remember to sanitize user input in real-life solution !!!
   $errorCorrectionLevel = 'L';
   if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
       $errorCorrectionLevel = $_REQUEST['level'];    

   $matrixPointSize = 4;
   if (isset($_REQUEST['size']))
       $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);


   if (isset($_REQUEST['data'])) { 

       //it's very important!
       if (trim($_REQUEST['data']) == '')
           die('data cannot be empty! <a href="?">back</a>');

       // user data
       $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
       QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);    

   } else {    

       //default data
       echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';    
       QRcode::png('PHP QR Code ', $filename, $errorCorrectionLevel, $matrixPointSize, 2);    

   }    

   //display generated file
   echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>';  

//------------------------------------------------------------------------------upload/insert------------------------------------------------------

set_time_limit(0);
require_once("Connections/busloreConnect.php");
mysql_select_db("db_businesslore_com");

$name = $_POST['name'];
$content = $_POST['content'];

$filetype = $_FILES['pic']['type'];
if($filetype == "image/png") { 
$target_path = "images/".$target_path.basename( $_FILES['pic']['name']); 
$showname = basename( $_FILES['pic']['name']);//-----------------*****************i dont know which value to give this***********---------------------//
if(move_uploaded_file($_FILES['pic']['tmp_name'], $target_path)) {
$querystring = "INSERT INTO qr_generator(qr_id,qr_name,qr_content,qr_pic) VALUES(NULL,'".$name."','".$content."','".$showname."')";
$doquery = mysql_query($querystring);
echo "";
} else{
   echo "There was an error uploading the file, please try again!";
}
}

//-------------------------------------------------------------------------------------------------------------------------------------------------

   //config form
   echo '<form action="index.php" method="post">
<table border="0">
<tr>
<td>Name: </td><td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td>Content: </td><td><textarea name="content" cols="45" rows="15"></textarea></td>
</tr>
<tr>
   <td>Data: </td><td><input name="data" value="'.(isset($_REQUEST['data'])?htmlspecialchars($_REQUEST['data']):'PHP QR Code ').'" /></td>
</tr>
<tr>
   <td>ECC: </td><td><select name="level">
           <option value="L"'.(($errorCorrectionLevel=='L')?' selected':'').'>L - smallest</option>
           <option value="M"'.(($errorCorrectionLevel=='M')?' selected':'').'>M</option>
           <option value="Q"'.(($errorCorrectionLevel=='Q')?' selected':'').'>Q</option>
           <option value="H"'.(($errorCorrectionLevel=='H')?' selected':'').'>H - best</option>
       </select></td>
</tr>
<tr>
<td>Size: </td><td><select name="size">';

   for($i=1;$i<=10;$i++)
       echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>';

   echo '</select></td></tr>
<tr>
<td> </td>
<td><input type="submit" value="GENERATE"></td>
 </tr>

 </table>
 </form>

 <hr/>';

   // benchmark
   QRtools::timeBenchmark(); 
?>

 

Can anyone let me know if I am on the right track ? or more likely.... way off?

Link to comment
Share on other sites

there is also a file in the library called "qrimage.php"

 

which is attached to the "qrlib.php" file.

 

the code on this page is this:

<?php

define('QR_IMAGE', true);


   class QRimage {

       //----------------------------------------------------------------------
       public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE) 
       {
           $image = self::image($frame, $pixelPerPoint, $outerFrame);

           if ($filename === false) {
               Header("Content-type: image/png");
               ImagePng($image);
           } else {
               if($saveandprint===TRUE){
                   ImagePng($image, $filename);
                   header("Content-type: image/png");
                   ImagePng($image);
               }else{
                   ImagePng($image, $filename);
               }
           }

           ImageDestroy($image);
       }

       //----------------------------------------------------------------------
       public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85) 
       {
           $image = self::image($frame, $pixelPerPoint, $outerFrame);

           if ($filename === false) {
               Header("Content-type: image/jpeg");
               ImageJpeg($image, null, $q);
           } else {
               ImageJpeg($image, $filename, $q);            
           }

           ImageDestroy($image);
       }

       //----------------------------------------------------------------------
       private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) 
       {
           $h = count($frame);
           $w = strlen($frame[0]);

           $imgW = $w + 2*$outerFrame;
           $imgH = $h + 2*$outerFrame;

           $base_image =ImageCreate($imgW, $imgH);

           $col[0] = ImageColorAllocate($base_image,255,255,255);
           $col[1] = ImageColorAllocate($base_image,0,0,0);


           imagefill($base_image, 0, 0, $col[0]);




           for($y=0; $y<$h; $y++) {
               for($x=0; $x<$w; $x++) {
                   if ($frame[$y][$x] == '1') {
                       ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]); 
                   }
               }
           }

           $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
           ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
           ImageDestroy($base_image);

           return $target_image;
       }
   }

 

could this be where I would add the upload file script ?

Link to comment
Share on other sites

If you want to store the image on the same server that is generating the image, you do not "upload" the image --- it is already there. You just need to store or copy the image to where you want it.

 

This part of the code:

if (isset($_REQUEST['data'])) { 

   //it's very important!
   if (trim($_REQUEST['data']) == '')
           die('data cannot be empty! <a href="?">back</a>');

   // user data
   $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
   QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);  

} else {        

   //default data
   echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';     
   QRcode::png('PHP QR Code ', $filename, $errorCorrectionLevel, $matrixPointSize, 2);   

}       

appears to be placing the image in a file in the $PNG_TEMP_DIR. You should be able to copy or move the generated file to your images directory.

 

On a side note, unless there is some code somewhere to clean out that temporary directory, it is going to collect a lot of files. I would recommend cleaning it out periodically (with a cron job).

Link to comment
Share on other sites

I ended up with this:

 

// user data
       $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
       QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);
 //copy image to images folder
 copy($PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png', 'images/test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'); 

 

and it seemed to have worked pretty good !

 

but its only copied so yea I am going to have to clean out the temp folder from time to time

 

Thanks a lot for the help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.