Archive | CakePHP RSS feed for this section

Easy way to generate thumbnails with GD library on runtime

23 Jan

It is really a panic work with image when we need to make several thumbnails. We can do it directly from Photoshop or with GD library. Generally when we upload an image we make two copies. One is for thumbnail view and another for large view.

It would be an inefficient idea to make 4 or 5 copies of image. This technique is wasting of space as well as time. Let’s say if you have to change the size of the thumbs then either you have to do it manually or you have to say not possible.

What if we don’t generate any thumbnail?!! 😀

Actually it is very easy to write some line of code to resize your image runtime. To get ride of this panic situation. I just wrote a simple function. It receive file path and new size and return image object. And IMG tag can render it easily. So i can resize image in any size with it.

Lets we have a look in following information


$image_file_path = ‘img/my_image.jpg’; // File path of the image
$resize_to = ‘100’; //100 pixel


Now we have to set an URL in image tag from where my server side langue pick the image file path and width. Then we have to generate the image again and return the image object. To send the image file path I like to make it base64_encode otherwise it will edit the image base URL. Now let’s see how I called the image ULR from image tag.

For CakePHP we can do it.


echo ‘<img ” src=”‘ . $html->url(‘/’) . ‘common/get_image/’ . base64_encode($image_file_path) . ‘/100/fix” />’;


Here we have a controller named “Common” and from where we calling “get_image” function.

For general use. We can write is like this.


echo ‘<img ” src=” file=’ . base64_encode($image_file_path) . ‘&size=100& resize_flag=fix” />’;


Now we have to execute the function. This function receive three parameter. First parameter is encode file path of the image. Second one is the new size of the image. If you sent third parameter in percentage or keep it blank then you have to set size like .5 for 50%, .25 or 25% or 1 for 100%. And if you keep third parameter “Fix” then you have to set size in pixel.

For cakePHP simply we have to place the function in “Common” controller.


class CommonController extends AppController {
var $name = ‘Common’;
var $uses = array(”);
var $layout =’blunk’;
var $pageTitle = ‘Process Image’;

// Resize image
function get_image( $file = NULL, $size = NULL,$resize_flag = ‘Percentage’ )
// Image resize code here

for simple implementation we can do following process

$image_path = $_GET[‘file’];
$size = $_GET[‘size’];
$resize_flag = $_GET[‘resize_flag’];

// Call the function from here
get_image($image_path, $size, $resize_flag )

// Declaration of the resize image function
function get_image( $file = NULL, $size = NULL,$resize_flag = ‘Percentage’ )
// Image resize code here

This is the final step of the process. New here is the source of the get_image function.

// Resize image
function get_image( $file = NULL, $size = NULL,$resize_flag = ‘Percentage’ )

//Set the layout to ajax

// Decode the image path
$file = base64_decode($file);
// This sets it to a .jpg, but you can change this to png or gif
header(‘Content-type: image/jpeg’);

// Setting the resize parameters
list($width, $height) = getimagesize($file);

if( $resize_flag == ‘Percentage’)
$modwidth = $width * $size;
$modheight = $height * $size;
$modwidth = $size;
$modheight = ( $height * $size ) / $width;

// Resizing the Image
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

// Outputting a .jpg, you can make this gif or png if you want
//notice we set the quality (third value) to 100
imagejpeg($tn, null, 100);
}//function get_image()


Once you have set total process then you need not to think about resize. Just change the size from image source. Then image will resize proportionally. I got a good advantage from this function. Hope you guys will enjoy it.

Easy way to generate PDF in PHP

9 Oct

Working with PDF is really sticky. We need to calculate every pixel. And if we use ZEND LIBRARY arranging the layout is also a big issue. Recently I used FPDF ( library it is really nice thing. [Thnx to HASIN BHAI  as he referd me to FPDF.]

Using  FPDF is really easy and fancy. For layout it use cells almost like table. Sot we can add text any where easily.

They have a good tutorial to learn it in a second. I am just describing their fired example

$pdf=new FPDF();
$pdf->Cell(40,10,’Hello World!’);

In first line we how to include the PDF library.

$pdf=new FPDF();
This is the reference of the fpdf library.

This instruction use to arrange the page layout. We can send some string parameter to define the page like ‘A4’ ‘L’ (Landscape) etc.

This line is use to define the font. But have to keep in mind that we need the font folder with font reference with script.

There are some  extra instruction like
$this->SetDrawColor(0,80,180); // Define the border color or graphics color
$this->SetFillColor(230,230,0);  //  The graphics or cell background color
$this->SetTextColor(220,50,50); // Text color.

To add image we can use a simple function  

Now to add text normally we use cell. This is a very commonly used function in FPDF with some parameter.
First tow parameter means width and height. Third parameter is txt.
Fourth parameter define border. If we put 1 will enable border in all side. Also can send T for top, B for bottom, L for left and R for right. Let’s say we want to put border at top and left side then we have to write like this $pdf->Cell(20,10,’Title’,’LT’,1,’C’);
Fifth parameter defines the alignment. 0 for arrange after your current cell and 1 for put in new line.
Sixth parameter defines the text alignment.
Seventh parameter user for cell background must be 1 for colored or 0 for transparent. Default value: 0.

Now to send the PDF in output we user $pdf->output() function. If we want to  show in browser instantly just simply use  $pdf->Output(); and if we want to save as file then just enter the path in the function like $pdf->Output(‘/home/pub_html/text.pdf’);

Another on instruction we use frequently is $pdf->Ln() to go next line by force.

Loading time of a cakePhp project.

11 Jul

Well, today I m gonna talk about another shit that I faced recently.

Sometimes its not good to have some talents customers who really understand and identify your problems…hahaha…i know I sound like a nerd…….but this findings helped me to reorganize  loading time of my site….

well let me tell u guys the whole story……..
Continue reading