Web Development Tips

PHP – Create thumbnail image

Posted by: slvramesh on: June 15, 2011

PHP – Create thumbnail image
/** Common functions */
function make_thumb($name, $src, $dest, $new_w, $new_h) {
$border=false;
$transparency=true;
$base64=false;

if(!file_exists($src.DS.$name))
return false;

$arr = split("\.", $name);
$ext = strtolower($arr[count($arr)-1]);

if($ext=="jpeg" || $ext=="jpg"){
$img = @imagecreatefromjpeg($src.DS.$name);
} elseif($ext=="png"){
$img = @imagecreatefrompng($src.DS.$name);
} elseif($ext=="gif") {
$img = @imagecreatefromgif($src.DS.$name);
}
if(!$img)
return $name;

$old_x = imageSX($img);
$old_y = imageSY($img);

// proportionately - re-size image
/*
if($old_x < $new_w && $old_y $old_y) {
$thumb_w = $new_w;
$thumb_h = floor(($old_y*($new_h/$old_x)));
} elseif ($old_x < $old_y) {
$thumb_w = floor($old_x*($new_w/$old_y));
$thumb_h = $new_h;
} elseif ($old_x == $old_y) {
$thumb_w = $new_w;
$thumb_h = $new_h;
}*/

$thumb_w = $new_w;
$thumb_h = $new_h;

$thumb_w = ($thumb_w<1) ? 1 : $thumb_w;
$thumb_h = ($thumb_h= 0) {
//its transparent
$trnprt_color = imagecolorsforindex($img, $trnprt_indx);
$trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($new_img, 0, 0, $trnprt_indx);
imagecolortransparent($new_img, $trnprt_indx);
}
}
else{
$white = imagecolorallocate($new_img, 255, 255, 255);
Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
imagefilledrectangle($new_img, 0, 0, $new_w, $new_h, $white);
}
} else {
$white = imagecolorallocate($new_img, 255, 255, 255);
Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
imagefilledrectangle($new_img, 0, 0, $thumb_w, $thumb_h, $white);
}

imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);

if($border) {
$black = imagecolorallocate($new_img, 0, 0, 0);
imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);
}

$dest_filename = $arr[0].'_'.$new_w.'_'.$new_h.'.'.$ext;
$dest_filename = strtolower($dest_filename);
$dest_path = $dest.DS.$dest_filename;

if($base64) {
ob_start();
imagepng($new_img);
$img = ob_get_contents();
ob_end_clean();
$return = base64_encode($img);
} else {
if($ext=="jpeg" || $ext=="jpg"){
imagejpeg($new_img, $dest_path);
} elseif($ext=="png"){
imagepng($new_img, $dest_path);
} elseif($ext=="gif") {
imagegif($new_img, $dest_path);
}
}
imagedestroy($new_img);
imagedestroy($img);

//Delete the orginal file
if($src==$dest)
@unlink($src.DS.$name);
return $dest_filename;
}

Drupal : Find the path or menu available in drupal site.

Posted by: slvramesh on: September 1, 2010

When you going to add or edit path / menu in your site, it is necessary to check the menu are already taken by the site, other wise it will conflict.
When you create internal menu the drupal system automatically check with you site. but when you create external or via coding the below code will help to validate the path already taken or not.

All the internal or external menus are stored in the table “menu_router”.

/*
* Find the path is available
* Parameter menu path
* return TRUE if menu exists else return FALSE
*/

function is_path_exists($path) {
return (db_fetch_array(db_query("SELECT * FROM {menu_router} where path = '%s' ", $path))) ? TRUE : FALSE;
}

How to call customized page template in drupal pages?

Posted by: slvramesh on: July 13, 2010

Drupal template system is very interesting because you can call any of page template as to your decided page. The following way you can call page template in drupal.
1) As per node or content type
2) As per menu
3) As per your argument in menu

The above three categories are covers your all drupal pages. So the following code will used to get template suggestion as per node or menu or argument.


/**
* Override or insert PHPTemplate variables into the templates.
*/
function phptemplate_preprocess_page(&$vars) {
// first, proceed with a modifed version of the standard
// Drupal template suggestion calls
$i = 0;
$suggestions = array();
$suggestion = 'page';
while ($arg = arg($i++)) {
$suggestions[] = $suggestion .'-'. $arg;
if (!is_numeric($arg)) {
$suggestion .= '-'. $arg;
}
}
if (drupal_is_front_page()) {
$suggestions[] = 'page-front';
}
// next, check for templates that use the path alias
if (module_exists('path')) {
$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$suggestions[] = $template_filename;
}
}
$vars['template_files'] = $suggestions;
} // end path alias template check
if ($suggestions) {
$vars['template_files'] = $suggestions;
}

if (isset($vars['node'])) {
// When viewing a node focused as a full "page", it will suggest the node type.
// For example, a node type of "some_foo" will search for "page-some-foo.tpl.php".
$vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
}
}

The phptemplate_page_preprocess used to override drupal variables. The template file have been override the above code.

The same way you can user node template also.

How to display sub-menus in drupal?

Posted by: slvramesh on: June 22, 2010

The following code used to get sub-menu item of their parent menu.
Syntax:
$menu = menu_get_item(”);

Example:

$menu = menu_get_item('admin/store/export');
$content = system_admin_menu_block($menu);
$output = theme('admin_block_content', $content);

The above code get all the sub-menus under export parent menu.

PHP Interview questions

Posted by: slvramesh on: May 28, 2010

I decided to keep on updating interview questions in this post. Because of hadling interview / attending interview in my previous company most of technical guys very strong in php coding and mysql etc. But no one update her/his small small technical knowledge even my self. So I will update my self and to you. Keep on view this post every week. I will update my knowledge this post. Do you have any question feel free just comment this post i will answer you.

Question 1: How to validate XHTML?

Answer: Before going to answer this question, i need to explain some of basic things in XHTML. I used Dreamweaver in windows, Aptana in Mac, Komodo in Linux. Those editors make default in document type, header and body tags. some of guys never take care about the document type because of the editors will handle. It is important one. Make sure what are the editors you are used and what are the default code placed when you create new html file. I hope every one should know about what is XHTML. Let see about How to validate XHTML.

The XHTML have Three doctype.
1. strict
2. transitional
3. frameset

Using above three doctype you can validate XHTML. For more information see How to validate XHTML

Question 2: What are the functions used to find Brwoser in AJAX?
Answer:
For getting IE7+, Firefox, Chrome, Opera, Safari browsers use the following code.
xmlhttp=new XMLHttpRequest();

For getting IE6, IE5 use the following code
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);

Problem definition:

Drupal is the best CMS with building media related sites. Galleria is the best module for image gallery in drupal, the galleria module did not support caption when you use image filefield because the gallery module get caption for field description. When you use filefield it is support title and alt input field.

So here is the solution for getting caption for image in title tag.

1) In the content type manage fields enable alt and title tags.
2) Add the following coding in your galleria.module file function theme_galleria_formatter_imagefield_galleria end the “alt” text.
‘title’ => $element[$key]['#item']['data']['title']
3) Replace the coding in the function “template_preprocess_galleria”
$caption = ($image->title != $image->filename) ? $image->description : ”;
to
$caption = ($image->title != $image->filename) ? $image->title : ”;

Now view your galleria image gallery, caption will dispaly.

Cheers!

Menu breadcrumb – Problem in Views pages (Drupal)

Posted by: slvramesh on: May 7, 2010

Menu breadcrumb has problem in views page. Having spend some times customized in template pages.

Add the below code in your template.php them file.

function phptemplate_preprocess_page(&$variables) {
$breadcrumb = drupal_set_breadcrumb();
if($breadcrumb[1]==”"){
$breadcrumb[1] = $variables["title"];
$variables['breadcrumb'] = theme(‘breadcrumb’, $breadcrumb);
}
}

HTML5 Forms

Posted by: slvramesh on: March 29, 2010

HTML5 Forms have more features. Check his url http://snook.ca/archives/html_and_css/html5-forms-are-coming

