Image Manipulation in PHP

 Image Manipulation in PHP


PHP can be used to manipulate images by resizing, cropping, and adding effects to them. The built-in GD library provides functions to perform image manipulation operations.

Example:


php


// resize an image

$filename = "image.jpg";

$percent = 0.5;

list($width, $height) = getimagesize($filename);

$newwidth = $width * $percent;

$newheight = $height * $percent;

$image = imagecreatetruecolor($newwidth, $newheight);

$source = imagecreatefromjpeg($filename);

imagecopyresized($image, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($image, "resized_image.jpg");


// add a watermark

$filename = "image.jpg";

$watermark = "watermark.png";

$watermark_width = 200;

$watermark_height = 100;

$image = imagecreatefromjpeg($filename);

$watermark = imagecreatefrompng($watermark);

$dest_x = imagesx($image) - $watermark_width - 10;

$dest_y = imagesy($image) - $watermark_height - 10;

imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

imagejpeg($image, "watermarked_image.jpg");

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...