برگزیده های پرشین تولز

★★★★ تاپیک جامع سورس کدهای PHP

MOHAMMAD026_1

مدیر بخش ویژه وب مستر
مدیر انجمن
تاریخ عضویت
16 ژوئن 2014
نوشته‌ها
5,051
لایک‌ها
8,680
محل سکونت
ROOT
تابع register_shutdown_function
در تابع register_shutdown_function() شما میتونید تعیین کنید که قبل از پایان اجرا اسکریپت تون، چه تابعی اجرا بشه!
به این مثال توجه کنید:
PHP:
$start_time = microtime(true);

register_shutdown_function('my_shutdown');

function my_shutdown() {
    global $start_time;
    echo "execution took: ". (microtime(true) - $start_time). " seconds.";
}

یا
PHP:
function shutdown()
{
    // This is our shutdown function, in
    // here we can do any last operations
    // before the script is complete.

    echo 'Script executed with success', PHP_EOL;
}

register_shutdown_function('shutdown');
 

Hasam

Registered User
تاریخ عضویت
2 اکتبر 2007
نوشته‌ها
665
لایک‌ها
159
محل سکونت
flynic.ir
جستجوی گوگل
گاهی اوقات هست که نمایش نتایج جستجوی گوگل نیاز هست. برای این کار گوگل یک API رایگان ارائه داده، اما محدودیت‌های خاص خودش رو داره.

PHP:
$search_query = 'linux';

$data = file_get_contents('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $search_query);
$data = json_decode($data);

print 'Google search results for "' . $search_query . ':<hr />';

foreach ($data->responseData->results as $item)
    print 'Title: ' . $item->title . '<br />URL: ' . $item->url . '<br />' . $item->content . '<hr />';

محدودیت‌ها:
در صورت چند request پی‌در‌پی با API بالا ممکن هست به جای نتایج پیامی با محتوای abuse یا suspected دریافت کنید. برای حل این مشکل از قسمت "گوگل کد" و در قسمت API ها لازم هست با گوگل اکانت لاگین کرده و یک key برای استفاده از این API دریافت کنید.
یا این حال با استفاده از key دریافت شده، فقط اجازه‌ی ارسال 100 کوئری در روز وجود داره. برای استفاده‌ی بیشتر لازم هست پرداختی داشته باشید (1000 کوئری 5$).
 

soroush_tayyebi

Registered User
تاریخ عضویت
15 می 2009
نوشته‌ها
513
لایک‌ها
110
سن
32
محل سکونت
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
سن
38
تابع تبدیل حروف عجق وجق که با &# شروع میشوند مثل رشته &#127; به حروف فارسی
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
نوشته‌ها
513
لایک‌ها
110
سن
32
محل سکونت
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
نوشته‌ها
513
لایک‌ها
110
سن
32
محل سکونت
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);
}
 
بالا