Tag Archives: php tutorial for beginners with examples

Add Watermark On Image in PHP


Add Watermark On Image in PHP- It is very easy and simple to add watermarks on images in PHP. GD library and GD functions are used for adding watermarks on image in PHP. Here in this tutorial we are going to cover two things – 1. Add Text as watermark. 2. Add Image as Watermark.


How to Add Watermark On Image in PHP?

You can add the watermark on images in following ways –

Add Text As Watermark

Three functions are used to add the text as watermark in php –

  • imagecreatetruecolor– Returns an image identifier representing a black image of the specified size.
  • imagecopy– Used to copy image.
  • imagettftext– This is used to write the texts to the image using TrueType fonts.

Syntax

Here is the syntax for adding the text watermark on image.

Text Watermark : imagettftext() Syntax


imagettftext() function is main function which is responsible for adding the text watermark on the image. This function accepts the following parameters-

  • $imageResource– This is obtained from the function imagecreatetruecolor().
  • $fontsize– This is used to define the font size of the watermark text.
  • $waterMarkAngle– This is used to define the watermark angle.
  • $x, $y– $x is used to define the distance of the watermark from X axis. $y is used to define the distance from Y axis.
  • $fontFilePath– This is used to add the font file path. If font file is not there then download the font file – Download Arial Font
  • $watermarkText– Watermark text to be added.
  • $color– This is used to add the watermark font color.

Text Watermark On Image : Example 1 –

Let us pass the font color white, angle 0 degree and font size 30px.

Add Text As Watermark On Image Example:


Try it »

If you run the above example it will produce the output something like this –

Add Watermark On Image in PHP

Text Watermark on Image : Example 2 –

Let us pass the font color white, angle 45 degree and font size 40px.

Add Text As Watermark On Image PHP Script Example:


Try it »

If you run the above example it will produce the output something like this –

Add Watermark On Image diagonal php

Add image as Watermark

Apart from texts you can also add image as watermarks. Sometimes we need some image to be used as watermark such as logo of company on each images. Php imagecopy() function is enough to add image watermarks on another image. You can add the image watermarks simply as below –

Syntax

Syntax for image watermark is as –

Text Watermark : imagettftext() Syntax


The above function is enough for adding the image watermark on another image.
The imagecopy function copy the source Image($srcImage) onto the destination image($dstImage). Input parameter of imagecopy() function are as –

  • $dstImage– The main image on which you want to add the watermark.
  • $srcImage– The image used for watermark.
  • $dstX, $dstY– The x, y co-ordinates of destination image.
  • $srcX , $srcY– The x, y co-ordinates of the source image.
  • $srcW , $srcH– The height & width of the image to be copied. The image will be copied with a height of $srcW & Height of $srcH from the co-ordinates $srcX, $srcW And will be added onto the destination image’s co-ordinate $x, $y.

Add Image Watermark : Example –

Let us add watermark simply using the imagecopy() function as below- .

Add image As Watermark On Image PHP Script Example:


Try it »

The above example will add the source image onto the destination image. If you run the above example it will produce the output something like this –

Php Force to download a file


Php Force to download a file : Sometimes we need to download file forcefully. Here in this tutorial we are going to explain how you can force browser to download the files.


Php Force to download a file

You can use below method to download a file in php as below –

Php Force to download a file Example:

$file = "http://testdomain.com/file.exe"; 
header("Content-Description: File Transfer"); 
header("Content-Type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=\"$file\""); 
readfile ($file); 

The above example will download the file from the specified location.

Php Delete an element from array


Php Delete an element from array – You can remove array element using the unset($position) function. It deletes the element at the specified position. Here in this tutorial we are going to explain how you can delete an element form an array. We will explain this with example and demo.


Php Delete an element from array

You can remove an element from array using the unset function as below –

Php Delete an element from array Example:


Try it »

If you run the above example it will produce output something like this-

Php Delete an element from array


More Examples

Let’s have look over more example and demo here.


Php delete element from array by value

You can delete the array element by value as below –

Php delete element from array by value Example:

'2',"c"=>'1',"d"=>'4',"e"=>'9');
print_r($array);
$delete_value = 4;
if(($key = array_search($delete_value, $array)) !== false) {
    unset($array[$key]);
}
echo "

After Deleting

"; print_r($array); ?>

Try it »

If you run the above example it will produce output something like this-

Php delete element from array by value Example:

Php delete element from array by key

You can delete the array element by key as below –

Php delete element from array by key Example:

'2',"c"=>'1',"d"=>'4',"e"=>'9');
print_r($array);
$key = 'c';
    unset($array[$key]);
echo "

After Deleting

"; print_r($array); ?>

Try it »

If you run the above example it will produce output something like this-

Php delete element from array by value Example:

The requested PHP extension ext-intl * is missing from your system


The requested PHP extension ext-intl * is missing from your system : While installing magento2 via composer this error occurred. This error also appears when installing Cakephp 3. You can fix this problem by following the simple steps as below.


The requested PHP extension ext-intl * is missing from your system

Here is the screen shot of composer command –

The requested PHP extension ext-intl * is missing from your system

