Archive for the ‘PHP/MySQL’ Category

Setting up and getting DOMpdf to work in XAMPP on Windows

Monday, November 23rd, 2009

Setting up the Dompdf (a pdf generation package in php) in XAMPP in windows environment can sometimes be an issue.
After we have set up the package in the htdocs and go for testing the examples in the packages , error is encountered which is as follows-

A PHP Error was encountered
Severity: Warning
Message: domdocument::domdocument() expects at least 1 parameter, 0 given
Filename: include/dompdf.cls.php
Line Number: 165
Fatal error: Call to undefined method domdocument::loadHTML() in C:\xampp\htdocs\bambooinvoice\bamboo_system_files\plugins\dompdf\include\dompdf.cls.php on line 284


The error message is caused due to a conflict between DomDocument and DomXML extentions , so the first thing we did was to disable the domdocument extention in php.ini.
extension = php_domxml.dll

After restarting the apache and then again running the examples again blank pages were being rendered. The problem was due to the fact that dompdf uses PDFLib by default if it is available in the system and PDFLib is a commercial package which is not available by default in the system but its extention is enabled in php.ini by default in the system.

So what we did was to comment out the PDFLib extension extension=php_pdf.dll in the php.ini file. After the modifications we restarted the apache and then the examples could be run on the XAMPP environment.

Improve Security using php.ini

Wednesday, March 25th, 2009

PHP has some vulnerable functions which can be used to break into your server if your scripts are not coded securely. You can setup a list of functions in php.ini using disable_functions directive. This directive allows you to disable certain functions for security reasons. It takes on a comma-delimited list of function names. This disable_functions feature is not affected by Safe Mode. This directive must be set in php.ini For example, you cannot set this in httpd.conf

Open php.ini file:

#vi /etc/php.ini

Find disable_functions and set new list as follows:

disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,

curl_multi_exec,parse_ini_file,show_source

Save and close the file. Restart httpd:

# service httpd restart

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Creating image thumbnails in php

Thursday, March 19th, 2009

Thumbnail are used by graphic designers and photographers for a small image representation of a larger image. The main advantage of creating thumbnails is that it generates the new image with the proportional dimension of the large image and hence the image resolution and quality remain intact. As the thumbnails are smaller in size they load quickly and makes the page render faster as well.

We are using a thumbnail class to make code simpler and easy to use. Image extensions with jpg, gif and png are supported for creating thumbnail using this class.

Three easy steps to create and image thumbnail :

Step 1. Create a folder and named  as createThumb.  Create a file named thumbnail_Class.php in the createThumb folder and paste the code given below into it.

<?
function createThumb($srcname,$destname,$maxwidth,$maxheight)
{
$oldimg = $srcname;
$newimg = $destname;

list($imagewidth,$imageheight,$imagetype)=@getimagesize($oldimg);

$shrinkage = 1;
if ($imagewidth > $maxwidth)
$shrinkage = $maxwidth/$imagewidth;
if($shrinkage !=1)
{
$dest_height = $shrinkage * $imageheight;
$dest_width = $maxwidth;
}
else
{
$dest_height=$imageheight;
$dest_width=$imagewidth;
}
if($dest_height > $maxheight)
{
$shrinkage = $maxheight/$dest_height;
$dest_width = $shrinkage * $dest_width;
$dest_height = $maxheight;
}
if($imagetype==2)
{
$src_img = imagecreatefromjpeg($oldimg);
$dst_img = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagejpeg($dst_img, $newimg, 75);
imagedestroy($src_img);
imagedestroy($dst_img);
}

elseif ($imagetype == 3)
{
$src_img = imagecreatefrompng($oldimg);
$dst_img = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagepng($dst_img, $newimg, 75);
imagedestroy($src_img);
imagedestroy($dst_img);
}
else
{
$src_img = imagecreatefromgif($oldimg);
$dst_img = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagegif($dst_img, $newimg, 75);
imagedestroy($src_img);
imagedestroy($dst_img);
}
}
?>

Step 2. Create a file named index.php with in this folder and paste the code given below inside index.php. This is the file which you need to run from the browser. Place your image (e.g., grapes.jpg) to create thumbnail within the createThumb folder.

<?
include(‘thumbnail_Class.php’);

$filePath    =    ‘grapes.jpg’;
$destPath    =    ‘Thumb_grapes.jpg’;
$maxwidth        =    450;
$maxheight        =    300;
createThumb($filePath,$destPath,$maxwidth,$maxheight);

echo ‘Thumbnail Created.’;
?>

Step 3. Finally, Run the index.php from your browser and if your thumbnail is created successfully then you will get an message “Thumbnail Created” and the thumnail image will create in createThumb folder with name Thumb_grapes.jpg.
(more…)