失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > php 图片处理慢 php图片处理

php 图片处理慢 php图片处理

时间:2023-07-26 13:17:26

相关推荐

php 图片处理慢 php图片处理

直接绘制文字

步骤:

- 使用imagecreatetruecolor创建真彩色图像

- 使用imagecolorallocate,imagefill填充背景色

- 使用imagecolorallocate设置文字颜色

- 使用imagettfbbox获取文字所占空间,以此为前提根据具体推算出需要文字起始点

- 使用imagettftext添加文字

- 输出图片

- 注销资源

public function images(){

$image_width = 400;

$image_height = 400;

// 创建真彩色图像

$image = imagecreatetruecolor($image_width,$image_height);

// 创建背景颜色

$background_color = imagecolorallocate($image,255,255,255);

// 填充背景色

imagefill($image,0,0,$background_color);

// 文字颜色

$text_color = imagecolorallocate($image,0,0,0);

$string = "

和晋陵陆丞早春游望

独有宦游人,偏惊物候新。

云霞出海曙,梅柳渡江春。

淑气催黄鸟,晴光转绿蘋。

忽闻歌古调,归思欲沾巾。";

// 字体地址

$fontfile = ROOT_PATH."public/assets/fonts/fzltxh.ttf";

// 取得使用 TrueType 字体的文本的范围 可参考:/php/php-imagettfbbox-ref.html

$ttbox = imagettfbbox(20,0,$fontfile,$string);

// 获取x,y坐标,文字居中

$x = ($image_width - $ttbox[0]-$ttbox[2])/2;

$y = ($image_height - $ttbox[1]-$ttbox[5])/2;

// 添加文字

imagettftext($image,20,0,$x,$y,$text_color,$fontfile,$string);

// 输出文件

header("Content-type:image/jpeg");

imagejpeg($image);

// 释放资源

imagedestroy($image);

exit; //tp5 需要添加断点,否则输出base64

}

给图片加文字水印

步骤

- 使用getimagesize获取图片属性

- 使用imagecreatefrom+图片类型函数,创建画布背景

- 使用imagettftext添加文字水印

- 输出图片

- 注销资源

public function images(){

// 图片地址

$fielpath = ROOT_PATH."public/uploads/touming.png";

// 获取原图像的属性

list($img_width,$img_height,$img_type) = getimagesize($fielpath);

// getimagesize()函数返回type类型定义

$img_type_list = [1=>'GIF',2=>'JPEG',3=>'PNG',4=>'SWF'];

// 使用图片创建背景

$create_function = "imagecreatefrom".$img_type_list[$img_type];

$image = $create_function($fielpath);

// 设定图像的混色模式

// 关闭 alpha 渲染并设置 alpha 标志 函数详细介绍请参考:/manual/zh/function.imagesavealpha.php

imagealphablending($image,false);

imagesavealpha($image,true);

// 水印字符串

$string = "和晋陵陆丞早春游望 ";

// 水印颜色

$text_color = imagecolorallocate($image,255,23,23);

// 字体地址

$fontfile = ROOT_PATH."public/assets/fonts/fzltxh.ttf";

// 水印放置右下角

// 获取像素和字体大小之间的比例

$tt_box = imagettfbbox(16,0,$fontfile,$string);

$fontsize = (16*$img_width/3)/($tt_box[2]-$tt_box[0]);

$tt_box = imagettfbbox($fontsize,0,$fontfile,$string);

// 设置x起点

$x = $img_width*2/3;

// y起点

if ($tt_box[5] - $tt_box[1] > 0){

$y = $img_height - ($tt_box[5] - $tt_box[1]);

}else{

$y = $img_height + ($tt_box[5] - $tt_box[1]);

}

// 添加水印

imagettftext($image,$fontsize,0,$x,$y,$text_color,$fontfile,$string);

// 输出文件

header("Content-type:image/jpeg");

$put_function = "image".$img_type_list[$img_type];

$put_function($image);

// 释放资源

imagedestroy($image);

exit; //tp5 需要添加断点,否则输出base64

}

图片水印

步骤

- 创建背景图片

- 根据原图大小对水印图片进行缩放:imagecopyresampled

- imagecopy添加水印

public function images(){

// 图片地址

$fielpath = ROOT_PATH."public/uploads/touming.png";

$water_fielpath = ROOT_PATH."public/uploads/22.png";

// 获取原图像的属性

list($img_width,$img_height,$img_type) = getimagesize($fielpath);

// getimagesize()函数返回type类型定义

$img_type_list = [1=>'GIF',2=>'JPEG',3=>'PNG',4=>'SWF'];

// 使用图片创建背景

$create_function = "imagecreatefrom".$img_type_list[$img_type];

$image = $create_function($fielpath);

// 设定图像的混色模式

// 关闭 alpha 渲染并设置 alpha 标志 函数详细介绍请参考:/manual/zh/function.imagesavealpha.php

imagealphablending($image,false);

imagesavealpha($image,true);

// 获取水印图片属性

list($water_img_width,$water_img_height,$water_img_type) = getimagesize($water_fielpath);

// 根据原图设置水印图片的缩放

// 缩放后的宽度,缩放后的高度

$to_zoo_width = $img_width*0.2;

$to_zoo_height = $img_height*0.2;

// 等比例缩放图片

if ($water_img_width>$water_img_height){

$to_zoo_width = $water_img_width*($to_zoo_height/$water_img_height);

}else{

$to_zoo_height = $water_img_height*($to_zoo_width/$water_img_width);

}

// 创建防止缩放后的图片地址

$to_zoo_image = imagecreatetruecolor($to_zoo_width,$to_zoo_height);

// 使用图片创建背景

$create_water_function = "imagecreatefrom".$img_type_list[$water_img_type];

$water_image = $create_water_function($water_fielpath);

// 缩放图片 ps:裁剪和缩放是方法相同

$imagecopyresampled = imagecopyresampled($to_zoo_image,$water_image,0,0,0,0,$to_zoo_width,$to_zoo_height,$water_img_width,$water_img_height);

// 缩放成功后,设置水印

if ($imagecopyresampled){

$x = $img_width - $to_zoo_width - $img_width*0.001;

$y = $img_height - $to_zoo_height - $img_height*0.001;

imagecopy($image,$to_zoo_image,$x,$y,0,0,$to_zoo_width,$to_zoo_height);

// 输出文件

header("Content-type:image/jpeg");

$put_function = "image".$img_type_list[$img_type];

$put_function($image);

// 释放资源

imagedestroy($image);

exit; //tp5 需要添加断点,否则输出base64

}else{

echo "异常错误";

}

}

如果觉得《php 图片处理慢 php图片处理》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。