PHP 图片处理
图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片
1、转Base64编码
/**
*获取图片的Base64编码(不支持url)
*@param$img_file传入本地图片地址
*@returnstring
*/
functionimgToBase64($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){//判读图片类型
case1:$img_type="gif";
break;
case2:$img_type="jpg";
break;
case3:$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$direction1顺时针902逆时针90
*@returnstring
*/
functionimgturn($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);
}
}
}elseif($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、图片压缩
/**
*图片压缩处理
*@paramstring$sFile源图片路径
*@paramint$iWidth自定义图片宽度
*@paramint$iHeight自定义图片高度
*@returnstring压缩后的图片路径
*/
functiongetThumb($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、生成目标图片
/**
*生成图片
*@paramstring$im源图片路径
*@paramstring$dest目标图片路径
*@paramint$maxwidth生成图片宽
*@paramint$maxheight生成图片高
*/
functionresizeImage($im,$dest,$maxwidth,$maxheight){
$img=getimagesize($im);
switch($img[2]){
case1:
$im=@imagecreatefromgif($im);
break;
case2:
$im=@imagecreatefromjpeg($im);
break;
case3:
$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图片处理的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
