Home > Article > Backend Development > How to use PHP to develop a simple picture filter function
How to use PHP to develop a simple picture filter function?
Introduction: In web development, adding image filters can add some interest to the user's visual experience. This article will introduce how to use PHP to develop a simple picture filter function and provide specific code examples.
$sourceImage = imagecreatefromjpeg("path/to/source/image.jpg");
$targetImage = imagecreatetruecolor(imagesx($sourceImage), imagesy($sourceImage)); $backgroundColor = imagecolorallocate($targetImage, 255, 255, 255); imagefill($targetImage, 0, 0, $backgroundColor);
imagefilter($sourceImage, IMG_FILTER_GRAYSCALE);
imagefilter($sourceImage, IMG_FILTER_NEGATE);
imagefilter($sourceImage, IMG_FILTER_GRAYSCALE); imagefilter($sourceImage, IMG_FILTER_COLORIZE, 100, 50, 0);
imagejpeg($targetImage, "path/to/target/image.jpg");
$sourceImage = imagecreatefromjpeg("path/to/source/image.jpg"); $targetImage = imagecreatetruecolor(imagesx($sourceImage), imagesy($sourceImage)); $backgroundColor = imagecolorallocate($targetImage, 255, 255, 255); imagefill($targetImage, 0, 0, $backgroundColor); imagecopy($targetImage, $sourceImage, 0, 0, 0, 0, imagesx($sourceImage), imagesy($sourceImage)); // 添加滤镜效果 imagefilter($sourceImage, IMG_FILTER_GRAYSCALE); // 或者:imagefilter($sourceImage, IMG_FILTER_NEGATE); // 或者:imagefilter($sourceImage, IMG_FILTER_GRAYSCALE); // imagefilter($sourceImage, IMG_FILTER_COLORIZE, 100, 50, 0); // 输出处理后的图片 imagejpeg($targetImage, "path/to/target/image.jpg"); // 释放图片资源 imagedestroy($sourceImage); imagedestroy($targetImage);
Summary: Through the above steps, we can use PHP to develop a simple picture filter function. In actual development, you can adjust filter effects or add more filter effects according to different needs. Using PHP and the GD library, you can easily perform various processing on images.
The above is the detailed content of How to use PHP to develop a simple picture filter function. For more information, please follow other related articles on the PHP Chinese website!