PHP 图片操作(按照指定尺寸压缩,按照比例裁剪)
提供二个常用的图片处理方法:
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 图片操作(按照指定尺寸压缩,按照比例裁剪)的更多相关文章
- 织梦dedecms将指定图片自动生成指定尺寸的小图、缩略图、图片的方法
对于普通企业网站来讲,织梦原来的程序只是提供了一个缩略图,但是这样对于一些相对来说图片会比较多的网站来说,图片太大当缩略图会导致网站整体的访问速度,所以我今天就来教你织梦把一张大图转换成生成一张小图或 ...
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...
- java图片压缩工具类(指定压缩大小)
1:先导入依赖 <!--thumbnailator图片处理--> <dependency> <groupId>net.coobird</groupId> ...
- Android应用程序开发之图片操作(二)——工程图片资源的加载及OOM的处理
(一)工程图片资源的加载方法 在Android应用程序开发之图片操作(一)中,详细说明了如何操作各种资源图片,只是有的没有附上示例代码,在此,我将针对项目工程中的图片资源的显示加载进行说明.官方说明, ...
- Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas
Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas 1,Bitmap对象的获取 首先说一下Bitmap,Bitmap是Androi ...
- media静态文件统一管理 操作内存的流 - StringIO | BytesIO PIL:python图片操作库 前端解析二进制流图片(了解) Admin自动化数据管理界面
一.media ''' 1. 将用户上传的所有静态文件统一管理 -- settings.py -- MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 2. 服务 ...
- PHP图片操作
<?php $filename="http://pic.nipic.com/2007-12-06/2007126102233577_2.jpg";//图片地址//获取图片信息 ...
- C#图片操作公共库
存一下,以后找起来方便 包括图片加载.压缩.base64等 public static class ImageFun { #region 图片 public static EncoderParamet ...
- C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码
本文介绍在 C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码.网上文章大多只是简单介绍内置参数的设置,根据我的使用目的,增加了自定义目标二维码图片尺寸和白边 ...
随机推荐
- <2013 12 28> AOI PCB设计
主要设计指标: “3.多块拼板最大尺寸:60*50(CM)4. 检测速度:(230-250)片/小时 5.检测通过率:98%6.最窄线宽:设两种精度 A.最窄线宽:0.2mm, 识别精度 0.1mm ...
- 关于Java中的toString()方法
package c07; class ewq{ public String toString() { return "ppppppppp"; } public static voi ...
- mysql数据库补充知识6 完整性约束
一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...
- javascript高级语法二
一.BOM对象 1.什么是BOM对象? BOM是浏览器对象模型,核心对象就是window,所有浏览器都支持 window 对象.一个html文档对应一个window对象,主要功能是控制浏览器窗口的, ...
- JAVA项目中常用的异常知识点总结
JAVA项目中常用的异常知识点总结 1. java.lang.nullpointerexception这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用 ...
- MVC4 中使用 Area 和 注意的地方
在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰. 步骤如下: 项目 –> 添加 -> 区域 (Area) 输入 Admin 添加成功后 Area包含:创建一个空 ...
- 流量分析系统----实现-echarts模拟迁移(bmap.js/china.js)
china.js: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- PAT 天梯赛 L1-029. 是不是太胖了 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-029 AC代码 #include <iostream> #include <cstdio&g ...
- Shell 条件判断总结
-b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d file 若文件存在且是一个目录,则为真 -e file 若文件存在,则为真 -f ...
- finally中的return
周五晚6点下班去面试,出了一份笔试题,看到第一题有些蒙了,虽然以前遇到过类似的问题,但并没有留心记一下,觉得没人会这样写代码,但实际上没有面试题中是有的. 1,有在try块中执行不到finally的情 ...