The below code used to show dynamic videos for depending upon the client machine.

Example i have open the page in windows machine the windows video is shown. if i open the page in mac system the mac video is shown.

Function refference:

1) getos – this function used to get client machine environment

2) replacevideo – this function used to return the bulk of video object

3) add_video_content – this function used to filter the content.

How to use this code?

1) Open in your wordpress/wp-content/themes/youthemes/functions.php file and add the below code.

2) Edit your post / create your post and add the line ‘[getvideo 1]‘.

Code:

/*
* function find the text “[getvideo 1]” in your post content
* and replace the video file
*/
function add_video_content($content = ”) {
$platform = getos();
$winmsg = ‘<strong>Windows videos</strong><br>’;
$macmsg = ‘<strong>Mac videos</strong><br>’;
if(trim($platform)==”mac”)
$content = str_replace(‘[getvideo 1]‘, $macmsg . replacevideo(‘mac/macvideo.flv’), $content);
else
$content = str_replace(‘[getvideo 1]‘, $winmsg . replacevideo(‘windows.flv’), $content);
echo $content;
}

//add filter the_content
add_filter( “the_content”, “add_video_content” );

//replaced video object
function replacevideo($vidfile){
$st = ‘<object width=”500″ height=”402″ data=”http://dragonfablepowerlevel.com/members/flowplayer/flowplayer-3.1.5.swf” type=”application/x-shockwave-flash”>’;
$st .= ‘<!–> <![endif]–>’;
$st .= ‘<!–[if IE]>’;
$st .= ‘<object type=”application/x-shockwave-flash” width=”500″ height=”402″‘;
$st .= ‘classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″‘;
$st .= ‘codebase=”http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0″>’;
$st .= ‘<![endif]–>’;
$st .= ‘<param value=”http://yourdomain/flowplayer/flowplayer-3.1.5.swf” name=”movie”/>’;
$st .= ‘<param value=”true” name=”allowFullScreen”/>’;
$st .= ‘<param value=”transparent” name=”wmode”/>’;
$st .= ‘<param value=”sameDomain” name=”allowScriptAccess”/>’;
$st .= ‘<param value=”high” name=”quality”/>’;
$st .= ‘<param value=”config={\’playerId\’:\’player\’,\’clip\’:{\’url\’:\’http://yourdomain/’ . $vidfile .’\'},\’playlist\’:[{\'url\':\'http://yourdomain/' . $vidfile .'\',\'autoPlay\':false, \'loop\':true}]}” name=”flashvars”/><p>Your browser is not able to display this multimedia content.</p>’;
$st .= ‘</object>’;
return $st;
}

// Get client platform
function getos(){
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (preg_match(‘/linux/’, $userAgent)) {
$platform = ‘linux’;
}
elseif (preg_match(‘/macintosh|mac os x/’, $userAgent)) {
$platform = ‘mac’;
}
elseif (preg_match(‘/windows|win32/’, $userAgent)) {
$platform = ‘windows’;
}
else {
$platform = ‘unrecognized’;
}
return $platform;
}

A simple home page review: How to make a good home page?

Posted by: slvramesh on: November 9, 2009

I interested to looking beautiful websites because of my profession is developing quality websites. I think home page is most important for every website. If the visitor able to stay 3 minutes in your home page you site is good other wise you need to concentrate your home page.

The fantastic example for good home page is “Google Home page”.

Google homepage continues to maintain a bare minimum display of only 32 words, 19 links and 0 advertisement.

Why home page is important?

  1. Without home you cant do anything. The same principle applies to your web page. So give more important to your home page.
  2. All Search engine crawl your site via home page. Not only search engine the visitors also view your site via home page.
  3. Home page is the icon of your business.

What are the things you satisfy in your home page?

  1. Loading time.
  2. Look and feel and Eye catching color.
  3. Show your business relevant information.
  4. Keywords (Meta data).
  5. Browser compatibility.
  6. Site navigation.

 

Follow

Get every new post delivered to your Inbox.