پاسخ : مجموعه کدهای بدرد بخور در php
فشرده سازی خودکار فایلهای CSS با PHP
کد PHP:
<?php
ob_start('compress');
header('Content-Type: text/css');
function compress($buffer) {
/* remove comments */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* remove tabs and newlines, etc. */
$buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer);
/* remove unnecessary whitspaces */
$buffer = preg_replace('#[\s]{2,}#', ' ', $buffer);
return $buffer;
}
/* your css files */
if(isset($_GET['css']) && file_exists($_GET['css'])) {
require_once $_GET['css'];
}
ob_end_flush();
?>
مثالی از نحوه استفاده :
کد PHP:
<link href="csscompress.php?css=style.css" rel="stylesheet" type="text/css"/>
پاسخ : مجموعه کدهای بدرد بخور در php
تولید خودکار لینک کوتاه برای URLهای شما
کد PHP:
function getTinyUrl($url) {
return file_get_contents('http://tinyurl.com/api-create.php?url=' . $url);
}
پاسخ : مجموعه کدهای بدرد بخور در php
بدست آوردن اندازه فایلها با واحد B و KB و MB و... بطور خودکار :
کد PHP:
<?php
function FormatSize($url, $remote = false, $precision = 2) {
$bytes = 0;
if(!$remote) {
if(file_exists($url)) {
$bytes = filesize($url);
}
}
else {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Not necessary unless the file redirects
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
return -1;
}
if (preg_match('#Content-Length: (\d+)#i', $data, $matches)) {
$bytes = trim($matches[1]);
}
}
settype($bytes, 'double');
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$len = count($units);
for ($i = 0; $bytes >= 1024 && $i < $len; $i++) {
$bytes /= 1024;
}
return round($bytes, $precision) . $units[$i];
}
/* Usage Samples:
echo '<p>' . FormatSize('images/logo.png') . '</p>' . PHP_EOL;
echo '<p>' . FormatSize('http://www.google.com/images/srpr/logo4w.png', true) . '</p>' . PHP_EOL;
*/
?>
این اسکریپت حتی سایز فایل سایتهای دیگه و حتی لینک فایلهای RapidShare و... رو هم میتونه استخراج کنه.
پاسخ : مجموعه کدهای بدرد بخور در php
درج Overlay و Watermark بر روی تصاویر:
کد PHP:
header('Content-type: image/png');
## Red :: 0 - 255
## Green :: 0 - 255
## Blue :: 0 - 255
## Alpha :: 0 - 127
$data = array(
'file_1' => 'image_1.jpg',
'file_2' => 'image_2.png',
'position_x' => 80,
'position_y' => 90,
'watermark' => 'MostafaEs'
);
$font = array(
'family' => 'fonts/agencyb.ttf',
'rotate' => 0,
'size' => 17,
);
// Image Size
list($width_1, $height_1) = getimagesize($data['file_1']);
list($width_2, $height_2) = getimagesize($data['file_2']);
// Image Create
$image_1 = imagecreatefromjpeg($data['file_1']);
$image_2 = imagecreatefrompng($data['file_2']);
// Frame Create
$photoFrame_1 = imagecreatetruecolor($width_1, $height_1);
$photoFrame_2 = imagecreatetruecolor($width_2, $height_2);
// Frame (1)
imagecopyresampled($photoFrame_1, $image_1, 0, 0, 0, 0, $width_1, $height_1, imagesx($image_1), imagesy($image_1));
// Frame (2)
$transparent = imagecolorallocatealpha($photoFrame_2, 0, 0, 0, 127);
imagefill($photoFrame_2, 0, 0, $transparent);
imagecopyresampled($photoFrame_2, $image_2, 0, 0, 0, 0, $width_2, $height_2, imagesx($image_2), imagesy($image_2));
// Copy Frame
imagecopy($photoFrame_1, $photoFrame_2, $data['position_x'], $data['position_y'], 0, 0, imagesx($photoFrame_2), imagesy($photoFrame_2));
// Image Watermark
$box = imagettfbbox($font['size'], $font['rotate'], $font['family'], $data['watermark']);
$colors = array(
'black' => imagecolorallocatealpha($photoFrame_1, 0, 0, 0, 0),
'white' => imagecolorallocatealpha($photoFrame_1, 255, 255, 255, 0),
'red' => imagecolorallocatealpha($photoFrame_1, 255, 0, 0, 75),
'green' => imagecolorallocatealpha($photoFrame_1, 0, 255, 0, 75),
'blue' => imagecolorallocatealpha($photoFrame_1, 0, 0, 255, 75),
'custom-1' => imagecolorallocatealpha($photoFrame_1, 209, 163, 104, 5)
);
$width = imagesx($photoFrame_1);
$height = imagesy($photoFrame_1);
$margin_top = $height-35;
$margin_right = $height-3;
$margin_left = $width-3;
$margin_bottom = 3;
imagefilledrectangle($photoFrame_1, $margin_left, $margin_right, $margin_bottom, $margin_top, $colors['custom-1']);
imagettftext($photoFrame_1, $font['size'], $font['rotate'], ($width-$box[4])/2, ($height-10), $colors['black'], $font['family'], $data['watermark']);
imagepng($photoFrame_1);
imagedestroy($image_1);
imagedestroy($image_2);
imagedestroy($photoFrame_1);
imagedestroy($photoFrame_2);
آرایه data رو میبایست دستی ویرایش کنید! دراینجا تصویر اول JPG و تصویر دوم PNG هستش که میتونید بنابه نیازتون توابع رو تغییر بدید!
دراصل کار اصلی این کد درج تصویر دوم بصورت Transparent بر روی تصویر اول هست!
پاسخ : مجموعه کدهای بدرد بخور در php
تابعی مفید برای اینکد کردن url ها.
وقتی مفیده که بخواین مثلا از rss که لینک هاش حاوی کلمات فارسی هست، url صفحه ای رو گرفته سپس اون صفحه رو با cUrl بخواین باز کنید.
کد PHP:
function safe_urlencode($txt){
// Skip all URL reserved characters plus dot, dash, underscore and tilde..
$result = preg_replace_callback("/[^-\._~:\/\?#\\[\\]@!\$&'\(\)\*\+,;=]+/",
function ($match) {
// ..and encode the rest!
return rawurlencode($match[0]);
}, $txt);
return ($result);
}
پاسخ : مجموعه کدهای بدرد بخور در php
با استفاده از این تکه کد می تونید تاریخ و ساعت رو به شکل های ۲ دقیقه پیش ، ۱ ساعت و ۱۰ دقیقه پیش نمایش بدید
کد PHP:
public function Timeago($time,$format='Y/m/d H:i:s'){
$now=time();
if(is_string($time) && strlen(intval($time))!=10){
$time=strtotime($time);
}
$ago='پیش';
if($now<$time){
$diff=$time-$now;
$ago='بعد';
}else $diff=$now-$time;
if($diff<30) $output='همین حالا';
else if($diff>=30 && $diff<60) $output=$diff.' ثانیه '.$ago;
else if($diff>=60 && $diff<120) $output='کمتر از '.($diff/60).' دقیقه'.$ago;
else if($diff>=120 && $diff<3600){
$min=intval($diff/60);
$sec=intval($diff-($min*60));
$output=$min.' دقیقه'.($sec!=0?' و '.$sec.' ثانیه ':' ').$ago;
}else if($diff>=3600 && $diff<(24*3600)){
$h=intval($diff/3600);
$min=intval(($diff-($h*3600)) / 60);
$output=$h.' ساعت'.($min!=0?' و '.$min.' دقیقه ':' ').$ago;
}else if($diff>=(24*3600) && $diff<(2*24*3600)){
if($ago=='پیش') $output='دیروز';
else $output='فردا';
$output.=' ساعت '.date('H:i:s',$time);
}else $output=date($format,$time);
return $output;
}
طرز استفاده هم به این صورت هست شما زمان رو به عنوان پارامتر می فرستید و تابع خروجی مناسب رو برمی گردونه
مثال
ورودی -> 1378461081 ====> خروجی -> دیروز ساعت 14:21:21
ورودی -> strtotime('-5 mins') ====> خروجی -> ۵ دقیقه پیش
ورودی -> strtotime('+5 mins') ====> خروجی -> ۵ دقیقه بعد
پاسخ : مجموعه کدهای بدرد بخور در php
با استفاده از کد زیر میتوانیم هر فایلی را تا تعداد999 تکه splitکنیم. در ضمن این کد قابلیت rejoinفایل های split شده را دارد.
کد PHP:
<pre class="brush: php;" style="direction:ltr;">$filename = "http://www.iyinet.com/my-big-file.zip";
$targetfolder = '/tmp';
$piecesize = 10; // splitted file size in MB
$buffer = 1024;
$piece = 1048576*$piecesize;
$current = 0;
$splitnum = 1;
if(!file_exists($targetfolder)) {
if(mkdir($targetfolder)) {
echo "Created target folder $targetfolder".br();
}
}
if(!$handle = fopen($filename, "rb")) {
die("Unable to open $filename for read! Make sure you edited filesplit.php correctly!".br());
}
$base_filename = basename($filename);
$piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
if(!$fw = fopen($piece_name,"w")) {
die("Unable to open $piece_name for write. Make sure target folder is writeable.".br());
}
echo "Splitting $base_filename into $piecesize Mb files ".br()."(last piece may be smaller in size)".br();
echo "Writing $piece_name...".br();
while (!feof($handle) and $splitnum < 999) {
if($current < $piece) {
if($content = fread($handle, $buffer)) {
if(fwrite($fw, $content)) {
$current += $buffer;
} else {
die("filesplit.php is unable to write to target folder. Target folder may not have write permission! Try chmod +w target_folder".br());
}
}
} else {
fclose($fw);
$current = 0;
$splitnum++;
$piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
echo "Writing $piece_name...".br();
$fw = fopen($piece_name,"w");
}
}
fclose($fw);
fclose($handle);
echo "Done! ".br();
exit;
function br(){
return (!empty($_SERVER['SERVER_SOFTWARE']))?'':"\n";
}
پاسخ : مجموعه کدهای بدرد بخور در php
با استفاده از این کد می تونید خروجی اکسل بگیرید :
کد PHP:
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
//*********************************************
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
//*********************************************
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
//*********************************************
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
//*********************************************
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header("Content-Disposition: attachment;filename=Xsl-Ghorbani.xls ");
header("Content-Transfer-Encoding: binary ");
xlsBOF();
//*********************************************
xlsWriteLabel(0,0,"test test test test test");//خروجی اکسل برای بچه های گل برنامه نویس
//*********************************************
// Make column labels. (at line 3)
xlsWriteLabel(2,1,"First Name");
xlsWriteLabel(2,2,"Last Name");
xlsWriteLabel(2,3,"E-mail");
xlsWriteLabel(2,4,"Phone");
xlsWriteLabel(2,5,"Message");
xlsWriteLabel(2,6,"B First Name");
xlsWriteLabel(2,7,"B Last Name");
xlsWriteLabel(2,8,"B Phone");
xlsWriteLabel(2,9,"B E-mail");
xlsWriteLabel(2,10,"B Stuff");
$xlsRow = 3;
//************while($row=mysql_fetch_array($result)) {
xlsWriteLabel($xlsRow,1,"Reza");////$row['fname']
xlsWriteLabel($xlsRow,2,"Ghorbani");
xlsWriteLabel($xlsRow,3,"php_seo@yahoo.com");
xlsWriteLabel($xlsRow,4,"09119171500");
xlsWriteLabel($xlsRow,5,"Berid halesho bebarid");
xlsWriteLabel($xlsRow,6,"bfname");
xlsWriteLabel($xlsRow,7,"blname");
xlsWriteLabel($xlsRow,8,"btel");
xlsWriteLabel($xlsRow,9,"bemail");
xlsWriteLabel($xlsRow,10,"bstuff");
$xlsRow++;
////////////////////// }
xlsEOF();
exit();
پاسخ : مجموعه کدهای بدرد بخور در php
کد کردن و دیکد کردن
کد PHP:
function encryptDecrypt($key, $string, $decrypt)
{
if($decrypt)
{
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");
return $decrypted;
}else{
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
return $encrypted;
}
}
طریقه استفاده :
کد PHP:
echo encryptDecrypt('password', 'encrypt-decrypt this',0);
پاسخ : مجموعه کدهای بدرد بخور در php
تولید رشته تصادفی
کد PHP:
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
طریقه استفاده :
کد PHP:
echo generateRandomString(20);