Showing posts with label PHP Functions. Show all posts
Showing posts with label PHP Functions. Show all posts

Thursday 1 October 2015

imagecreatefrompng - Create a new image from file or URL - PHP

imagecreatefrompng - Create a new image from file or URL - PHP

resource imagecreatefrompng ( string $filename )
Create a new image from file or URL
imagecreatefrompng() returns an image identifier representing the image obtained from the given filename

<?php

function LoadPNG($imgname)
{
    
/* Attempt to open */
    
$im = @imagecreatefrompng($imgname);

    
/* See if it failed */
    
if(!$im)
    {
        
/* Create a blank image */
        
$im  imagecreatetruecolor(15030);
        
$bgc imagecolorallocate($im255255255);
        
$tc  imagecolorallocate($im000);

        
imagefilledrectangle($im0015030$bgc);

        
/* Output an error message */
        
imagestring($im155'Error loading ' $imgname$tc);
    }

    return 
$im;
}
header('Content-Type: image/png');$img LoadPNG('bogus.image');imagepng($img);imagedestroy($img);?>


PHPで画像を縮小してアップロードする
Make sure GD Library is enabled and "PNG Support" is must enabled in GD Library.

Wednesday 30 September 2015

count_chars - Count Number of character of string with PHP

count_chars - Count Number of character of string with PHP

mixed count_chars ( string $string [, int $mode = 0 ] )
Return information about characters used in a string
Mode value can have 0-5 and return following results
  1. 0 - Return an array with the byte-value as key and the frequency of every byte as value.
  2. 1 - same as 0 but only byte-values with a frequency greater than zero are listed.
  3. 2 - same as 0 but only byte-values with a frequency equal to zero are listed.
  4. 3 - a string containing all unique characters is returned.
  5. 4 - a string containing all not used characters is returned.



Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.

<?php
$data 
"Two Ts and one F.";

foreach (
count_chars($data1) as $i => $val) {
   echo 
"There were $val instance(s) of \"" chr($i) , "\" in the string.\n";
}
?>


065 PHP من الألف إلى الياء Arabic التعامل مع النصوص Strings pt1

Tuesday 7 July 2015

Check if string contains specific words - PHP

Check if string contains specific words - PHP


strpos: Find the position of the first occurrence of a substring in a string.

strpos have 3 parameter and are following:
  1. haystack:The string to search in.
  2. needle:If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
  3. offset: It is optional, If provided then search will start this number of characters counted from the beginning of the string.
  4. The offset cannot be negative.

Note: It is case sensitive.


Different Examples of strpos
var_dump(strpos('web technology experts','web'));//int(0)
var_dump(strpos('web technology experts','technology'));//int(4)
var_dump(strpos('web technology experts','Technology'));//int(4)
var_dump(strpos('web technology experts','technology',4));//int(4)
var_dump(strpos('web technology experts','technology',5));//bool(false)
var_dump(strpos('web technology experts','expertss',6));//bool(false)


Saturday 20 June 2015

New features of PHP 5.5 with Examples


New features of PHP 5.5 with Examples


Following are 5.5 new features.

  1. Generators: has been added via the yield keyword.
    Generators provide an easy way to implement simple iterators.
    For Example:
     function xrange($start, $limit, $step = 1) {
        for ($i = $start; $i <= $limit; $i += $step) {
            yield $i;
        }
      }
      
      echo 'List of odd Numbers:';
      
      foreach (xrange(1, 9, 2) as $number) {
          echo "$number ";
      }
      
  2. finally: keyword added in try catch.
    In try catch, finally block will be called every time, regardless of whether an exception has been thrown or not.
    For Example:
        
        try {
        //write code here 
      }
      catch (Exception $e) {
          //Exception comes here
      }
      finally {
          echo "I am called everytime";
      } 
  3. New password hashing API
    Password hashing API that makes it easier to securely hash and manage passwords using the same crypt() in PHP has been added.
  4. foreach now supports list()
    Now in PHP5.5, You can use list function inside foreach function.
    See Example
     $listArray = [
        [10, 20],
        [30, 40],
        [50, 60],
    ];
    
    foreach($listArray as list($a, $b)) {
        echo "A: $a; B: $b";
    } 
  5. empty() supports expressions
    Now you can use expression in empty function.
    For Example:
      if(!empty(2*3)){
        echo "Its working";
        }
  6. Array and String literal dereferencing.
    String and Array can now be dereferenced directly to access individual elements OR characters.
    For Example
    echo 'Array dereferencing: ';
      echo [10, 02, 30][0];//10
      echo "\n";
      
      echo 'String dereferencing: ';
      echo 'Web Technology'[0]; //W
        
  7. Class name resolution via ::class
    ClassName::class to get a fully qualified name of class ClassName.
    For Example
    namespace Foo;
      class Bar {}
       
      echo Bar::class;
  8. OPcache extension added
    The Zend Optimiser+ opcode cache has been added to PHP5.5 as the new OPcache extension.
  9. foreach now supports non-scalar keys. For non-scalar keys, an Iterator must be used as they cannot occur in native PHP array.
  10. Apache 2.4 handler supported on Windows
  11. Improvements to GD Library.

Friday 19 June 2015

New features of PHP 5.4 with Examples



Following are 5.4 new features.

  1. Traits has been added. For Example:
     //Create traits
    trait HttpRequest 
    { 
          public function getHttpResponse($url, $port) 
          { 
    
                return $response; 
          } 
    } 
    
    
    //Use the traits
    
    class ABC_API 
    { 
          use HttpRequest; // use our trait 
    
          public function getResponse($url) 
          { 
                return $this->getHttpResponse($url, 80); 
          } 
    } 
  2. Short array syntax has been added. For Example:
    $array = [1,2,3,'Web technology','Experts Notes'];
  3. Function array dereferencing has been added. For Example:
    function fruits(){
    return array(0=>'Apple', 1=>'Banana', 2=>'Mango',3=>'Orange');
    }   
    echo fruits()[2]; //Mango 
  4. Closures now support $this.
  5. Class member can access on instantiation has been added. For Example:
    class test{
      function fruits(){
      return array(0=>'Apple', 1=>'Banana', 2=>'Mango',3=>'Orange');
      }
    }
    print_r((new test())->fruits());
    
    Output:
    Array
    (
        [0] => Apple
        [1] => Banana
        [2] => Mango
        [3] => Orange
    )
  6. Class::{expr}() syntax is now supported. For Example:
    class test{
    static  function newMethod(){
    echo "called function";
    }
    }  
    
    $method = 'newMethod';
    // this works 
    test::$method(); 
    
    // and this works 
    test::{'newMethod'}();
    
  7. Binary number format has been added, e.g. 0b001001101.
  8. Improved parse error messages and improved incompatible arguments warnings.
  9. The session extension can now track the upload progress of files.
  10. Built-in development web server in CLI mode.




Following functions are deprecated in PHP 5.4.

  1. mcrypt_generic_end()
  2. mysql_list_dbs()