Steps to fix the problem –

  • Open php.ini located at – /xampp/php/php.ini
  • Uncomment ;extension=php_intl.dll ie. remove semocolon so it will be – extension=php_intl.dll
  • Restart apache from Xampp control panel.

The above solution will fix our problem.

Php save image from url


Php save image from url : There are many ways to save images from the the given url. file_get_contents and file_put_contents can be used to save images from the given url. For using the file_get_contents allow_url_fopen should be set true. Here in this tutorial we are going to explain how you can save an image from the url.


Php save image from url

Here are the methods you can use to save the image from given url –

Php save image from url : Using file_get_contents

If allow_url_fopen is set to true you can save image from given url as below –

Php save image from url : Using file_get_contents


$url = 'http://example.com/test-image.jpg';
$content = file_get_contents($url);
file_put_contents("/folder/myimage.jpg", $content);

Suppose that your image is on given url like – ‘http://example.com/test-image.jpg’ and you want to download it. First get the content of image using the function file_get_contents($url) where url is image full url. Now to save this image use the function file_put_contents(“/folder/myimage.jpg”, $content); where ‘/folder/myimage.jpg’ is folder name where you want to save the image with the name ‘myimage.jpg’ and $content contains the image content taken from the url.

Php save image from url : Using curl

You can save image from given url using the curl as below –

Php save image from url : Using Curl


$url = 'http://example.com/test-image.jpg';
$ch = curl_init($url);
$fp = fopen('/folder/myimage.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

The above example will save the image in folder with the name ‘myimage.jpg’.

Note : For using curl make sure the curl is enabled on your server.

Php remove first character from string


Php remove first character from string : Sometimes in few cases we need to remove first character from string in php. There are many ways to remove first character from string. Here in this tutorial we are going to explain the simplest method with example and demo.


Php remove first character from string Syntax & Example

You can use substr() function to remove the first character from string as below –

Php remove first character from string : Remove First Letter Using substr


Try it »

If you run the above example it will produce the output something like this –

Php remove first character from string


More Example :

Let us have some more examples –


Remove first character from string : Using ltrim

If you know the first character, you can remove the string as below using the ltrim function-

Php remove first character from string : Using substr


Try it »

Remove first character from string : Using Regular Expression

You can remove first letter using the regular expression also as below –

Php remove first character from string : Using Regular Expression Example


Try it »

If you run the above example it will produce the output something like this –

Remove first character from string : Using Regular Expression

Php Convert object to Associative Array


Php Convert object to Associative Array : We sometimes need to convert the php objects to associative array. An Object is converted to array which contains the keys that are member variables and values are object’s property. Here in this tutorial we are going to explain how you can convert php objects as associative array.


Php Convert object to Associative Array

Let us create a simple php object

Create Php Object : Example

firstName = "John";
$object->lastName = "Dee";

?>

Php objects are accessed in php as below-

Create Php Object : Example

firstName;
echo $object->lastName;

?>

The above example will give you the first name and last name.

Now let us convert the above php object in associative array –

Convert PHP object to Associative Array Example

Php Convert object to Associative Array : Example

firstName = "John";
$object->lastName = "Dee";

$array = (array)$object;
echo $array['firstName']."
"; echo $array['lastName']."
"; ?>

Try it »

$array[‘firstName’] give you the first name and $array[‘lastName’] will give you last name as associative key pair value. If you run the above example it will produce the following output –

Php Convert object to Associative Array

Exception : Some complex object such as private, protected variable are not converted to array properly using this method.

Php Read file line by line


Php Read file line by line : For large files we can’t load the full file in memory at a time which will cause memory exhaust errors. To read large file programmatically we should always follow the line by line approach. If you working with an application which has large file in GB or TB or more than that always try to read the files line by line. Here in this tutorial we are going to explain how you can read a big file line by line without causing the memory exhaust error problem.


Php Read file line by line : Read Large File

You can read a large file line by line as below –

Php Read Large file line by line : Example

$file = fopen("bigfile.txt", "r");
if($file){
// if the file is opened successfully 
while(!feof($file)){
    $line = fgets($file);
    //perform action with $line
}
fclose($file);
}else{
  echo "Error while opening the file.";
}

The function feof() is used to get the line no from the file. The above example will open the file and read the file bigfile.txt line by line.

Note : feof() gets the line no from the file pointer. For more detail about the function feof() you can refer – fgets() function.

Remove Last Character From String in Php


Remove Last Character From String in Php : We sometimes need to remove last character from string such as comma(,), semicolon(;) etc. We can remove last character from string in php using the function substr(). There are many ways to remove last character from string we are going to explain some of them with example and demo.


Remove Last Character From String in Php

You can remove the last character using the following – method 1

Remove Last Character From String Using substr() Method 1

Using the substr() function we can remove the last character as

Remove Last Character From String in Php: Remove Last Comma(,)


Try it »

If you run the above example it will produce the following output –

Test

Remove Last Character From String Using rtrim() – Method 2

Using the rtrim() function we can remove the last character as

Remove Last Character From String in Php: Remove Last Comma(,)


Try it »

rtrim($str, “,”) – first parameter is string and second parameter is character you want to remove. rtrim() function removes the character from the right of string if the character is specified otherwise it will remove blank space if found. If you run the above example it will produce the following output –

Test