提供二个常用的图片处理方法:

1、按照指定的尺寸压缩图片

   /**
* 按照指定的尺寸压缩图片
* @param $source_path 原图路径
* @param $target_path 保存路径
* @param $imgWidth 目标宽度
* @param $imgHeight 目标高度
* @return bool|string
*/
function resize_image($source_path,$target_path,$imgWidth,$imgHeight)
{
$source_info = getimagesize($source_path);
$source_mime = $source_info['mime'];
switch ($source_mime)
{
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break; case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break; case 'image/png':
$source_image = imagecreatefrompng($source_path);
break; default:
return false;
break;
}
$target_image = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图
imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $imgWidth, $imgHeight, $source_info[0], $source_info[1]);
//保存图片到本地
$dir = '../'.$target_path. '/'. date("Ymd") . '/';
if (!is_dir($dir)) {
mkdir($dir, 0777);
} $fileName = $dir.date("YmdHis").uniqid().'.jpg';
if(!imagejpeg($target_image,'./'.$fileName)){
$fileName = '';
}
imagedestroy($target_image);
return $fileName;
}

2、按照比例裁剪图片

/**
* 图像裁剪
* @param $title string 原图路径
* @param $content string 需要裁剪的宽
* @param $encode string 需要裁剪的高
* @param $target_path string 需要保存的路径
*/
function image_cropper($source_path, $target_width, $target_height, $target_path)
{
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width; if ($source_ratio > $target_ratio) // 源图过高
{
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2; }elseif ($source_ratio < $target_ratio){ // 源图过宽 $cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}else{ // 源图适中 $cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
} switch ($source_mime)
{
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break; case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break; case 'image/png':
$source_image = imagecreatefrompng($source_path);
break; default:
return false;
break;
} $target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height); // 裁剪
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height); //保存图片到本地(两者选一)
$dir = '../../'.$target_path. '/'. date("Ymd") . '/';
if (!is_dir($dir)) {
mkdir($dir, 0777);
} $fileName = $dir.date("YmdHis").uniqid().'.jpg';
if(!imagejpeg($target_image,'./'.$fileName)){
$fileName = '';
}
imagedestroy($target_image);
return $fileName;
}

PHP 图片操作(按照指定尺寸压缩,按照比例裁剪)的更多相关文章

  1. 织梦dedecms将指定图片自动生成指定尺寸的小图、缩略图、图片的方法

    对于普通企业网站来讲,织梦原来的程序只是提供了一个缩略图,但是这样对于一些相对来说图片会比较多的网站来说,图片太大当缩略图会导致网站整体的访问速度,所以我今天就来教你织梦把一张大图转换成生成一张小图或 ...

  2. Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩

    目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...

  3. java图片压缩工具类(指定压缩大小)

    1:先导入依赖 <!--thumbnailator图片处理--> <dependency> <groupId>net.coobird</groupId> ...

  4. Android应用程序开发之图片操作(二)——工程图片资源的加载及OOM的处理

    (一)工程图片资源的加载方法 在Android应用程序开发之图片操作(一)中,详细说明了如何操作各种资源图片,只是有的没有附上示例代码,在此,我将针对项目工程中的图片资源的显示加载进行说明.官方说明, ...

  5. Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas

    Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas   1,Bitmap对象的获取 首先说一下Bitmap,Bitmap是Androi ...

  6. media静态文件统一管理 操作内存的流 - StringIO | BytesIO PIL:python图片操作库 前端解析二进制流图片(了解) Admin自动化数据管理界面

    一.media ''' 1. 将用户上传的所有静态文件统一管理 -- settings.py -- MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 2. 服务 ...

  7. PHP图片操作

    <?php $filename="http://pic.nipic.com/2007-12-06/2007126102233577_2.jpg";//图片地址//获取图片信息 ...

  8. C#图片操作公共库

    存一下,以后找起来方便 包括图片加载.压缩.base64等 public static class ImageFun { #region 图片 public static EncoderParamet ...

  9. C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码

    本文介绍在 C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码.网上文章大多只是简单介绍内置参数的设置,根据我的使用目的,增加了自定义目标二维码图片尺寸和白边 ...

随机推荐

  1. 关于Apache Shiro权限框架的一些使用误区的解释

    多了不说了,进入正题,shiro是个权限框架提供权限管理等功能,网上的教程一般都是互相抄,比如<shiro:principal property="xxx"/>这个标签 ...

  2. linux 服务器所支持的最大句柄数调高数倍(与服务器的内存数量相关)

    https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(详尽版).pdf 2. [推荐]调大服务器所支持的最大文件句柄数(File Descri ...

  3. mysql 5.7.18版本 sql_mode 问题

    only_full_group_by 模式的,但开启这个模式后,原先的 group by 语句就报错,然后又把它移除了. only_full_group_by ,感觉,group by 将变成和 di ...

  4. dbUtils 原理

    // Jdbc 的增,删, 改流程类似,只是参数不同, 因此可以向上抽取 public class Demo{ // Jdbc 的增加 public void addStu(Stu stu){ Con ...

  5. 前端基础 & Bootstrap框架

    Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局,并且 ...

  6. hbase中清空整张表的数据

    hbase(main):005:0> truncate 'fr:test' Truncating 'FaceBase' table (it may take a while): - Disabl ...

  7. SpringBoot学习过程

    最近这两年最流行的java框架也属SpringBoot了,早在前几年我一直用NinjaFramwork这个java框架,也是非常优秀,不过最近在面试各家公司的过程中最为流行的还是SpringBoot了 ...

  8. 剑指offer 面试31题

    面试31题: 题目:栈的压入.弹出元素 题:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序 ...

  9. Linux文件IO

    参考<unix高级环境编程> 本章开始讨论U N I X系统,先说明可用的文件I / O函数——打开文件.读文件.写文件等等.大多数U N I X文件I / O只需用到5个函数:o p e ...

  10. Oracle处理Clob类型数据入库(String入库)

    从网上查找一堆参考,要么语焉不详,要么不可行.自己鼓捣了一堆可以正常入库了.请看最后: insert into CP_V_INFO" + "(ID, "+ "P ...