• WP shortcode to php output

    functions.php
    function jv($atts){
    $atts = shortcode_atts( array(
    'file' => ''
    ), $atts, 'akcija' );
    ob_start();
    include(ABSPATH.'wp-content/akcija/'.$atts['file'].'.php');
    $content = ob_get_clean();
    return $content;
    }
    add_shortcode('akcija', 'jv');


  • Saglābāt array datubāzē

    <?
    $array = array("foo", "bar", "hello", "world");
    $conn=mysql_connect('localhost', 'mysql_user', 'mysql_password');
    mysql_select_db("mysql_db",$conn);
    $array_string=mysql_escape_string(serialize($array));
    mysql_query("insert into table (column) values($array_string)",$conn);
    ?>
    <?
    $conn=mysql_connect('localhost', 'mysql_user', 'mysql_password');
    mysql_select_db("mysql_db",$conn);
    $q=mysql_query("select column from table",$conn);
    while($rs=mysql_fetch_assoc($q))
    {
    $array= unserialize($rs['column']);
    print_r($array);
    }
    ?>


  • Moved Permanently

    if ($_SERVER[HTTP_HOST]!="www.name.lv"){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.name.lv");
    }


  • png uz jpg ar izgriezšanas funkciju

    function png2jpg($originalFile, $outputFile, $quality, $crop, $crop2, $cirpt) {
    $image = imagecreatefrompng($originalFile);
    if ($crop>0){
    $size = getimagesize($originalFile);
    $width = $size[0];
    $height = $size[1];
    $ww=$crop;
    $hh=$crop2;
    if ($cirpt==1){
    if($width> $height) {
    $x=$width/$ww;
    } else {
    $x=$height/$hh;
    }
    $thumb_size1=ceil($width/$x);
    $thumb_size2=ceil($height/$x);
    } else if ($cirpt==2){
    $thumb_width = $crop;
    $thumb_height = $crop2;
    $width = $size[0];
    $height = $size[1];
    $original_aspect = $width / $height;
    $thumb_aspect = $thumb_width / $thumb_height;
    if ( $original_aspect >= $thumb_aspect )
    {
    // If image is wider than thumbnail (in aspect ratio sense)
    $new_height = $thumb_height;
    $new_width = $width / ($height / $thumb_height);
    }
    else
    {
    // If the thumbnail is wider than the image
    $new_width = $thumb_width;
    $new_height = $height / ($width / $thumb_width);
    }
    $thumb_size1=$crop;
    $thumb_size2=$crop2;
    $x1 = 0 - ($new_width - $thumb_width) / 2;
    $y1 = 0 - ($new_height - $thumb_height) / 8;
    $x=0;
    $y=0;
    } else {
    $new_width = $size[0];
    $new_height = $size[1];
    $width = $size[0];
    $height = $size[1];
    }
    $new_im = ImageCreatetruecolor(($thumb_size1),$thumb_size2);
    imagecopyresampled($new_im,$image,$x1,$y1,$x,$y,$new_width,$new_height,$width,$height);
    imagejpeg($new_im, $outputFile, $quality);
    imagedestroy($new_im);
    } else {
    imagejpeg($image, $outputFile, $quality);
    imagedestroy($image);
    }
    }


  • curl post pieprasījums

    curl_setopt($handle, CURLOPT_URL,"http://www.example.lv/kautkas.php");
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, array("var1" => $var1 ));
    curl_exec($handle);


  • json no php masīva

    $json[]=array('artist'=>'var1','title'=>$var2));
    echo json_encode($json);


  • Youtube ID no dziesmas nosaukuma

    $homepage = file_get_contents('https://gdata.youtube.com/feeds/api/videos?q='.urlencode($dz_izpilditajs).'%20-%20'.urlencode($dz_nosaukums).'&orderby=relevance&start-index=1&max-results=1&v=2&alt=json');
    $ytraw=json_decode($homepage, true);
    $here = explode(":", $ytraw['feed']['entry'][0]['id']['$t']);
    $num = (count($here) - 1);
    $yt = $here[$num];

    Vairāk info šeit:
    https://developers.google.com/youtube/2.0/developers_guide_protocol_api_query_parameters?hl=cs


  • Krāsu kods no STRING

    No STRING ģenerē HTML krāsas kodu

    function stringToColorCode($str) {
    $code = dechex(crc32($str));
    $code = substr($code, 0, 6);
    return $code;
    }


  • Attēla kvadrāta thumbnail izgriezšana PHP

    function LoadJpeg($imgname,$izmers)
    {
    $im = @imagecreatefromjpeg($imgname);
    if(!$im)
    {
    $im = imagecreatetruecolor(150, 30);
    $bgc = imagecolorallocate($im, 255, 255, 255);
    $tc = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
    imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }
    $size = getimagesize($imgname);
    $width = $size[0];
    $height = $size[1];
    if($width> $height) {
    $x = ceil(($width - $height) / 2 );
    $width = $height;
    } else{
    $y = ceil(($height - $width) / 2);
    $height = $width;
    }
    $thumb_size1=$izmers;
    $thumb_size2=$izmers;

    $new_im = ImageCreatetruecolor(($thumb_size1),$thumb_size2);
    imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size1,$thumb_size2,$width,$height);

    return $new_im;
    }


  • Login with Facebook – tas patiesībā ir graujoši vienkārši

    Pirmā koda daļa nodrošina pliku pieslēgšanos.
    Atliek vien noreģistrēt savu websaitu “facebook apps” un iegūt APP_ID, APP_SECRET utt.
    <fb:login-button autologoutlink="true" style="display:table-cell;padding:3px;" ></fb:login-button>
    <p><fb:like></fb:like></p>
    <div id="fb-root"></div>
    <script>
    window.fbAsyncInit = function() {
    FB.init({appId: '235xxxxxxxxxxxxxxx', status: true, cookie: true,
    xfbml: true});
    };
    (function() {
    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol +
    '//connect.facebook.net/lv_LV/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
    }());
    </script>

    Lai tiktu pie lietotāja Facebook datiem… spēlējamies tālāk…

    <?php
    define('YOUR_APP_ID', '235xxxxxxxxxxxxxxx');
    define('YOUR_APP_SECRET', 'c9fb5xxxxxxxxxxxxxxxxxxxxxxxxx');

    function get_facebook_cookie($app_id, $app_secret) {
    $args = array();
    parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
    ksort($args);
    $payload = '';
    foreach ($args as $key => $value) {
    if ($key != 'sig') {
    $payload .= $key . '=' . $value;
    }
    }
    if (md5($payload . $app_secret) != $args['sig']) {
    return null;
    }
    return $args;
    }

    $cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);

    $user = json_decode(file_get_contents(
    'https://graph.facebook.com/me?access_token=' .
    $cookie['access_token']));

    ?>
    <?php if ($cookie) { ?>
    Welcome <?= $user->name ?>
    <?php print_r($user); ?>
    <?php } else { ?>
    <fb:login-button></fb:login-button>
    <?php } ?>
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
    FB.init({appId: '<?= YOUR_APP_ID ?>', status: true,
    cookie: true, xfbml: true});
    FB.Event.subscribe('auth.login', function(response) {
    window.location.reload();
    });
    </script>