php如何处理图片并且生成新图片?
Admin 2021-04-27 群英技术资讯 1543 次浏览
php如何处理图片并生成新图片?在开发过程中,我们会遇到需要对图片大小,方向,色调等等修改的需求,那么PHP要如何对图片进行这些处理呢?处理完图片之后是怎样生成图片的呢?
图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片
1、转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 '<pre>' . print_r($img_info, true) . '</pre><br>';
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
}
2、图片旋转
/**
* 图片旋转
* @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);
}
3、图片压缩
/**
* 图片压缩处理
* @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;
}
4、生成目标图片
/**
* 生成图片
* @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实现图片处理的介绍就到这,当然更多对图片的处理操作,大家可以自己去扩展了解,希望本文对大家学习有帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在之前的文章中给大家带来了《一定搞得懂PHP中如何添加图片水印》,其中通过过示例给大家详细介绍了在PHP中应该如何添加水印,本篇文章我们继续来看一下PHP中错误处理的相关知识,希望能帮助到大家!
laravel怎样引入公共文件?一些新手对于laravel在中如何引入公共文件的操作不是很了解。对此,下面就给大家分享两个laravel引入公共文件的方法供大家参考,感兴趣的朋友可以看看。
本节介绍 PHP 中无法归类的一些函数,即,杂项函数。有不少朋友对此感兴趣,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。
今天小编就为大家分享一篇关于PDO::_construct讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
其中,Redis常见的报错就是:Redis可以配置如果客户端经过多少秒还不给Redis服务器发送数据,那么就会把连接close掉;MySQL常见的报错:和Redis服务器一样,MySQL也会定时的去清理掉没用的连接。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008