What are some common functions in PHP for manipulating images, such as combining two images or creating watermarks?
When working with images in PHP, common functions for manipulating images include combining two images and creating watermarks. To combine two images, you can use the `imagecopy()` function to copy one image onto another. For creating watermarks, you can use the `imagecopy()` function to overlay a watermark image onto the original image.
// Combine two images
$source = imagecreatefromjpeg('image1.jpg');
$overlay = imagecreatefrompng('image2.png');
// Get the dimensions of the overlay image
$overlay_width = imagesx($overlay);
$overlay_height = imagesy($overlay);
// Copy the overlay onto the source image
imagecopy($source, $overlay, 0, 0, 0, 0, $overlay_width, $overlay_height);
// Output the combined image
header('Content-Type: image/jpeg');
imagejpeg($source);
// Create a watermark
$source = imagecreatefromjpeg('original_image.jpg');
$watermark = imagecreatefrompng('watermark.png');
// Get the dimensions of the watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// Copy the watermark onto the source image
imagecopy($source, $watermark, imagesx($source) - $watermark_width - 10, imagesy($source) - $watermark_height - 10, 0, 0, $watermark_width, $watermark_height);
// Output the watermarked image
header('Content-Type: image/jpeg');
imagejpeg($source);
Related Questions
- What are potential pitfalls when using move_uploaded_file function in PHP for file uploads?
- How can PHP code for adding news content be optimized to prevent errors and ensure smooth functionality?
- How can PHP developers troubleshoot issues when the data stream appears to be empty despite sending JSON data to the server?