php 图片剪切
<?php /**
* 图像裁剪
* @param $source_path 原图路径
* @param $target_width 需要裁剪的宽
* @param $target_height 需要裁剪的高
* @return bool
*/
function imagecropper($source_path, $target_width, $target_height)
{
$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;
}
$funcList = array(
'image/gif' => array(
'create' => 'imagecreatefromgif',
'make' => 'imagegif',
'mime' => '.gif',
),
'image/jpeg' => array(
'create' => 'imagecreatefromjpeg',
'make' => 'imagejpeg',
'mime' => '.jpg',
),
'image/png' => array(
'create' => 'imagecreatefrompng',
'make' => 'imagepng',
'mime' => '.png',
),
);
if (isset($funcList[$source_mime])) {
$curFunc = $funcList[$source_mime];
} else {
return FALSE;
}
$source_image = $curFunc['create']($source_path); $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); //保存图片到本地(两者选一)
$randNumber = mt_rand(00000, 99999) . mt_rand(000, 999);
//$fileName = substr(md5($randNumber), 8, 16) . ".png";
$fileName = time() . '_' . substr(md5($randNumber), 8, 16) . $curFunc['mime'];
//imagepng($target_image, './' . $fileName);
//imagepng($target_image, './' . $fileName);
//$imgFunc2($target_image, './' . $fileName);
$curFunc['make']($target_image, './' . $fileName);
imagedestroy($target_image); //直接在浏览器输出图片(两者选一)
/*header('Content-Type: image/jpeg');
imagepng($target_image);
imagedestroy($target_image);
imagejpeg($target_image);
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);*/
} //调用
//imagecropper('./img033.jpg',300,300);
imagecropper('./4.jpg', 140, 140);
//imagecropper('./img033.jpg',55,55);
?>
<?php
/**
* 图像裁剪
* @param $source_path 原图路径
* @param $target_width 需要裁剪的宽
* @param $target_height 需要裁剪的高
* @return bool
*/
function imagecropper($source_path, $target_width, $target_height)
{
$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;
}
$funcList = array(
'image/gif' => array(
'create' => 'imagecreatefromgif',
'make' => 'imagegif',
'mime' => '.gif',
),
'image/jpeg' => array(
'create' => 'imagecreatefromjpeg',
'make' => 'imagejpeg',
'mime' => '.jpg',
),
'image/png' => array(
'create' => 'imagecreatefrompng',
'make' => 'imagepng',
'mime' => '.png',
),
);
if (isset($funcList[$source_mime])) {
$curFunc = $funcList[$source_mime];
} else {
return FALSE;
}
$source_image = $curFunc['create']($source_path);
$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);
//保存图片到本地(两者选一)
$randNumber = mt_rand(00000, 99999) . mt_rand(000, 999);
//$fileName = substr(md5($randNumber), 8, 16) . ".png";
$fileName = time() . '_' . substr(md5($randNumber), 8, 16) . $curFunc['mime'];
//imagepng($target_image, './' . $fileName);
//imagepng($target_image, './' . $fileName);
//$imgFunc2($target_image, './' . $fileName);
$curFunc['make']($target_image, './' . $fileName);
imagedestroy($target_image);
//直接在浏览器输出图片(两者选一)
/*header('Content-Type: image/jpeg');
imagepng($target_image);
imagedestroy($target_image);
imagejpeg($target_image);
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);*/
}
//调用
//imagecropper('./img033.jpg',300,300);
imagecropper('./4.jpg', 140, 140);
//imagecropper('./img033.jpg',55,55);
?>
php 图片剪切的更多相关文章
- iOS开发UI篇—Quartz2D使用(图片剪切)
iOS开发UI篇—Quartz2D使用(图片剪切) 一.使用Quartz2D完成图片剪切 1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - ...
- 【iOS】Quartz2D图片剪切
一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - (void)drawRect:(CGRect)rec ...
- 图片上传,图片剪切jquery.imgareaselect
---恢复内容开始--- <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- android——拍照,相册图片剪切其实就这么简单
接触android这么久了.还没有真正的浩浩看看android拍照,相册图片剪切到底是怎么回事,每次都是从别人的代码一扣,就过来了.其实,谷歌提供的API已经很强大.只需要用的好,就那么几句就可以搞定 ...
- 图片剪切之Croppic插件
前几天做图片上传时需要进行图片的剪切和缩放,上网查找时找到了这个插件.样式很好看,功能也很OK.但是网上都是php进行后台处理图片的例子,然后只好慢慢琢磨C#的处理.插件地址是:http://www. ...
- 用JavaScript实现图片剪切效果
学会如何获取鼠标的坐标位置以及监听鼠标的按下.拖动.松开等动作事件,从而实现拖动鼠标来改变图片大小. 还可以学习css中的clip属性. 一.CSS实现图片不透明及裁剪效果. 图片剪切三层结构 1.第 ...
- php imagemagick 处理 图片剪切、压缩、合并、插入文本、背景色透明
php有一款插件叫做imagemagick,功能很强大,提供了图片的很多操作,图片剪切.压缩.合并.插入文本.背景色透明等.并且有api方法调用和命令行操作两种方式,如果只是简单处理的话建议api方法 ...
- opencv 图片剪切
import cv2 as cv import numpy as np # 图片剪切 img = cv.imread('../images/moon.jpg', flags=1) # flags=1读 ...
- android 7.0以上共享文件(解决调用系统照相和图片剪切出现的FileUriExposedException崩溃问题)
在android7.0开始试共享“file://”URI 将会导致引发 FileUriExposedException. 如果应用需要与其他应用共享私有文件,则应该使用 FileProvider, F ...
- 在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
原文 http://www.jb51.net/article/75671.htm 图片剪切一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中先把图片绘制到view上.按照原始大小 ...
随机推荐
- Graph (floyd)
Description Everyone knows how to calculate the shortest path in a directed graph. In fact, the oppo ...
- Silverlight 5 Developer Rumtime
因为更新了Silverlight SDK,所以也要更新相应的Silverlight开发运行时. Silverlight 5 Developer Rumtime (32bit): http://go.m ...
- linux安装配置postgres及使用dblink
好久不写东西,一直在看些开源的东西,下面贴下linux上安装配置postgres及使用dblink的操作参考,以供读者和自己今后参考: 1.下载源码:postgresql-9.3.2.tar.gz 2 ...
- 【转】C# 生成二维码并且在中间加Logo(图片合并)
public class QRCodeHelper { public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidt ...
- 异常检测——局部异常因子(Local Outlier Factor ,LOF)算法
在中等高维数据集上执行异常值检测的另一种有效方法是使用局部异常因子(Local Outlier Factor ,LOF)算法.1.算法思想 LOF通过计算一个数值score来反映一个样本的异常程度.这 ...
- Unicode与UTF-8,UTF-16
Unicode(UTF-8, UTF-16)令人混淆的概念 为啥需要Unicode 我们知道计算机其实挺笨的,它只认识0101这样的字符串,当然了我们看这样的01串时肯定会比较头晕的,所以很多时候为了 ...
- AOJ1024 Cleaning Robot 2.0
先说一说这个OJ:貌似是11区某大学ACM的OJ,叫AIZU ONLINE JUDGE,貌似还可以看到部分犇的代码...跪跪跪 然后知道这个OJ是某场比赛安利的= = 接下来将做法: 首先我们可以发现 ...
- 快速切题 poj1258
坑!!!我还以为一个整数会截到两行!! Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 40056 Ac ...
- bzoj1084&&洛谷2331[SCOI2005]最大子矩阵
题解: 分类讨论 当m=1的时候,很简单的dp,这里就不再复述了 当m=2的时候,设dp[i][j][k]表示有k个子矩阵,第一列有i个,第二列有j个 然后枚举一下当前子矩阵,状态转移 代码: #in ...
- 《C语言中分配了动态内存后一定要释放吗?》
问:比如main函数里有一句 malloc(),后面没有free()1.那么当main结束后,动态分配的内存不会随之释放吗?2.如果程序结束能自动释放,那么还加上free(),是出于什么考虑? 答: ...