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上.按照原始大小 ...
随机推荐
- 深入理解$watch ,$apply 和 $digest --- 理解数据绑定过程——续
Angular什么时候不会自动为我们$apply呢? 这是Angular新手共同的痛处.为什么我的jQuery不会更新我绑定的东西呢?因为jQuery没有调用$apply,事件没有进入angular ...
- spring学习笔记Core Technologies
Spring 框架最重要的是Ioc(Inversion of Control)容器,在这个基础之上衍生出了AOP(Aspect-Oriented Programming)技术,80/20法则,这货可以 ...
- beta阶段贡献分配实施
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2281] 要求1 每位组员的贡献分值 刘莹莹 王玉潘 潘世维 周昊 赵美增 ...
- cocos2d-x移植:xcode到eclipse
xcode程序移植到eclipse 必要组件: 1.macos gcc编译器,若没有,在xcode->preference->downloads中下载command line tools( ...
- xxx/labelKeypoint/utils/qt.py:81: RuntimeWarning: invalid value encountered in double_scalars
原代码: return np.linalg.norm(np.cross(p2 - p1, p1 - p3)) / np.linalg.norm(p2 - p1) 出现报错: xxx/labelKeyp ...
- ubuntu16安装mysql图形界面
之前在windows用sqlyog当做图形界面连接mysql,现在在ubuntu上需要连接测试环境的数据库,需要安装mysql图形界面.安装只需要条命令: sudo apt-get update su ...
- buy now按钮的添加
样例是 www.dealfreeship.com:在D:\xampp\htdocs\aliexpress\app\design\frontend\default\se101\template\cata ...
- SIM800C SIM卡唯一标识符ICCID
/******************************************************************************* * SIM800C SIM卡唯一标识符 ...
- ReSharper2017.3的列对齐、排版格式、列对齐错误的修复
ReSharper代码排版格式 列对齐 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- CF1083(div1)
A. The Fair Nut and the Best Path 题意:给定有点权,有边权的树,让你选择一条链(也可以是只有一个点),使得点权之和-边权最大. 思路:裸的树形DP,我们用dp[i]表 ...