Hyper Smash

Friday, 31 May 2013

"Get list of images based on width and height AND get list of files from a directory based on files extenstion"

<?php
/* "get list of images based on width and height" */
$dirpath = 'imgdirname'; // Name of folder(directory) containing the images files
function getimg($w,$h,$dirpath)
{
    $filesimg= array();
    $dh  = opendir($dirpath);
    while (false !== ($filename = readdir($dh)))
    {
        if(!is_dir($filename))
        {
            $imginfo = getimagesize($dirpath.'/'.$filename);
            if($imginfo[0] == $w || $imginfo[0] == $h ){
                $filesimg[] = $filename;
            }
        }
    }
    sort($filesimg);
    return $filesimg;
}
$imgdata = getimg(100,100,$dirpath);
echo "<pre>";print_r($imgdata); exit;

/* get list of html files from a directory*/
$dirpath = 'htmlfiledirc'; // Name of folder(directory) containing all the html files
function getHtmlfileList($dirpath,$fileExt)
{
    $fileshtml= array();
    $dh  = opendir($dirpath);
    while (false !== ($filename = readdir($dh)))
    {
        $files[] = $filename;
        if(!is_dir($filename) && strstr($filename,$fileExt)){
            $fileshtml[] = $filename;
        }
    }
    return $fileshtml;
}
$fileExt = '.html';
$filesdata = getHtmlfileList($dirpath,$fileExt);
echo "<pre>";print_r($imgdata); exit;
?>

Monday, 27 May 2013

"X cart Product Sort By - Dropdown"


Use belwo code instead of current used by "main/search_sort_by.tpl" or "customer/search_sort_by.tpl"

<strong class="search-sort-title">{$lng.lbl_sort_by}:</strong>
      <select name="sort_by" ONCHANGE="location = this.options[this.selectedIndex].value;" >
    {foreach from=$sort_fields key=name item=field}
        {if $navigation_page > 1}
            {assign var="cur_url" value=$url|cat:"page="|cat:$navigation_page|cat:"& amp;sort="|cat:$name|cat:"&amp;sort_direction="}
        {else}
            {assign var="cur_url" value=$url|cat:"sort="|cat:$name|cat:"&amp;sort_direction="}
        {/if}
        <option value="{$cur_url}1" {if $name eq $selected && $direction eq 1}selected="selected"{/if}>{$field} (UP)</option>
        <option value="{$cur_url}0" {if $name eq $selected && $direction eq 0}selected="selected"{/if}>{$field} (Down)</option>
    {/foreach}
    </select>
 

Monday, 13 May 2013

"PHP Function For Truncate string and function For Truncate string"

/* Below function  For Truncate string */
    function truncate($text, $chars) {
    $add_dots='';
    if(strlen($text) > $chars){
        $add_dots = "...";
    }
    $text = str_replace('"','&rdquo;',$text);
    $text = str_replace('"','&ldquo;',$text);
    $text = str_replace("\n", "", $text);
    $text = str_replace("\r", "", $text);


    $text = $text." ";
    $text = substr($text,0,$chars);
    $text = substr($text,0,strrpos($text,' '));
    $text = $text.$add_dots;
    return $text;
    }
    /*Function for refine tag and quotes in description. */
    function refineTagQuotes($text)
    {
        $text = str_replace('"','&rdquo;',$text);
        $text = str_replace('"','&ldquo;',$text);
        $text = str_replace("\n", "", $text);
        $text = str_replace("\r", "", $text);
        return $text;
    }

Wednesday, 8 May 2013

Event Based calendar with Javascript

<html>
    <head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/scripts/date.js"></script>
<script type="text/javascript" src="http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/scripts/jquery.datePicker.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/styles/datePicker.css">
<link rel="stylesheet" type="text/css" media="screen" href="http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/styles/datePicker.css">

<script type="text/javascript" charset="utf-8">
    $(function()
            {
                $('.date-pick').datePicker()
                $('#start-date').bind(
                    'dpClosed',
                    function(e, selectedDates)
                    {
                        var d = selectedDates[0];
                        if (d) {
                            d = new Date(d);
                            $('#end-date').dpSetStartDate(d.addDays(1).asString());
                        }
                    }
                );
                $('#end-date').bind(
                    'dpClosed',
                    function(e, selectedDates)
                    {
                        var d = selectedDates[0];
                        if (d) {
                            d = new Date(d);
                            $('#start-date').dpSetEndDate(d.addDays(-1).asString());
                        }
                    }
                );
            });
</script>
    </head>
    <body>
        <ol>
                        <li><label for="start-date">Start date:</label>
            <input name="start-date" id="start-date" class="date-pick" /></li>
                        <li><label for="end-date">End date:</label>
            <input name="end-date" id="end-date" class="date-pick" /></li>   
        </ol>
    </body>
</html>

Image Upload With Resize



Image Upload With Resize

Create following directory structure
ImageResize (foldername)
ImageUploadWithResize.php
delete_all_images.php
uploads (foldername add 777 permission to this folder)

ImageUploadWithResize.php

<?php
//Define Memory Limit for large images so that we do not get "Allowed memory exhausted"
ini_set("memory_limit", "200000000");


// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
            // file needs to be jpg,gif,bmp,x-png and 4 MB max
            if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
            {
                        // some settings
                        $max_upload_width = 2592; $max_upload_height = 1944;
                        // if user chosed properly then scale down the image according to user preferances
                        if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
                                    $max_upload_width = $_REQUEST['max_width_box'];
                        }   
                        if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
                                    $max_upload_height = $_REQUEST['max_height_box'];
                        }         

                        // if uploaded image was JPG/JPEG
            if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg")
{         
                                    $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
                        }                     
                        // if uploaded image was GIF
                        if($_FILES["image_upload_box"]["type"] == "image/gif"){
                                    $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
                        }         
                        // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)           
                        // if uploaded image was BMP
                        if($_FILES["image_upload_box"]["type"] == "image/bmp"){          
                                    $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
                        }                                 
                        // if uploaded image was PNG
                        if($_FILES["image_upload_box"]["type"] == "image/x-png"){
                                    $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
                        }
                       
                       
                        $remote_file = "uploads/".$_FILES["image_upload_box"]["name"];
                        imagejpeg($image_source,$remote_file,100);
                        chmod($remote_file,0644);
                       
                        // get width and height of original image
                        list($image_width, $image_height) = getimagesize($remote_file);
           
                        if($image_width>$max_upload_width || $image_height >$max_upload_height){
                                    $proportions = $image_width/$image_height;
                                   
                                    if($image_width>$image_height){
                                                $new_width = $max_upload_width;
                                                $new_height = round($max_upload_width/$proportions);
                                    }                     
                                    else{
                                                $new_height = $max_upload_height;
                                                $new_width = round($max_upload_height*$proportions);
                                    }                     
                                   
                                   
                                    $new_image = imagecreatetruecolor($new_width , $new_height);
                                    $image_source = imagecreatefromjpeg($remote_file);
                                   
                                    imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
                                    imagejpeg($new_image,$remote_file,100);
                                   
                                    imagedestroy($new_image);
                        }
                       
                        imagedestroy($image_source);
                       
                       
                        header("Location: ImageUploadWithResize.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
                        exit;
            }
            else{
                        header("Location: ImageUploadWithResize.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
                        exit;
            }
}
?>

<html>
<head><title>Image Upload with resize</title>
<style type="text/css">
<!--
.upload_message_success {padding:4px;background-color:#009900;border:1px solid #006600;color:#FFFFFF;margin-top:10px;margin-bottom:10px;}
.upload_message_error {padding:4px;background-color:#CE0000;border:1px solid #990000;color:#FFFFFF;margin-top:10px;margin-bottom:10px;}
-->
</style></head>
<body>
<h1 style="margin-bottom: 0px">Upload an image</h1>
<?php if(isset($_REQUEST['upload_message'])){?>
            <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
                        <?php echo htmlentities($_REQUEST['upload_message']);?>
            </div>
<?php }?>
<form action="#" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
            <label>Image file, maximum 4MB. it can be jpg, gif,  png:</label><br />
    <input name="image_upload_box" type="file" id="image_upload_box" size="40" />
    <input type="submit" name="submit" value="Upload image" /><br /><br />
            <label>Scale down image? (2592 x 1944 px max):</label><br />
            <input name="max_width_box" type="text" id="max_width_box" value="1024" size="4"> x
            <input name="max_height_box" type="text" id="max_height_box" value="768" size="4"> px.
    <br /><br />
            <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;">
                        <strong>Notes:</strong><br />
                        The image will not be resized to this exact size; it will be scalled down so that neider width or height is larger than specified.<br />
                        After you uploaded images and made tests on our server please <a href="delete_all_images.php">delete all uploaded images </a> :)<br />
            </p>
            <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
</form>
<?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>
<p><img src="uploads/<?php echo $_REQUEST['show_image'];?>" /></p>
<?php }?>
</body>
</html>

delete_all_images.php

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Delete all images</title>
</head>

<body>

<?
   $dir = 'uploads/';
   // open specified directory
   $dirHandle = opendir($dir);
   $total_deleted_images = 0;
   while ($file = readdir($dirHandle)) {
      // if not a subdirectory and if filename contains the string '.jpg'
      if(!is_dir($file)) {
         // update count and string of files to be returned
                         unlink($dir.$file);
         echo 'Deleted file <b>'.$file.'</b><br />';
                         $total_deleted_images++;
      }
   }
   closedir($dirHandle);
            if($total_deleted_images=='0'){
                        echo 'There ware no files uploaded there.';
            }
            echo '<br />Thank you.';
            echo '<br /><a href="ImageUploadWithResize.php" title="Upload Image"> Upload New Image </a>';

?>

</body>
</html>

Monday, 6 May 2013

PHP Time Stamp Function

time_stamp.php
Contains PHP code. You have to use the time_stamp() function to display the date and time.

<?php
function time_stamp($session_time)
{
$time_difference = time() - $session_time ;

$seconds = $time_difference ;
$minutes = round($time_difference / 60 );
$hours = round($time_difference / 3600 );
$days = round($time_difference / 86400 );
$weeks = round($time_difference / 604800 );
$months = round($time_difference / 2419200 );
$years = round($time_difference / 29030400 );



// Seconds
if($seconds <= 60)
{
echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60)
{

   if($minutes==1)
  {
   echo "one minute ago";
   }
   else
   {
    echo "$minutes minutes ago";
   }

}
//Hours
else if($hours <=24)
{

   if($hours==1)
  {
   echo "one hour ago";
  }
  else
  {
   echo "$hours hours ago";
  }

}
//Days
else if($days <= 7)
{

  if($days==1)
  {
   echo "one day ago";
  }
  else
  {
   echo "$days days ago";
   }

}
//Weeks
else if($weeks <= 4)
{

   if($weeks==1)
  {
   echo "one week ago";
   }
  else
  {
   echo "$weeks weeks ago";
  }

}
//Months
else if($months <=12)
{

   if($months==1)
  {
   echo "one month ago";
   }
  else
  {
   echo "$months months ago";
   }

}
//Years
else
{

   if($years==1)
   {
    echo "one year ago";
   }
   else
  {
    echo "$years years ago";
   }

}

}
$session_time ="1264326122";
//$session_time=time();
echo time_stamp($session_time);
?>