Found 1060 Articles for PHP

How to create a new true-color image in PHP using imagecreatetruecolor()?

Urmila Samariya
Updated on 09-Aug-2021 11:39:48

1K+ Views

imagecreatetruecolor( ) is an inbuilt function in PHP that is used to create a new true-color image. It returns a blank image of the given size.Syntaxresource imagecreatetruecolor($width, $height)Parametersimagecreatetruecolor() takes two paramters, $width and $height.$width − The $width parameter is used to set the image width.$height − The $height parameter is used to set the image height.Return Valuesimagecreatetruecolor() returns an image resource identifier on success or it returns false on errors.Example 1OutputExample 2 − Below PHP code will create a new GD image streamOutput

How to crop an image to the given rectangle using imagecrop() function using PHP?

Urmila Samariya
Updated on 09-Aug-2021 11:08:50

3K+ Views

imagecrop() is an inbuilt function in PHP that is used to crop an image to the given rectangle. It crops the image from the given rectangle area and returns the output image.The given image is not modified.Syntaxresource imagecrop ($image, $rect)Parametersimagecrop() takes two parameters, $image and $rect.$image − It is the parameter returned by the image creation functions, such as imagecreatetruecolor(). It is used to create the size of an image.$rect − The cropping rectangle is an array with keys X, Y, width, and height.Return Valuesimagecrop() returns the cropped image resource on success or it returns false on failure.ExampleOutputInput image before ... Read More

How to crop an image automatically using imagecropauto() function in PHP?

Urmila Samariya
Updated on 09-Aug-2021 10:12:07

1K+ Views

imagecropauto() is an inbuilt function in PHP that is used to crop an image automatically using one of the available modes.Syntaxresource imagecropauto(resource $image, int $mode, float $threshold, int $color)Parametersimagecropauto() takes four different parameters − $image, $mode, $threshold and $color.$image − Specifies the image resource to be cropped.$mode − It is an optional parameter and it is used to specify an integer corresponding to a crop mode, below is the list of crop modes.IMG_CROP_DEFAULT − The IMG_CROP_DEFAULT works just like the IMG_CROP_TRANSPARENT mode.IMG_CROP_TRANSPARENT − This mode is used to crop out a transparent background.IMG_CROP_BLACK − This mode is used to crop ... Read More

How to destroy an image in PHP using imagedestroy() function?

Urmila Samariya
Updated on 09-Aug-2021 10:05:23

639 Views

imagedestroy() is an inbuilt PHP function that is used to destroy an image and free any memory associated with the image.Syntaxbool imagedestroy(resource $image)Parametersimagedestroy() takes only one parameter, $image. It holds the name of an image.Return Valuesimagedestroy() returns true on success and failure on false.Example 1 − Destroying an image after loading it.OutputNote − By using imagedestroy() function, we have destroyed the $cropped variable and therefore, it can no longer be accessed.Explanation − In Example1, imagecreatefrompng() loads an image from the local drive folder and crops a part of the image from the given image using imagecropauto() function. After cropping, imagedestroy() ... Read More

How to create a new image from a WBMP file or URL using imagecreatefromwbmp() function in PHP?

Urmila Samariya
Updated on 09-Aug-2021 09:58:16

258 Views

In PHP, imagecreatefromwbmp() is an inbuilt function that is used to create a new image from a WBMP file or URL. imagecreatefromwbmp() returns an image identifier representing the image obtained from the given filename. We can use imagecreatefromwbmp() whenever we want to edit the images after loading them from a WBMP file. Using the imagewbmp() function, an image can be converted into WBMP.Syntaxresource imagecreatefromwbmp(string $filename)Parametersimagecreatefromwbmp() takes only one parameter, $filename. It holds the name of the image.Return Valuesimagecreatefromwbmp() returns an image resource identifier on success, and it gives an error on false.Example 1

How to create a new image from a PNG file or URL using the imagecreatefrompng() function in PHP?

Urmila Samariya
Updated on 09-Aug-2021 09:54:10

2K+ Views

In PHP, imagecreatefrompng() is an inbuilt function that is used to create a new image from a PNG file or URL. imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.Syntaxresource imagecreatefrompng(string $filename)Parametersimagecreatefrompng() takes only one parameter, $filename. This parameter holds the name of the image or path to the PNG image.Return Valuesimagecreatefrompng() returns an image resource identifier on success, and it gives an error on false.Example 1 − Showing the loaded PNG image in the browserOutputExample 2 − Loaded and saving PNG image in the local drive pathOutputExplanation − In Example 2, a png image is ... Read More

How to create a new image from a JPEG file using the imagecreatefromjpeg() function in PHP?

Urmila Samariya
Updated on 09-Aug-2021 09:48:33

3K+ Views

imagecreatefromjpeg() is an inbuilt function in PHP that is used to create a new image from a JPEG file. It returns an image identifier representing the image obtained from the given filename.Syntaxresource imagecreatefromjpeg(string $filename)Parametersimagecreatefromjpeg() uses only one parameter, $filename, that holds the name of the image or path to the JPEG image.Return Valuesimagecreatefromjpeg() returns an image resource identifier on success, and it gives an error on false.Example 1OutputExample 2Input ImageOutput ImageExplanation − In Example 2, we loaded the jpeg image from the local path using the imagecreatefromjpeg() function. Thereafter, we used the imageflip() function to flip the image.Example 3 − ... Read More

PHP 8 – How to use ValueError to check if the value encountered is of correct type?

Urmila Samariya
Updated on 01-Apr-2021 06:57:44

553 Views

PHP 8 uses a new built-in exception ValueError. PHP throws this exception when we pass a value to a function, which has a valid type but cannot be used for operation. In the earlier versions of PHP, we used to get a Warning error in such cases, but PHP 8 will show a ValueError.Example: ValueError in PHP 8OutputFatal error: Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be emptyExample Live DemoOutputbool(false)Example: ValueError in PHP 8OutputFatal error: Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be empty

How to get resource ID using get_resource_id() function in PHP and PHP 8?

Urmila Samariya
Updated on 01-Apr-2021 06:58:38

311 Views

The resource is a type of variable that holds a reference to an external resource. The resource can be a filehandle, a database connection, or a URL handle. Every resource is identified by a unique id. In the previous versions of PHP, we needed to cast a resource to int to get the resource id.Example: get_recource_id using int.Output1In PHP 8, the get_resource_id() function always returns an int. It is used to get the ID for a given resource. This function always guarantees type safety.Example: Using get_recource_id in PHP 8.Output1

How the Non-Capturing Exception Catches work in PHP 8?

Urmila Samariya
Updated on 01-Apr-2021 06:50:36

307 Views

In the previous versions of PHP, if we wanted to catch an exception, then we needed it to store in a variable to check whether that variable is used or not.Before PHP 8, to handle the exception catch block, we needed to catch the exception (thrown by the try block) to a variable.Example: Capturing Exception Catches in PHPExplanation − In the above program, the exception is being caught by the catch block to a variable $e. Now the $e variable can hold any information about the exception as code, message, etc.PHP 8 introduced non-capturing catches. Now, it is possible to ... Read More

Advertisements