扣丁学堂PHP培训简述PHP图片裁剪函数

2019-05-05 16:21:59 2848浏览

本篇文章扣丁学堂PHP培训小编给读者们实例分享一下PHP图片裁剪函数的具体代码,对PHP开发技术感兴趣或者是想要参加PHP培训的小伙伴就随小编来了解一下吧。



扣丁学堂PHP培训简述PHP图片裁剪函数



/*
 * 图片裁剪工具
 * 将指定文件裁剪成正方形
 * 以中心为起始向四周裁剪
 * @param $src_path string 源文件地址
 * @param $des_path string 保存文件地址
 * @param $des_w double 目标图片宽度
 * */
function img_cut_square($src_path,$des_path,$des_w=100){
  $img_info = getimagesize($src_path);//获取原图像尺寸信息
  $img_width = $img_info[0];//原图宽度
  $img_height = $img_info[1];//原图高度
  $img_type = $img_info[2];//图片类型 1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG 格式
  if($img_type != 2 && $img_type != 3) return ;

  /*计算缩放尺寸*/
  if($img_height > $img_width){
    $scale_width = $des_w;//缩放宽度
    $scale_height = round($des_w / $img_width * $img_height);//缩放高度

    $src_y = round(($scale_height - $des_w)/2);
    $src_x = 0;
  }else{
    $scale_height = $des_w;
    $scale_width = round($des_w / $img_height * $img_width);

    $src_y = 0;
    $src_x = round(($scale_width - $des_w)/2);
  }

  $dst_ims = imagecreatetruecolor($scale_width, $scale_height);//创建真彩画布
  $white = imagecolorallocate($dst_ims, 255, 255, 255);
  imagefill($dst_ims, 0, 0, $white);
  if($img_type == 2){
    $src_im = @imagecreatefromjpeg($src_path);//读取原图像
  }else if($img_type == 3){
    $src_im = @imagecreatefrompng($src_path);//读取原图像
  }

  imagecopyresized($dst_ims, $src_im, 0, 0 ,0, 0 , $scale_width , $scale_height , $img_width,$img_height);//缩放图片到指定尺寸


  $dst_im = imagecreatetruecolor($des_w, $des_w);
//  $white = imagecolorallocate($dst_im, 255, 255, 255);
//  imagefill($dst_im, 0, 0, $white);
  imagecopy($dst_im, $dst_ims, 0, 0, $src_x, $src_y, $des_w, $des_w);//开始裁剪图片为正方形
// imagecopyresampled($dst_im, $src_im, $src_x, $src_y, 0, 0, $real_width, $real_width,$img_width,$img_height);
  if($img_type == 2) {
    imagejpeg($dst_im, $des_path);//保存到文件
  }else if($img_type == 3){
    imagepng($dst_im,$des_path);
  }
//  imagejpeg($dst_im);//输出到浏览器
  imagedestroy($dst_im);
  imagedestroy($dst_ims);
  imagedestroy($src_im);
}



想要了解更多关于PHP开发方面内容的小伙伴,请关注扣丁学堂PHP培训官网、微信等平台,扣丁学堂IT职业在线学习教育有专业的PHP讲师为您指导,此外扣丁学堂老师精心推出的PHP视频教程定能让你快速掌握PHP从入门到精通开发实战技能。扣丁学堂PHP技术交流群:374332265。


扣丁学堂微信公众号                          Python全栈开发爬虫人工智能机器学习数据分析免费公开课直播间


【关注微信公众号获取更多学习资料】         【扫码进入Python全栈开发免费公开课】



查看更多关于“php培训资讯”的相关文章>>


标签: PHP培训 PHP基础教程 PHP学习视频 PHP教学视频 PHP入门教程 PHP教程视频 PHP在线学习 PHP在线视频 PHP在线教程 扣丁学堂PHP培训

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号