search
Homephp教程php手册php实现给图片加灰色半透明效果的方法,

php实现给图片加灰色半透明效果的方法,

本文实例讲述了php实现给图片加灰色半透明效果的方法。分享给大家供大家参考。具体实现方法如下:

原理:

1.首先计算出原图片的尺寸
2.创建相同尺寸的半透明图片
3.使用 imagecopy()方法,把新创建的半透明图片与原图合并

具体实现代码如下:

复制代码 代码如下:

/*php 给图片加灰色透明效果*/
$imfile = './0.jpg';//原图
$origim = imagecreatefromjpeg($imfile);//从 JPEG 文件或 URL 新建一图像
 
$w=imagesx($origim);//原图宽度
$h=imagesy($origim);//原图高度
 
$newimg = imagecreatetruecolor($w, $h);//返回一个图像标识符,代表了一幅大小为    x_size 和 y_size 的黑色图像。imagecreatetruecolor//     
 
$color=imagecolorallocatealpha($newimg,0,0,0,75);//为一幅图像分配颜色 + alpha; 和 imagecolorallocate() 相同,但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明。 
 
imagecolortransparent($newimg,$color);//将某个颜色定义为透明色
 
imagefill($newimg,0,0,$color);//区域填充;resource $image , int $x , int $y , int $color 
 
imagecopy($origim,$newimg, 0,0, 0, 0,$w, $h);//拷贝图像的一部分;将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
 
imagejpeg($origim, './2.jpg');//输出图象到浏览器或文件。;resource $image [, string $filename [, int $quality ]]
?>

希望本文所述对大家的PHP程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor