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

Tuesday, 2 June 2015

PHP: Image to base64 string

<title>Image to Base64 String</title>
<fieldset>
    
<legend>Image to Base64 String</legend>
        
<center>
        
<form name="select_all">
                
 <?php 
                
/** 
 *   This code will help you to learn how we can convert an image into a base64 string!! 
 */ 
                    
echo"<h3><p>Image</p></h3>"; 
                    
//$file = File Image yang ingin di encode  
                    //Filetype: JPEG,PNG,GIF 
                    
$file "encode.jpg"; 
                    if(
$fp fopen($file,"rb"0)) 
                    { 
                    
$gambar fread($fp,filesize($file)); 
                    
fclose($fp); 

                     
                    
$base64 chunk_split(base64_encode($gambar)); 
                    
//Result 
                    
$encode '<img src="data:image/jpg/png/gif;base64,' $base64 .'" >'; 
                    echo 
$encode; 
                    }     
                
?> 

                
<br><textarea name="text_area" rows="20" cols="70">
 <? echo $encode?> </textarea>
                
<p><input type="button" value="Select All Code" onClick="javascript:this.form.text_area.focus();this.form.text_area.select();"></p>
        
</form>
        
</center>
    
</fieldset>

Thursday, 25 September 2014

chunk_split in PHP

 PHP chunk_split() function specified string is divided into a series of smaller chunks.

Syntax :


chunk_split(string,length,end)
 
Parameters Description:
 
string : Required. Defines the input string
length : Optional. Defines length of chunks. Default is 76.
end : Optional. Defines the line ending sequence. Default is \r\n.
Note: PHP chunk_split() function does not modify the original string.

Example:


<?php 
echo "<strong>here we will split the string after each character and add a (. dot) after each split :<strong><br />";
$str_name = "Good Morning!";
echo chunk_split($str_name,1,".")."<br />";
echo "<strong>here we will split the string after the sixth character and add (...) after each split :</strong><br />";
$str_name = "Good Morning!";
echo chunk_split($str_name,5,"..."); 
?>

Output will be:


here we will split the string after each character and add a (. dot) after each split :
G.o.o.d. .M.o.r.n.i.n.g.!. 
here we will split the string after the sixth character and add (...) after each split :
Good ...Morni...ng!...