Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

May 24, 2007

Truncate a string without breaking a word

function truncate_str($string, $length = 80, $etc = '...',$break_words = false)
{
if ($length == 0)
return '';
if (strlen($string) > $length) {
$length -= strlen($etc);
if (!$break_words)
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
return substr($string, 0, $length).$etc;
} else
return $string;
}

April 30, 2007

To chek your link on other site

Check Reciprocal linking:

function
check_backlink($remote_url, $link) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 9);
$data = curl_exec($ch);

if (
curl_errno($ch)) {
return
'URL not working';
} else {
if (
eregi($link, $data)) return 'Found'; else return 'Not Found';
}
curl_close($ch);
}

March 29, 2007

Run Time Image Thumbnail

This code can be used to create Dynamic Image thumbnails on the fly
i.e. You need not to bother about the actual image size while uploading. Just show the desired scaled image anywhere on your website. Same image may need to display in different size on different web-page.


/********** (thumb.php) ****************/
$img=$_GET['img']; $w=$_GET['w']; $h=$_GET['h'];
if(!defined('DIR_CACHE'))
define('DIR_CACHE', './image_cache/');

if (!Is_Dir(DIR_CACHE))
mkdir(DIR_CACHE, 0777);

#IMAGE RESIZE AND SAVE TO FIT IN $new_width x $new_height
if (file_exists($img))
{
$thumb=strtolower(preg_replace('/\W/is', "_", "$img $w $h"));
$changed=0;

if(!is_file($img))
$img = "images/product-image.jpg";

if (file_exists($img) && file_exists(DIR_CACHE.$thumb))
{
$mtime1=filemtime(DIR_CACHE.$thumb);
$mtime2=filemtime($img);
if ($mtime2 > $mtime1)
$changed=1;
}
elseif (!file_exists(DIR_CACHE.$thumb))
$changed=1;

if ($changed)
{
$filename=$img;
$new_width=(int)@$w;
$new_height=(int)@$h;
$lst=GetImageSize($filename);
$image_width=$lst[0];
$image_height=$lst[1];
$image_format=$lst[2];

if ($image_format==1)
$old_image=imagecreatefromgif($filename);
elseif ($image_format==2)
$old_image=imagecreatefromjpeg($filename);
elseif ($image_format==3)
$old_image=imagecreatefrompng($filename);
else
exit;

if (($new_width!=0) && ($new_width < $image_width))
{
$image_height=(int)($image_height*($new_width/$image_width));
$image_width=$new_width;
}

if (($new_height!=0) && ($new_height < $image_height))
{
$image_width=(int)($image_width*($new_height/$image_height));
$image_height=$new_height;
}

$new_image=ImageCreateTrueColor($image_width, $image_height);
$white = ImageCopyResampled($new_image, $old_image, 0, 0, 0, 0, $image_width, $image_height, imageSX($old_image), imageSY($old_image));
#ImageFill($new_image, 0, 0, $white);
imageJpeg($new_image, DIR_CACHE.$thumb);
}

header("Content-type:image/jpeg");
readfile(DIR_CACHE.$thumb);
}
/********** CODE END ****************/

Here is the example use
< img src="thumb.php?img=image1.jpg?w=150&h=100" >

Tricky & nice :
If a thumbnail of a particular size of an image is created, script will not create the dynamic thumbnail again. It will pick that thumb from image_cache folder, if already exists.

note: "image_cache" folder requires 777 permission

March 26, 2007

Reading site content using CURL

For reading content of a website in PHP, there are lot of different ways & functions available like:
$site_content=file($url); #OR
$site_content=file_get_contents($url) ;

But these functions does not work in some server environments where "allow_url_fopen" is off in php.ini & you don't have permission to change this.
You can use the following alternative (using CURL) in that case.

$url="http://www.bestenough.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_close ($ch);
echo($store);

February 22, 2007

Random Password

This function returns you the Random password (text) of the required length. Default password length is given 8. This function is useful to give a temporary password, in case of 'User Registeration' and/or 'Forgot password'


function getRandomPassword($length=8)
{
$chars = "abcdefghijkmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 1;
$pass = '' ;

#Default: 8 digit password
while ($i <= $length)
{
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;}
return $pass;
}

February 21, 2007

Make Clickable Links

This is very small & useful function that finds & converts all the Email text, URL text to a clickable text. You just need to pass the full text & use the returned text.

Here is the code & usage example


### Use: To convert text parts(Email address, URLs) to clickable text
### Author: Akash Dwivedi
### Date: 27 June 2005

  1. function makeClickableLinks($text)
  2. {
  3. $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  4. '\\1">\\1', $text);
  5. $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  6. '\\1\\2">\\2', $text);
  7. $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
  8. '\\1">\\1', $text);
  9. return $text;
  10. }

# Example Usage
#Email address example
$text = "Mail me: you@example.com";
echo makeClickableLinks($text);

# URL example
$text = "My URL is: http://www.tech-akash.blogspot.com";
echo makeClickableLinks($text);

# FTP URL example
$text = "FTP is: ftp://ftp.example.com Here is the trailing text";
echo makeClickableLinks($text);
?>

I hope this is useful.