课程名称:PHP进阶篇-GD库图像处理
课程章节:第3章 图像常用操作
主讲老师:king
课程内容:
今天学习的内容包括:
优化缩略图操作
缩略图的封装及测试
文字水印效果的实现
课程收获:
我的环境是 php 8.19 nts版本
缩略图优化操作实现流程 缩略图的函数 文字水印效果实现
<?php
header('content-type:text/html;charset=utf-8');
ini_set('date.timezone','Asia/Shanghai');
//optimization 缩略图优化操作
$filename='images/ipad.png';
if($fileInfo=getimagesize($filename)){
list($src_w,$src_h)=$fileInfo;
$mime=$fileInfo['mime'];
}else{
exit('文件不是真实图片文件!');
}
$createFun=str_replace('/','createfrom',$mime);
//echo $createFun;exit;
$outFun=str_replace('/','',$mime);//不能用null
//echo $outFun;exit;
//等比例缩放 设置最大宽高
$dst_w=300;
$dst_h=600;
$ratio_orig=$src_w/$src_h;
if($dst_w/$dst_h>$ratio_orig){
$dst_w=$dst_h*$ratio_orig;
}else{
$dst_h=$dst_w/$ratio_orig;
}
//创建原画布资源和目标画布资源
//$src_image=imagecreatefromjpeg($filename);
$src_image=$createFun($filename);
$dst_image=imagecreatetruecolor($dst_w,$dst_h);
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
//imagejpeg($dst_image,'thumbs/test_thumb.jpg');
$outFun($dst_image,'thumbs/test1_thumb.jpg');
imagedestroy($src_image);
imagedestroy($dst_image);
/**
* 形成缩略图的函数
* @param $filename 文件名
* @param $dest 缩略图保存路径 thumbs目录下
* @param $pre 默认前缀 thumb_
* @param $dst_w 最大宽度
* @param $dst_h 最大高度
* @param $scale 缩放比例
* @param $delsource 是否删除原文件
* @return string 最终保存路径文件名
*/
function thumb($filename, $dest = 'thumbs', $pre = 'thumb_', $dst_w = null, $dst_h = null, $scale = 0.5, $delsource = false)
{
//$filename = 'images/tongdawei.jpg';
//$scale = 0.5;
//$dst_w = 200;
//$dst_h = 300;
//$dest = 'thumbs';
//$pre = 'thumb_';
//$delsource=false; 默认不删除
$fileInfo = getImageInfo($filename);
$src_w = $fileInfo['width'];
$src_h = $fileInfo['height'];
//如果指定最大宽度和高度按照等比例缩放处理
if (is_numeric($dst_w) && is_numeric($dst_h)) {
$ratio_orig = $src_w / $src_h;
if ($dst_w / $dst_h > $ratio_orig) {
$dst_w = $dst_h * $ratio_orig;
} else {
$dst_h = $dst_w / $ratio_orig;
}
} else {
//没指定按照默认的缩放比例处理
$dst_w = ceil($src_w * $scale);
$dst_h = ceil($src_h * $scale);
}
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
$src_image = $fileInfo['createFun']($filename);
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
//检测目录是否存在 不存在就创建
if ($dest && !file_exists($dest)) {
mkdir($dest, 0777, true);
}
$randNum = mt_rand(100000, 999999);
$dstName = "{$pre}{$randNum}" . $fileInfo['ext'];
$destination = $dest ? $dest . '/' . $dstName : $dstName;
$fileInfo['outFun']($dst_image, $destination);
//echo $dstName;
imagedestroy($src_image);
imagedestroy($dst_image);
if ($delsource) {
@unlink($filename);
}
return $destination;
}<?php
header('content-type:text/html;charset=utf-8');
ini_set('date.timezone','Asia/Shanghai');
//文字水印效果
$filename='images/1.jpg';
$fileInfo=getimagesize($filename);
$mime=$fileInfo['mime'];
$createFun=str_replace('/','createfrom',$mime);
//echo $createFun;exit;
$outFun=str_replace('/','',$mime);//不能用null//php 8.19
$image=$createFun($filename);
//$red=imagecolorallocate($image,255,0,0);
$red=imagecolorallocatealpha($image,255,0,0,60);
$fontfile='fonts/kaiti.ttc';
imagettftext($image, 30, 0, 0, 1020, $red, $fontfile,'佳和信息');
header('content-type:'.$mime);
$outFun($image);
imagedestroy($image);
随时随地看视频
热门评论
-
百亿佳和2022-08-17 0
查看全部评论欢迎大家交流!