PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The imagecopy()
function is one of the built-in functions in PHP, and it's used to copy a part of an image from one image to another. This function is part of the GD library, a powerful tool for creating and manipulating image files in a variety of different image formats, including GIF, PNG, JPEG, WBMP, and XPM.
Here's a basic example of how to use imagecopy()
:
// Load the source image $source = imagecreatefromjpeg('source.jpg'); // Create a new, blank image with the same dimensions as the source image $dest = imagecreatetruecolor(imagesx($source), imagesy($source)); // Copy the source image onto the destination image imagecopy($dest, $source, 0, 0, 0, 0, imagesx($source), imagesy($source)); // Output the new image header('Content-Type: image/jpeg'); imagejpeg($dest); // Free up memory imagedestroy($source); imagedestroy($dest);
In the above example, imagecreatefromjpeg('source.jpg')
is used to load an existing JPEG image from a file. Then, imagecreatetruecolor()
is used to create a new, blank image with the same dimensions as the source image.
The imagecopy()
function is then used to copy the source image onto the destination image. The parameters of imagecopy()
are as follows:
$dest
: The destination image link resource.$source
: The source image link resource.dest_x
and dest_y
: x and y coordinates in the destination image where the copy should start.src_x
and src_y
: x and y coordinates in the source image where the copy should start.src_w
and src_h
: width and height of the portion of the source image to copy.Finally, the header()
function is used to set the HTTP response header to "image/jpeg", and imagejpeg()
is used to output the new image.
Remember to always destroy the images you have created with imagedestroy()
to free up memory.
Please, make sure you have the GD library enabled in your PHP environment. If it's not, you can uncomment or add extension=gd
in your php.ini
file and restart your web server. If you're using a hosted environment, you might need to ask your hosting provider to do this.
Remember to replace 'source.jpg'
with the path to your actual source image. The above code should be run in a PHP file on a server, and it should be accessed through a web browser to see the result.
This is a very basic use of imagecopy()
. It can also be used to copy and paste smaller parts of images, or to copy from one part of an image to another, among other things.
PHP imagecopy()
watermark example:
<?php $image = imagecreatefromjpeg('original.jpg'); $watermark = imagecreatefrompng('watermark.png'); // Watermark position $x = 10; $y = 10; imagecopy($image, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark)); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); imagedestroy($watermark);
Watermarking images in PHP with transparency:
<?php $image = imagecreatefromjpeg('original.jpg'); $watermark = imagecreatefrompng('watermark.png'); // Watermark position $x = 10; $y = 10; imagecopy($image, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark)); header('Content-Type: image/png'); imagepng($image); imagedestroy($image); imagedestroy($watermark);
Adding text watermarks to images in PHP:
<?php $image = imagecreatefromjpeg('original.jpg'); $textColor = imagecolorallocate($image, 255, 255, 255); // White text $font = 5; $text = 'Watermark'; // Text position $x = 10; $y = 10; imagestring($image, $font, $x, $y, $text, $textColor); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image);
Image watermarking with PHP and TrueType fonts:
<?php $image = imagecreatefromjpeg('original.jpg'); $textColor = imagecolorallocate($image, 255, 255, 255); // White text $font = 'arial.ttf'; // TrueType font file $text = 'Watermark'; // Text position $x = 10; $y = 10; imagettftext($image, 12, 0, $x, $y, $textColor, $font, $text); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image);