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 生成指定尺寸和边框宽度的二维码.网上文章大多只是简单介绍内置参数的设置,根据我的使用目的,增加了自定义目标二维码图片尺寸和白边 ...
随机推荐
- crontab 问题分析 - CSDN博客 https://blog.csdn.net/tengdazhang770960436/article/details/50997297
cd /mnt/tools/trunk/plugins/personas; python update_keywords.py crontab 问题分析 crontab 问题分析 - CSDN博客 ...
- 安装和配置jenkins
1.首先准备java环境,安装JDK 2.下载jenkins至Linux服务器 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkin ...
- 为什么调用 GdiplusShutdown 函数会在 DllExports::GdipDeleteGraphics(nativeGraphics) 位置抛出异常?
因为没有仔细看文档 https://docs.microsoft.com/en-us/windows/desktop/api/Gdiplusinit/nf-gdiplusinit-gdiplusshu ...
- window 注册表上下文菜单如何配置?
注册表结构? Keys Abbreviation Description 描述 HKEY_CLASSES_ROOT HKCR Stores file association and COM objec ...
- POCO c++ 使用例子
.定时器 #include "Poco/Timer.h" #include "Poco/Thread.h" using Poco::Timer; using P ...
- Nginx图片及样式文件不记录访问日志
1.Nginx作为web服务器是可直接在server配置文件中做如下配置: server { listen ; server_name www.fansik.com; access_log /data ...
- linux环回文件
我们通常在设备上(比如磁盘分区)上创建文件系统,这些存储设备能够以设备文件的形式来使用,如/dev/device_name.为了使用存储设备上的文件系统,我们将其挂载到挂载点. 环回文件系统是指那些在 ...
- 使用PLSQL Developer和DbVisualizer、SQLDBx查询oracle数据库时出现乱码
使用PLSQL Developer和DbVisualizer查询oracle数据库时,出现查询数据中文乱码情况. 查看了一下数据库编码格式select * from v$nls_parameters; ...
- DevOps能力是落地微服务的前提
在软件开发领域不存在银弹,当用一项新的技术或新的架构时一定要明白其背后的原理,确保把合适的技术应用在合适的项目上,而不是盲目跟风. 单体应用伸缩性差,而且随着应用规模的扩大,业务逻辑和开发部署过程都变 ...
- Python学习进程(6)函数
函数最重要的目的是方便我们重复使用相同的一段程序. (1)函数的定义: 函数定义的简单规则: 1.函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(): 2.任何传入参数和 ...