Archive for tutorial

OOP Javascript

Already heard about Object Oriented Programming on Javscript, well !, now post it in here cos i think this very important, specially people who will concern about AJAX scripting. However know about OOP on Javascript will make you better on Javascript programing.

This is a very simple sample, how to write:

<script language="Javascript">


ProgKu={
pariabelsatu : "Javascript" ,
   parkedua : "" ,
   fungsiSatu :
      function(val){
         if ( ( (val * 5) % 2 ) == 0 ) {
            this.pariabelsatu = "Javascript hasil OOPJs";
            this.parkedua = "Genap" ;
            return true;
         }else{
            this.pariabelsatu = "Hasil gak bener";
            this.parkedua = "Ganjil" ;
            return true;
         }
      }
   fungsiDua :
      function(){
         var t = prompt("Masukan angka 1 hingga 1000");
         var myVal = parseInt(t);
         if(isNaN(myVal)){
            alert("Yang dimasukan bukan angka");
            return false;
         }else{
            this.fungsiSatu(myVal);
         }
      }
}


//Sudah diluar class
function cobaCoba(){
var h = ProgKu.fungsiDua();
document.getElementById("test").innerHTML =
h.pariabelsatu +" mungkin karena "+h.parkedua;
}

And on HTML please put this code :

<div id="test">Look at here</div>

OK, have nice try :p

Leave a Comment

Create watermark on the Fly

I come to office earlier now. At a momment i just read my schedule of what will i do for today…? Let me see. ooh God, I have job to create a script which can protect all photo in my project using Logo, and i guess that i have to create watermark script.

However, it’s very interisting to create it. OK let’s create it…!!! (And don’t forget to help me if i’m wrong.. ). And this is the code which i used in my script:


<?php
/*create watermark
*By Gunturris - gunturris#a.t#gmail.com
*End of Feb 2007
*$imagesorce is original image
*$imagelogo is logo to blending (I used PNG)
*$alpha is alpha color output for watermark */


function watermak_on_the_fly($imagesource, $imagelogo, $alpha=15 ){
header("content-type: image/png");
//Check image resourse
if(! file_exists($
imagesource)){
$im = @imagecreate(50, 150)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Img not found", $text_color);
imagepng($im);
return imagedestroy($im);
}


//Check watermark logo
if(! file_exists($imagelogo)){
$im = @imagecreate(50, 150)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Logo not found", $text_color);
imagepng($im);
return imagedestroy($im);
}


settype($alpha,"integer");
if($alpha > 100) $alpha=100;
if($alpha < 0 )$alpha = 0;


list($ori_width,$ori_height,$ori_type,$ori_attr)=getimagesize($
imagesource);
switch($type){
case 1 : $img = @imagecreatefromgif($
imagesource);break; //GIF
case 2 : $img = @imagecreatefromjpeg($
imagesource); break; //JPG
case 3 : $img = @imagecreatefrompng($
imagesource); break; //PNG
default : die("IMAGE NOT SUPPORTED!"); break;
}


$watermark = imagecreatefrompng( $imagelogo );
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);


@imagecopymerge($img, $watermark, 0, ($ori_height-$watermark_height-5), 0, 0, $watermark_width, $watermark_height, $alpha);


return $img;


}
?>

And to use just ‘do’ like this:

<?php
$test = watermak_on_the_fly("imagejpeg.jpeg", "logo.png",25);
@imagejpeg($test );
@imagedestroy($test);
?>

OK, I hope it’s works good, if not please tell me or please debug by your self. Cos in my computer is no problem :p

Comments (3)

Force-download use PHP

Wuih..
There’s long time for me do not write any to here.
It’s cause my job deadline.. hehehe…

Anyway, Now, I want to write about force-download on PHP.
Why i write this? Yeah, i just feel that this technique was good for share private download file(s), cos many my friend ask me if they add file in public_html it is not secure, and not private.

So please do like this:

/home/project/public_html/ //<<< this path for world
/home/project/content/ //<<< this for private share file

And every file will download is from private and if want to download must login 1st, and script will like this.

<?php
define("DOWNLOAD_PATH","/home/project/content/"); // location to download
class loginToDownload{


var $username = "guntur"; //User can get from DB
var $password = "test"; //Password can get from DB
var $file_to_download = "test.txt";

function authen($user,$pass,$filename){
if($user == $this->username and $pass == $this->password ){
//Get file
$filephysics = DOWNLOAD_PATH.$filename;
if( $this->download($filephysics) ) return true; //success download
}


return false; //failed to download
}


function download($fname){
if(! file_exists($fname) ) return false; //Checkin' file
$filesize = filesize($fname);
$filemimetype = mime_content_type($fname);
$exten = end( explode($fname,".") );


header('Content-type: '. $filemimetype );
header('Content-Disposition: inline ; filename="doload.'. $exten . '" ');
print(file_get_contents($fname));
exit;
}
}//end class
?>

And please try, and report if wrong, cos i don’t test it :D
OK, bro, have nice try…

Leave a Comment