PHP 图片处理功能概述与历史记录策略
PHP 提供的图片处理函数支持多种操作,如缩放、剪切、添加相框和水印、锐化、旋转、翻转、调整透明度以及反色处理。为了有效保存修改记录,通常采用一种策略:每当对原始图片进行任何改动时,系统会自动生成一张新的图片文件,从而保留历史版本。
将本地图片转换为 Base64 编码
第一步是将图片转换为 Base64 编码格式,这便于在网络传输或存储中使用。
/**
* 获取图片的Base64编码(不支持url)
* @param $img_file 传入本地图片地址
* @return string
*/
function imgToBase64($img_file) {
$img_base64 = '';
if (file_exists($img_file)) {
$app_img_file = $img_file; // 图片路径
$img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
//echo '' . print_r($img_info, true) . ''; list($width, $height, $type, $attr) = getimagesize($app_img_file); $fp = fopen($app_img_file, "r"); // 图片是否可读权限 if ($fp) { $filesize = filesize($app_img_file); $content = fread($fp, $filesize); $file_content = chunk_split(base64_encode($content)); // base64编码 switch ($type) { //判读图片类型 case 1: $img_type = "gif"; break; case 2: $img_type = "jpg"; break; case 3: $img_type = "png"; break; } $img_base64 = 'data:image/png;base64,' . $file_content;//合成图片的base64编码 } fclose($fp); }else{ return $img_file; } return $img_base64; //返回图片的base64 }
实现图片顺时针或逆时针旋转
第二步涉及图片的旋转操作,支持顺时针或逆时针旋转 90 度。
/**
* 图片旋转
* @param $src 图片地址
* @param $direction 1顺时针90 2 逆时针90
* @return string
*/
function imgturn($src, $direction = 1){
$ext = pathinfo($src)['extension'];
switch ($ext) {
case 'gif':
$img = imagecreatefromgif($src);
break;
case 'jpg':
case 'jpeg':
$img = imagecreatefromjpeg($src);
break;
case 'png':
$img = imagecreatefrompng($src);
break;
default:
die('图片格式错误!');
break;
}
$width = imagesx($img);
$height = imagesy($img);
$img2 = imagecreatetruecolor($height, $width);
//顺时针旋转90度
if($direction == 1){
for ($x = 0; $x < $width; $x++) {
for($y=0; $y<$height; $y++) {
imagecopy($img2, $img, $height - 1 - $y, $x, $x, $y, 1, 1);
}
}
}else if($direction == 2) {
//逆时针旋转90度
for ($x = 0; $x < $height; $x++) {
for($y = 0; $y < $width; $y++) {
imagecopy($img2, $img, $x, $y, $width - 1 - $y, $x, 1, 1);
}
}
}
switch ($ext) {
case 'jpg':
case "jpeg":
imagejpeg($img2, $src, 100);
break;
case "gif":
imagegif($img2, $src, 100);
break;
case "png":
imagepng($img2, $src, 100);
break;
default:
die('图片格式错误!');
break;
}
imagedestroy($img);
imagedestroy($img2);
}
执行图片压缩与生成目标图片
第三步是对图片进行压缩处理,通常用于减小文件体积以适应网络传输或存储需求。
/**
* 图片压缩处理
* @param string $sFile 源图片路径
* @param int $iWidth 自定义图片宽度
* @param int $iHeight 自定义图片高度
* @return string 压缩后的图片路径
*/
function getThumb($sFile, $iWidth, $iHeight){
//图片公共路径
$public_path = '';
//判断该图片是否存在
if(!file_exists($public_path . $sFile)) return $sFile;
list($width, $height, $type, $attr) = getimagesize($sFile);
if($width < $height){
imgturn($sFile, 2);
}
//判断图片格式(图片文件后缀)
$extend = explode("." , $sFile);
$attach_fileext = strtolower($extend[count($extend) - 1]);
if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
return '';
}
//压缩图片文件名称
$sFileNameS = str_replace("." . $attach_fileext, "_" . $iWidth . '_' . $iHeight . '.' . $attach_fileext, $sFile);
//判断是否已压缩图片,若是则返回压缩图片路径
if(file_exists($public_path . $sFileNameS)){
return $sFileNameS;
}
//生成压缩图片,并存储到原图同路径下
resizeImage($public_path . $sFile, $public_path . $sFileNameS, $iWidth, $iHeight);
if(!file_exists($public_path . $sFileNameS)){
return $sFile;
}
return $sFileNameS;
}
第四步是生成最终的目标图片,即根据设定的宽度和高度参数,将处理后的图像保存为新文件。
/**
* 生成图片
* @param string $im 源图片路径
* @param string $dest 目标图片路径
* @param int $maxwidth 生成图片宽
* @param int $maxheight 生成图片高
*/
function resizeImage($im, $dest, $maxwidth, $maxheight) {
$img = getimagesize($im);
switch ($img[2]) {
case 1:
$im = @imagecreatefromgif($im);
break;
case 2:
$im = @imagecreatefromjpeg($im);
break;
case 3:
$im = @imagecreatefrompng($im);
break;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
$resizewidth_tag = false;
$resizeheight_tag = false;
if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
if ($maxwidth && $pic_width > $maxwidth) {
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}
if ($maxheight && $pic_height > $maxheight) {
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}
if ($resizewidth_tag && $resizeheight_tag) {
if ($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}
if ($resizewidth_tag && !$resizeheight_tag){
$ratio = $widthratio;
}
if ($resizeheight_tag && !$resizewidth_tag){
$ratio = $heightratio;
}
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if (function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
}
imagejpeg($newim, $dest);
imagedestroy($newim);
} else {
imagejpeg($im, $dest);
}
}
以上即为 PHP 对图片进行各类处理的详细流程与方法。如需获取更多关于 PHP 图片处理的资料,建议关注本站后续发布的相关技术文章。