This article will introduce to you an article about adding gray transparency effect to pictures in PHP. I hope this method will be helpful to all students.
Principle:
1. First calculate the size of the original image
2. Create translucent images of the same size
3. Use the imagecopy() method to merge the newly created semi-transparent image with the original image
The code is as follows
代码如下 |
复制代码 |
/*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 ]]
?>
|
|
Copy code |
|
/*php adds gray transparency effect to the image*/
$imfile = './0.jpg';//Original image
$origim = imagecreatefromjpeg($imfile);//Create a new image from a JPEG file or URL
$w=imagesx($origim);//Width of original image
$h=imagesy($origim);//Height of original image
$newimg = imagecreatetruecolor($w, $h);//Returns an image identifier, representing a black image of size x_size and y_size. imagecreatetruecolor//
$color=imagecolorallocatealpha($newimg,0,0,0,75);//Assign color + alpha to an image; Same as imagecolorallocate(), but with an additional transparency parameter alpha, whose value ranges from 0 to 127 . 0 means completely opaque, 127 means completely transparent.
imagecolortransparent($newimg,$color);//Define a color as a transparent color
imagefill($newimg,0,0,$color);//Area filling;resource $image, int $x, int $y, int $color
imagecopy($origim,$newimg, 0,0, 0, 0,$w, $h);//Copy part of the image; the coordinates in the src_im image start from src_x, src_y, the width is src_w, and the height is part of src_h Copy to the positions of coordinates dst_x and dst_y in the dst_im image.
imagejpeg($origim, './2.jpg');//Output the image to the browser or file. ;resource $image [, string $filename [, int $quality ]]