Usefull Snippets

Started on the old forum in January 2009 · view the original in the archive · old links redirect here

12 replies · 2,313 views · started 17 years ago · 1 min read

6 participants
looony Developer · 95 posts #1

this came in usefull for me so il thought i would ad it here

<?php
function shortenurl($url){
    $length = strlen($url);
    if($length > 45){
        $length = $length - 30;
        $first = substr($url, 0, -$length);
        $last = substr($url, -15);
        $newurl = $first."[ ... ]".$last;
        return $newurl;
    }else{
        return $url;
    }
}

$longurl = "http://www.google.com/search?hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial&hs=BCR&q=metacafe&btnG=Search";
$shorturl = shortenurl($longurl);
echo"<a href=\"$longurl\">$shorturl</a>";
?>

$longurl is the long url that you want shortened. $shorturl is calling the function to shorten the url down to 45 characters.

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

looony Developer · 95 posts #2

tumbnails script

// this script creates a thumbnail image from an image file - can be a .jpg .gif or .png file 
// where $thumbsize is the maximum width or height of the resized thumbnail image
// where this script is named resize.php 
// call this script with an image tag 
// [img]resize.php?path=imagepath[/img] where path is a relative path such as subdirectory/image.jpg 
$thumbsize = 200; 
$imagesource = $_GET['path']; 
$filetype = substr($imagesource,strlen($imagesource)-4,4); 
$filetype = strtolower($filetype); 
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); 
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); 
if($filetype == ".png") $image = @imagecreatefrompng($imagesource); 
if (!$image) die(); 
$imagewidth = imagesx($image); 
$imageheight = imagesy($image); 
if ($imagewidth >= $imageheight) { 
  $thumbwidth = $thumbsize; 
  $factor = $thumbsize / $imagewidth; 
  $thumbheight = $imageheight * $factor; 
} 
if ($imageheight >= $imagewidth) { 
  $thumbheight = $thumbsize; 
  $factor = $thumbsize / $imageheight; 
  $thumbwidth = $imagewidth * $factor; 
} 
$thumb = @imagecreatetruecolor($thumbwidth,$thumbheight); 
imagecopyresized($thumb, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight); 
imagejpeg($thumb); 
imagedestroy($image); 
imagedestroy($thumb);

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

looony Developer · 95 posts #3

random images script

$array = array('one','two','three','four','five');
$count = count($array) - 1;
$rand = rand (0,$count);
echo ('[img]'.$array[$rand].'[/img]');

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

looony Developer · 95 posts #4

detect language script

<?php

function lixlpixel_get_env_var($Var)
{
     if(empty($GLOBALS[$Var]))
     {
         $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
         $GLOBALS['_SERVER'][$Var] : (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var])) ? $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
     }
}

function lixlpixel_detect_lang()
{
     // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
     lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
     lixlpixel_get_env_var('HTTP_USER_AGENT');

     $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
     $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);
  
     // Try to detect Primary language if several languages are accepted.
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(strpos($_AL, $K)===0)
         return $K;
     }
  
     // Try to detect any language if not yet detected.
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(strpos($_AL, $K)!==false)
         return $K;
     }
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(preg_match("/[[( ]{$K}[;,_-)]/",$_UA))
         return $K;
     }

     // Return default language if language is not yet detected.
     return $GLOBALS['_DLANG'];
}

// Define default language.
$GLOBALS['_DLANG']='en';

// Define all available languages.
// WARNING: uncomment all available languages

$GLOBALS['_LANG'] = array(
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
);

// Redirect to the correct location.

//header('location: http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php'); // Example Implementation
echo 'The Language detected is: '.lixlpixel_detect_lang(); // For Demonstration

?>

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

looony Developer · 95 posts #5

generates random password

function generate_passwd($length = 16) {
  static $chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789';
  $chars_len = strlen($chars);
  for ($i = 0; $i < $length; $i++)
    $password .= $chars[mt_rand(0, $chars_len - 1)];
  return $password;
}

External image (may be gone)

if(isset($_SESSION['username'])) { print "everybody is a little loony inside"; } else { print"You should be shot"; }

marwin29 Junior · 35 posts #6

Mate can u help me !. How did i code if i use your image thumb to resize a ramdom image for example code a random image echo "Random Photo "; $panget = mysql_fetch_array(mysql_query("SELECT uid, imageurl FROM ibwf_photo ORDER BY RAND() LIMIT 1")); $user = getnick_uid($panget[0]); echo "<a href="index.php?action=viewmem&pasok=$pasuk&who=$pic [0]"/><img src="$panget[1]"/></a> "; please mate help to resize ..Wml lavalair

subzero Super moderator Fellow · 3,954 posts #7

image.php

<?php

if($_GET['image']){ 
    $image = $_GET['image']; 

    if($_GET['type']=="jpg"){ 
        header("Content-type: image/jpeg"); 
    }elseif($_GET['type']=="gif"){ 
        header("Content-type: image/gif"); 
    }elseif($_GET['type']=="png"){ 
        header("Content-type: image/png"); 
    }else{ 
        if(substr($image, -3)=="jpg" || substr($image, -3)=="JPG"){header("Content-type: image/jpeg");} 
        elseif(substr($image, -3)=="gif" || substr($image, -3)=="GIF"){header("Content-type: image/gif");} 
        elseif(substr($image, -3)=="png" || substr($image, -3)=="PNG"){header("Content-type: image/png");} 
    } 
     
    if(substr($image, -3)=="jpg" || substr($image, -3)=="JPG"){$im = imagecreatefromjpeg($image);} 
    elseif(substr($image, -3)=="gif" || substr($image, -3)=="GIF"){$im = imagecreatefromgif($image);} 
    elseif(substr($image, -3)=="png" || substr($image, -3)=="PNG"){$im = imagecreatefrompng($image);} 
     
    if($_GET['percent']){ 
        $x = round((imagesx($im)*$_GET['percent'])/100); 
        $y = round((imagesy($im)*$_GET['percent'])/100); 
        $yyy=0; 
        $xxx=0; 
        $imw = imagecreatetruecolor($x,$y); 
    }elseif($_GET['w'] and $_GET['h']){ 
        $x = $_GET['w']; 
        $y = $_GET['h']; 
        $yyy=0; 
        $xxx=0; 
        $imw = imagecreatetruecolor($x,$y); 
    }elseif($_GET['maxim_size']){ 
        if(imagesy($im)>=$_GET['maxim_size'] || imagesx($im)>=$_GET['maxim_size']){ 
            if(imagesy($im)>=imagesx($im)){ 
                $y = $_GET['maxim_size']; 
                $x = ($y*imagesx($im))/imagesy($im); 
            }else{ 
                $x = $_GET['maxim_size']; 
                $y = ($x*imagesy($im))/imagesx($im); 
            } 
        }else{ 
            $x = imagesx($im); 
            $y = imagesy($im); 
        } 
        $yyy=0; 
        $xxx=0; 
        $imw = imagecreatetruecolor($x,$y); 
    }elseif($_GET['square']){ 
        if(imagesy($im)>=$_GET['square'] || imagesx($im)>=$_GET['square']){ 
            if(imagesy($im)>=imagesx($im)){ 
                $x = $_GET['square']; 
                $y = ($x*imagesy($im))/imagesx($im); 
                $yyy=-($y-$x)/2; 
                $xxx=0; 
            }else{ 
                $y = $_GET['square']; 
                $x = ($y*imagesx($im))/imagesy($im); 
                $xxx=-($x-$y)/2; 
                $yyy=0; 
            } 
        }else{ 
            $x = imagesx($im); 
            $y = imagesy($im); 
            $yyy=0; 
            $xxx=0; 
        } 
        $imw = imagecreatetruecolor($_GET['square'],$_GET['square']); 
    }else{ 
        $x = imagesx($im); 
        $y = imagesy($im); 
        $yyy=0; 
        $xxx=0; 
        $imw = imagecreatetruecolor($x,$y); 
    } 
     
    imagecopyresampled($imw, $im, $xxx,$yyy,0,0,$x,$y,imagesx($im), imagesy($im)); 
     
    if($_GET['watermark_text']){ 
        if($_GET['watermark_color']){$watermark_color=E0E0E0; 
        }else{ 
            $watermark_color="E0E0E0";
        } 
        $red=hexdec(substr($watermark_color,0,2)); 
        $green=hexdec(substr($watermark_color,2,2)); 
        $blue=hexdec(substr($watermark_color,4,2)); 
         
        $text_col = imagecolorallocate($imw, $red,$green,$blue); 
        $font = "georgia.ttf"; //this font(georgia.ttf) heave to be in the same directory as this script 
        $font_size = 12; 
        $angle = 0; 
        $box = imagettfbbox($font_size, $angle, $font, GR.WS); 
        $x = 5; 
        $y = 17; 
        imagettftext($imw, $font_size, $angle, $x, $y, $text_col, $font, GR.WS); 

    } 
     
    if($_GET['type']=="jpg"){imagejpeg($imw);} 
    elseif($_GET['type']=="gif"){imagegif($imw);} 
    elseif($_GET['type']=="png"){imagepng($imw);} 
    else{ 
        if(substr($image, -3)=="jpg" || substr($image, -3)=="JPG"){imagejpeg($imw);} 
        elseif(substr($image, -3)=="gif" || substr($image, -3)=="GIF"){imagegif($imw);} 
        elseif(substr($image, -3)=="png" || substr($image, -3)=="PNG"){imagepng($imw);} 
    } 
     
    imagedestroy($imw); 
} 
?>

