منتخب بازارچه

تکه کد های پی اچ پی (Code Snippets)

shahapaya

کاربر تازه وارد
تاریخ عضویت
14 آگوست 2016
نوشته‌ها
34
لایک‌ها
6
سن
34
بسیار عالی کار میکنه.
 

soroush_tayyebi

Registered User
تاریخ عضویت
15 می 2009
نوشته‌ها
511
لایک‌ها
110
سن
28
محل سکونت
Tehran
با سلام مجدد
تبدیل HEX به RGB :

PHP:
function hex2rgb( $colour ) {
        if ( $colour[0] == '#' ) {
                $colour = substr( $colour, 1 );
        }
        if ( strlen( $colour ) == 6 ) {
                list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
        } elseif ( strlen( $colour ) == 3 ) {
                list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
        } else {
                return false;
        }
        $r = hexdec( $r );
        $g = hexdec( $g );
        $b = hexdec( $b );
        return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}
 

shahapaya

کاربر تازه وارد
تاریخ عضویت
14 آگوست 2016
نوشته‌ها
34
لایک‌ها
6
سن
34
تابع تبدیل حروف عجق وجق که با &# شروع میشوند مثل رشته  به حروف فارسی
PHP:
function charset_decode_utf_8 ($string) {
    /* Only do the slow convert if there are 8-bit characters */
    /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */
    if (!preg_match("/[\200-\237]/", $string)
     && !preg_match("/[\241-\377]/", $string)
    ) {
        return $string;
    }

    // decode three byte unicode characters
    $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",
        "'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",
        $string
    );

    // decode two byte unicode characters
    $string = preg_replace("/([\300-\337])([\200-\277])/e",
        "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",
        $string
    );

    return $string;
}
 

soroush_tayyebi

Registered User
تاریخ عضویت
15 می 2009
نوشته‌ها
511
لایک‌ها
110
سن
28
محل سکونت
Tehran
با سلام خدمت همه‌ی دوستان

توسط تکه کد زیر می‌تونید سایز عکس رو تغییر بدید. لازم به ذکره این تیکه کد فقط برای تست هستش و توی پروژه‌ی واقعی استفاده نکنیدش همینجوری :)

PHP:
<?php
   ob_start();
   function calculate_width_height($width, $height,$width_needed, $height_needed)
   {
      if ($width <= $width_needed and $height <= $height_needed)
      {

         $l_new_width=$width;
         $l_new_height=$height;
      }
      else
      {
         if ($height > $height_needed)
         {
            $l_percentage_height =$height_needed/($height/100);
            $l_new_width=($width/100)*$l_percentage_height;
            $l_new_height=$height_needed;
            if ($l_new_width > $width_needed)
            {

               $l_percentage_width =$width_needed/($width/100);
               $l_new_height=($height/100)*$l_percentage_width;
               $l_new_width=$width_needed;
            }
         }
         else
         {
            $l_percentage_width =$width_needed/($width/100);
            $l_new_height=($height/100)*$l_percentage_width;
            $l_new_width=$width_needed;
            if ($l_new_height > $height_needed)
            {
               $l_percentage_height =$height_needed/($height/100);
               $l_new_width=($width/100)*$l_percentage_height;
               $l_new_height=$height_needed;
            }
         }
      }
   $calculated_width_height[] = round($l_new_width);
   $calculated_width_height[] = round($l_new_height);
   return $calculated_width_height;
   }
   function resize($filename, $dest, $width, $height, $type=''){
       if(@imagecreatefromgif($filename)){
           $img=imagecreatefromgif($filename);
           $type_r='gif';
       }
       elseif(@imagecreatefromjpeg($filename)){
           $img=imagecreatefromjpeg($filename);
           $type_r='jpg';
       }
       elseif(@imagecreatefrompng($filename)){
           $img=imagecreatefrompng($filename);
           $type_r='png';
       }
       elseif(@imagecreatefromwbmp($filename)){
           $img=imagecreatefromwbmp($filename);
           $type_r='bmp';
       }
       else{
           return 'Cannot open file';
       }
       $type=($type=='')?$type_r:'';
       $img_n= @ imagecreatetruecolor ($width, $height);
       @ imagecopyresampled($img_n, $img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));
       if($type=='gif'){
           imagegif($img_n, $dest);
       }
       elseif($type=='jpg'){
          @ imagejpeg($img_n, $dest);
       }
       elseif($type=='png'){
          @ imagepng($img_n, $dest);
       }
       elseif($type=='bmp'){
          @ imagewbmp($img_n, $dest);
       }
       Return true;
   }
   function upload_file($root_dir,$file_name,$file_name_tmp,$lc_width,$lc_height)
   {
      $lc_max_filesize=2001000;
      $filesize=filesize($file_name_tmp);
      $sub_server=substr($_SERVER["HTTP_REFERER"],7,9);
      $uploaddir = "/get_on_top/image_resize/uploads";
      if (
         !file_exists($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) ||
         2 != (fileperms($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) & 2) ||
         1 != (fileperms($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) & 1)
         )
      {
         $error="tets";
      }
      $fileext = substr($file_name, -4);
      if (strtolower($fileext) != ".jpg" and strtolower($fileext) != ".gif")
      {
         print "<b>For security reasons only the uploading of .jpg and .gif files is allowed.</b><br>\n";
         print "<a href=\"javascript:history.back();\">Click here to go back.</a><br>";
         exit;
      };
      $loc = $uploaddir."/".$file_name;
      $fullloc = $root_dir.$loc;
      $upfile = $file_name_tmp;
      if ($filesize>$lc_max_filesize)
      {
         $lc_message= "File is too big, may not be bigger then 2MB";
      }
      else
      {
         if(move_uploaded_file($upfile, $fullloc))
         {
            chmod($fullloc, 0644);
         }
         else
         {
            die("Error moving \"$upfile\" to \"$fullloc\".");
         }
         $test =filesize($fullloc);
      }
      list($original_width, $original_height, $type, $attr) = getimagesize($fullloc);
      list($l_new_width,$l_new_heigth) = calculate_width_height($original_width, $original_height,$lc_width,$lc_height);
      resize($fullloc, $root_dir.$uploaddir."/".$lc_width."_".$lc_height."_".$file_name, $l_new_width, $l_new_heigth);
   $upload_info[] = $lc_message;
   $upload_info[] = $original_width;
   $upload_info[] = $original_height;
   $upload_info[] = $l_new_width;
   $upload_info[] = $l_new_heigth;
   return $upload_info;
   }
