# php图片压缩,更改图片格式,转base64。
本人是“运维”,公司项目是外包公司做的,项目到手后发先很多细节的东西都很粗糙所以有些东西只能自己改咯。今天发现项目所上传的图片居然没有压缩,小的几m,大的几十m这样对客户体验很不友好,一次性载入几十m的图片这样体验非常不好,所以自己写了一个php的图片压缩(当然网上找了找修修改改)废话不多了说直接上代码。
需要开启拓展php-exif
<?php
//图片公共路径
$public_path = '';
$pt = base64aa('aaaa.jpg','900','800',$public_path,'jpg');
//base64去掉空格
$pt = str_replace(PHP_EOL, '', $pt);
echo '<img src="' . $pt . '">'; //图片形式展示
echo '<hr>';
echo $pt; //输出Base64编码
/**
* 整合 图片压缩 控制高宽 改格式
* 图片名称 ,高,宽,图片存入路径,图片格式
*/
function base64aa($tu,$w,$h,$public_path,$format){
//压缩
$sFileNameS = getThumb($tu,$w,$h,$public_path);
//测试
//echo $sFileNameS;
//判断图片格式(图片文件后缀)
$extend = explode("." , $sFileNameS);
$attach_fileext = strtolower($extend[0]);
$attach_fileext2 = strtolower($extend[count($extend)-1]);
if ($attach_fileext2 != $format){
// 转换后保存在test.png
transform_image($sFileNameS, '$format', $attach_fileext.".".$format);
}
//转换base64
return $img_base64 = imgToBase64($attach_fileext.".".$format);
}
//echo $tu;
//echo $sFileNameS;
//transform_image($filepath, 'png', './test.png');
// 转换后二进制结果直接返回
//transform_image($sFileNameS, 'png');
//transform_image($filepath, 'png');
/**
* 生成图片
* @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);
}
}
/**
* 图片压缩处理
* @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;
//判断图片格式(图片文件后缀)
$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, $iHeight."_".$iWidth."_.".$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 $public_path.$sFileNameS;
}
/**
* 图片格式转换
* @param string $image_path 文件路径或url
* @param string $to_ext 待转格式,支持png,gif,jpeg,wbmp,webp,xbm
* @param null|string $save_path 存储路径,null则返回二进制内容,string则返回true|false
* @return boolean|string $save_path是null则返回二进制内容,是string则返回true|false
* @throws Exception
* @author klinson <klinson@163.com>
*/
function transform_image($image_path, $to_ext = 'png', $save_path = null)
{
if (! in_array($to_ext, ['png', 'gif', 'jpeg', 'wbmp', 'webp', 'xbm'])) {
throw new \Exception('unsupport transform image to ' . $to_ext);
}
switch (exif_imagetype($image_path)) {
case IMAGETYPE_GIF :
$img = imagecreatefromgif($image_path);
break;
case IMAGETYPE_JPEG :
case IMAGETYPE_JPEG2000:
$img = imagecreatefromjpeg($image_path);
break;
case IMAGETYPE_PNG:
$img = imagecreatefrompng($image_path);
break;
case IMAGETYPE_BMP:
case IMAGETYPE_WBMP:
$img = imagecreatefromwbmp($image_path);
break;
case IMAGETYPE_XBM:
$img = imagecreatefromxbm($image_path);
break;
case IMAGETYPE_WEBP: //(从 PHP 7.1.0 开始支持)
$img = imagecreatefromwebp($image_path);
break;
default :
throw new \Exception('Invalid image type');
}
$function = 'image'.$to_ext;
if ($save_path) {
return $function($img, $save_path);
} else {
$tmp = __DIR__.'/'.uniqid().'.'.$to_ext;
if ($function($img, $tmp)) {
$content = file_get_contents($tmp);
unlink($tmp);
return $content;
} else {
unlink($tmp);
throw new \Exception('the file '.$tmp.' can not write');
}
}
}
/**
* 获取图片的Base64编码(不支持url)
* @date 2021-04-9 16:02:11
*
* @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 '<pre>' . print_r($img_info, true) . '</pre><br>';
$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 ($img_info[2]) { //判读图片类型
case 1: $img_type = "gif";
break;
case 2: $img_type = "jpg";
break;
case 3: $img_type = "png";
break;
}
$img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
}
fclose($fp);
}
return $img_base64; //返回图片的base64
}
php图片压缩,更改图片格式,转base64。
最新推荐文章于 2022-11-25 15:49:33 发布