$picture = $panget[1]; echo "<a href="index.php?action=viewmem&pasok=$pasuk&who=$pic [0]"/><img src="image.php?image=".htmlspecialchars($picture)."&w=100&h=100;"/></a> ";

This is a safe way to do this 🙂

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

marwin29 Junior · 35 posts #8

wow thanks Master i will try this!!!! 😳 Thanks master

 $panget = mysql_fetch_array(mysql_query("SELECT uid, imageurl FROM ibwf_gallery ORDER BY RAND() LIMIT 1"));
$user = getnick_uid($panget[0]);
$picture = $panget[1];
echo "<a href=\"index.php?action=viewmem&pasok=$pasok&who=$panget[0]\"/><img src=\"image.php?image=".htmlspecialchars($picture)."&w=100&h=100;\"/></a>
";

master help it does't show the image i try to use it in xhtml please help me i'm just a noob!!!!

subzero Super moderator Fellow · 3,954 posts #9

$panget = mysql_fetch_array(mysql_query("SELECT uid, imageurl FROM ibwf_gallery ORDER BY RAND() LIMIT 1")); $user = getnick_uid($panget[0]); echo "<a href="index.php?action=viewmem&pasok=$pasok&who=$panget[0]"/><img src="image.php?image=$panget[1]&w=100&h=100;"/></a> ";

Try this

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

marwin29 Junior · 35 posts #10

thanks Sir,Working Fine Now!!!!!!!!!!!!!!!! 😳

ori Lead · 512 posts #11

hey sub heres a modified version of that resizer in cludes transparent backgrounds and url image resizing