?>
<body>
<?php
   $root_dir =$HTTP_SERVER_VARS["DOCUMENT_ROOT"];
   if ($_POST["resize_submit"] == "resize")
   {
      $lc_height           =$_POST["height"];
      $lc_width            =$_POST["width"];
      $day = getdate();
      $minutes = $day['minutes'];
      $temp=gettimeofday();
      $sec=(int)$temp["sec"];
      if (!empty($_FILES["userfile"]["name"]))
      {
         $fileext = substr($_FILES["userfile"]["name"], -4);
         $ffile_name="1_".$minutes."_".$sec.$fileext;
         $tfile_name_tmp=$_FILES["userfile"]["tmp_name"];
         list($lc_upload_message,$original_width,$original_height,$l_new_width,$l_new_height) = upload_file($root_dir,$ffile_name,$tfile_name_tmp,$lc_width,$lc_height);
         if ($lc_upload_message!="")
         {
            echo "error_message".$lc_upload_message;
         }
      }
      ?>
      <form ENCTYPE="multipart/form-data"  action="resize_image.php" method="post">
      <table>
      <tr><td>height              :</td><td><input type="TEXT" name="height" size="10" value="<?php echo $lc_height ; ?>"></td></tr>
      <tr><td>width               :</td><td><input type="TEXT" name="width"  size="10" value="<?php echo $lc_width  ; ?>"></td></tr>
      <tr><td>image to upload     :</td><td><INPUT NAME="userfile" TYPE="file"></td></tr>
      <tr><td><input type="submit" name="resize_submit" value="resize"></td>
      </form>
      </table>
      <table>
      <tr>
      <td><IMG border=0 src="uploads/<?php echo $ffile_name ; ?>" ></td>
      <td  valign=bottom><IMG border=0 src="uploads/<?php echo $lc_width.'_'.$lc_height.'_'.$ffile_name ; ?>" ></td>
      </tr>
      <tr>
      <td><?php echo "org_height:".$original_height." org_width:".$original_width; ?></td>
      <td><?php echo "new_height_".$l_new_height." new_width_".$l_new_width; ?></td>
      </tr>
      </table>
      <?php
   }
   else
   {
      ?>
      <form ENCTYPE="multipart/form-data"  action="resize_image.php" method="post">
      <table>
      <tr><td>height              :</td><td><input type="TEXT" name="height" size="10" ></td></tr>
      <tr><td>width               :</td><td><input type="TEXT" name="width"  size="10" ></td></tr>
      <tr><td>image to upload     :</td><td><INPUT NAME="userfile" TYPE="file"></td></tr>
      <tr><td><input type="submit" name="resize_submit" value="resize"></td>
      </form>
      </table>
      <?php
   }
?>
</body>
</html>
<?php
ob_end_flush();
?>
 

soroush_tayyebi

Registered User
تاریخ عضویت
15 می 2009
نوشته‌ها
511
لایک‌ها
110
سن
28
محل سکونت
Tehran
با سلام

در این تکه کد پی اچ پی از یک فایل به اسم Mandrill.php استفاده شده که می‌تونید از طریق این لینک دانلود کنیدش.

PHP:
function send_email($to_email,$subject,$message1)
{
require_once 'Mandrill.php';
$apikey = 'XXXXXXXXXX'; //specify your api key here
$mandrill = new Mandrill($apikey);

$message = new stdClass();
$message->html = $message1;
$message->text = $message1;
$message->subject = $subject;
$message->from_email = "[email protected]";//Sender Email
$message->from_name = "KOONK";//Sender Name
$message->to = array(array("email" => $to_email));
$message->track_opens = true;

$response = $mandrill->messages->send($message);
}
 

soroush_tayyebi

Registered User
تاریخ عضویت
15 می 2009
نوشته‌ها
511
لایک‌ها
110
سن
28
محل سکونت
Tehran
سلام
ساخت و حذف جدول در یک دیتابیس:
PHP:
<?php
 
switch ($action){
    case "":
        display_ops();
        break;
    case "dis_delete":
        display_delete();
        break;
    case "delete":
           del_table($tname);
        break;
    case "dis_create":
        display_create();
        break;
    case "create":
        create($tblName,$cnf);
        break;
}

/******* Edit the values below ***********/
function connect_mySQL() {
    $dB = mysql_connect("mySQLHost", "user", "password") or die("Connection to server Failed!");
    return $dB;
    }
function connect_dB($dB) {
    $conn=mysql_select_db("dBName",$dB) or die("Connection to dB Failed!");
    return $conn;
}
/************ Stop Edit ********************/
 
function dis_header() {
        print "
        <html>
        <head>
            <title>Table Management</title>
        </head>
        <body>
        ";
}
 
function dis_footer() {
    print "
    <hr><center><a href='tableMng.php'>Main</a></center>
    </body>
    </html>
    ";
}
 
function display_ops() {
    dis_header();
    print "
    <p align='center'>
    <a href='tableMng.php?action=dis_create'>Create Table</a>&nbsp;&nbsp;
    <a href='tableMng.php?action=dis_delete'>Delete Table</a>
    </p>
    ";
    dis_footer();
}
 
function display_delete() {
            dis_header();
            print "
            <p align='center'>
            <form name='delete_table' method='post' action='tableMng.php'>
            <p>Table name: <input type='text' name='tname'>&nbsp;&nbsp;&nbsp;
            <input type='hidden' name='action' value='delete'>
            <input type='submit' value='Delete'>
            </form>
            </p>
            ";
        dis_footer();
}
 
 
function del_table($tname) {
    $dB = connect_mySQL();
    $conn= connect_dB($dB);
    $SQL="drop table $tname";
    $result=mysql_query($SQL, $dB) or die("Table deletion Failed!<BR><hr><center><a href='tableMng.php'>Main</a></center>");
    dis_header();
    print "Table "$tname" was deleted";
    dis_footer();
}
 
function get_cnfVal() {
$cnfVal="*********** Example ***********
ItemSKU VARCHAR(25) NOT NULL,
ItemName VARCHAR(100) NOT NULL,
ItemDescription MEDIUMTEXT NOT NULL,
ItemImgThumbnail VARCHAR(100) NOT NULL,
ItemImgUrl VARCHAR(100) NOT NULL,
ItemCost DECIMAL(7,2) NOT NULL,
Category BIGINT NOT NULL,
ShippingCost DECIMAL(6,2) NOT NULL,
ItemID BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (ItemID)";
return $cnfVal;
}
 
function display_create() {
    dis_header();
    $cnfVal=get_cnfVal();
    print "
    <form name='tableName' method='post' action='tableMng.php'>
    <p align='center'>Table name<BR> <input type='text' name='tblName'><BR>
    Fields<BR>
    <textarea name='cnf' cols='50' rows='15'>$cnfVal</textarea><BR>
    <input type='hidden' name='action' value='create'>
    <input type='submit' value='Create'>
    ";
    dis_footer();
}
 
function create($tblName,$cnf) {
    $dB = connect_mySQL();
    $conn= connect_dB($dB);
    $SQL="CREATE TABLE $tblName ($cnf)";
    $result=mysql_query($SQL, $dB) or die("Failed to create table "$tblName"!<BR><hr><center><a href='tableMng.php'>Main</a></center>");
    dis_header();
    print "Table "$tblName" was succesfuly created!";
    dis_footer();
}
?>
 
بالا