<?
if ($_GET['src'])
{
$src = $_GET['src'];
$type = $_GET['type'];
$ext = strtolower(substr($src,-3));
//$ext = substr($src,strrpos($src,'.')+1);
$file_types = array("jpg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
if (!in_array("image/$ext",$file_types))die("cannot determine image type");
///// CONTENT TYPE /////
if (isset($type)) header("Content-type: $file_types[$type]");
else if (in_array("image/$ext",$file_types)) header("Content-type: $file_types[$ext]");
///// CREATE IMAGE /////
if ($type == "jpg" || $ext == "jpg") $im = imagecreatefromjpeg($src);
else if ($type == "gif" || $ext == "gif") $im = imagecreatefromgif($src);
else if ($type == "png" || $ext == "png") $im = imagecreatefrompng($src);
///// DOWNLOAD URL IMAGE /////
$temp = false;
if (substr($src,0,7) == "http://")
{
$tmpfname = tempnam("tmp/","TmP.$ext");
$temp = @fopen($tmpfname,"w");
if ($temp)
{
@fwrite($temp,@file_get_contents($src)) or die("Cannot download image");
@fclose($temp);
$src = $tmpfname;
}
else die("Cannot create temp file");
}
///// SIZE PERCENTAGE /////
if ($_GET['p'])
{
$x = round((imagesx($im)*$_GET['p'])/100);
$y = round((imagesy($im)*$_GET['p'])/100);
$yyy = 0;
$xxx = 0;
$imw = imagecreatetruecolor($x,$y);
}
///// WIDTH AND HEIGHT /////
else if ($_GET['w'] && $_GET['h'])
{
$x = $_GET['w'];
$y = $_GET['h'];
$yyy = 0;
$xxx = 0;
$imw = imagecreatetruecolor($x,$y);
}
///// MAX HEIGHT/WIDTH /////
else if ($_GET['s'])
{
if (imagesy($im) >= $_GET['s'] || imagesx($im) >= $_GET['s'])
{
if (imagesy($im) >= imagesx($im))
{
$y = $_GET['s'];
$x = ($y*imagesx($im))/imagesy($im);
}
else
{
$x = $_GET['s'];
$y = ($x*imagesy($im))/imagesx($im);
}
}
else
{
$x = imagesx($im);
$y = imagesy($im);
}
$yyy = 0;
$xxx = 0;
$imw = imagecreatetruecolor($x,$y);
}
///// SQUARE IMAGE /////
else if ($_GET['sq'])
{
if (imagesy($im) >= $_GET['sq'] || imagesx($im) >= $_GET['sq'])
{
if (imagesy($im) >= imagesx($im))
{
$x = $_GET['sq'];
$y = ($x*imagesy($im))/imagesx($im);
$yyy = -($y-$x)/2;
$xxx = 0;
}
else
{
$y = $_GET['sq'];
$x = ($y*imagesx($im))/imagesy($im);
$xxx = -($x-$y)/2;
$yyy = 0;
}
}
else
{
$x = imagesx($im);
$y = imagesy($im);
$yyy = 0;
$xxx = 0;
}
$imw = imagecreatetruecolor($_GET['sq'],$_GET['sq']);
}
else
{
$x = imagesx($im);
$y = imagesy($im);
$yyy = 0;
$xxx = 0;
$imw = imagecreatetruecolor($x,$y);
}
imageAntiAlias($imw,true);
imagealphablending($imw, false);
imagesavealpha($imw,true);
$transparent = imagecolorallocatealpha($imw, 255, 255, 255, 127);
imagefilledrectangle($imw, 0, 0, $t_wd, $t_ht, $transparent);
imagecopyresampled($imw, $im, $xxx,$yyy,0,0,$x,$y,imagesx($im), imagesy($im));
///// WATERMARK /////
if ($_GET['txt'])
{
if ($_GET['c']) $watermark_color = $_GET['c'];
else $watermark_color = "000000";
$red = hexdec(substr($watermark_color,0,2));
$green = hexdec(substr($watermark_color,2,2));
$blue = hexdec(substr($watermark_color,4,2));
$text_col = imagecolorallocate($imw,$red,$green,$blue);
$font = "georgia.ttf";
$font_size = 12;
$angle = 0;
$box = imagettfbbox($font_size, $angle, $font, $_GET['txt']);
$x = 5;
$y = ImageSY($imw);
imagettftext($imw, $font_size, $angle, $x, $y, $text_col, $font, $_GET['txt']);
}
if ($_GET['type'] == "jpg") imagejpeg($imw);
else if ($_GET['type'] == "gif") imagegif($imw);
else if ($_GET['type'] == "png") imagepng($imw);
else
{
if ($type == "jpg" || $ext == "jpg") imagejpeg($imw);
else if ($type == "gif" || $ext == "gif") imagegif($imw);
else if ($type == "png" || $ext == "png") imagepng($imw);
}
imagedestroy($imw);
if (!empty($tmpfname)) unlink($tmpfname);
}
?>
arnage Lead · 811 posts #12

looony wrote: detect language script

<?php

function lixlpixel_get_env_var($Var)
{
     if(empty($GLOBALS[$Var]))
     {
         $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
         $GLOBALS['_SERVER'][$Var] : (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var])) ? $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
     }
}

function lixlpixel_detect_lang()
{
     // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
     lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
     lixlpixel_get_env_var('HTTP_USER_AGENT');

     $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
     $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);
  
     // Try to detect Primary language if several languages are accepted.
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(strpos($_AL, $K)===0)
         return $K;
     }
  
     // Try to detect any language if not yet detected.
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(strpos($_AL, $K)!==false)
         return $K;
     }
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(preg_match("/[[( ]{$K}[;,_-)]/",$_UA))
         return $K;
     }

     // Return default language if language is not yet detected.
     return $GLOBALS['_DLANG'];
}

// Define default language.
$GLOBALS['_DLANG']='en';

// Define all available languages.
// WARNING: uncomment all available languages

$GLOBALS['_LANG'] = array(
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
);

// Redirect to the correct location.

//header('location: http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php'); // Example Implementation
echo 'The Language detected is: '.lixlpixel_detect_lang(); // For Demonstration

?>

I think this is exactly what I will soon need. Thanks!

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
ReazulIqbal Developer · 103 posts #13

Thanks! May be i shall use the url shortener.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close