<?php
/**
* 说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形
*
* @param string $src_file 需要处理图片的文件名(绝对路径)
* @param string $dst_file 生成新图片的保存文件名(绝对路径)
* @param int $new_width 生成新图片的宽
* @param int $new_height 生成新图片的高
*/
function my_image_resize($src_file, $dst_file, $new_width, $new_height){
if ($new_width < 1 || $new_height < 1) {
echo 'params width or height error !';
die;
}
if (!file_exists($src_file)) {
echo $src_file . ' is not exists !';
die;
} // 图像类型
$type = exif_imagetype($src_file);
$support_type = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
if (!in_array($type, $support_type, true)) {
echo 'this type of image does not support! only support jpg , gif or png';
die;
} //Load image
switch ($type) {
case IMAGETYPE_JPEG:
$src_img = imagecreatefromjpeg($src_file);
break;
case IMAGETYPE_PNG:
$src_img = imagecreatefrompng($src_file);
break;
case IMAGETYPE_GIF:
$src_img = imagecreatefromgif($src_file);
break;
default:
echo 'Load image error!';
die;
}
$w = imagesx($src_img);
$h = imagesy($src_img);
$ratio_w = (1.0 * $new_width) / $w;
$ratio_h = (1.0 * $new_height) / $h;
$ratio = 1.0; // 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了)
if ($ratio_w < 1 && $ratio_h < 1 || $ratio_w > 1 && $ratio_h > 1) {
if ($ratio_w < $ratio_h) {
$ratio = $ratio_h;
} else {
$ratio = $ratio_w;
} // 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求
$inter_w = (int)($new_width / $ratio);
$inter_h = (int)($new_height / $ratio);
$inter_img = imagecreatetruecolor($inter_w, $inter_h);
imagecopy($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h); // 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
// 定义一个新的图像
$new_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height, $inter_w, $inter_h);
switch ($type) {
case IMAGETYPE_JPEG:
imagejpeg($new_img, $dst_file, 100); // 存储图像
break;
case IMAGETYPE_PNG:
imagepng($new_img, $dst_file, 100);
break;
case IMAGETYPE_GIF:
imagegif($new_img, $dst_file, 100);
break;
default:
break;
}
} else {
$ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w; // 取比例大的那个值
// 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大
$inter_w = (int)($w * $ratio);
$inter_h = (int)($h * $ratio);
$inter_img = imagecreatetruecolor($inter_w, $inter_h); // 将原图缩放比例后裁剪
imagecopyresampled($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h, $w, $h); // 定义一个新的图像
$new_img = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height);
switch ($type) {
case IMAGETYPE_JPEG:
imagejpeg($new_img, $dst_file, 100); // 存储图像
break;
case IMAGETYPE_PNG:
imagepng($new_img, $dst_file, 100);
break;
case IMAGETYPE_GIF:
imagegif($new_img, $dst_file, 100);
break;
default:
break;
}
}
}

PHP图像裁剪为任意大小的图像,图像不变形,不留下空白的更多相关文章

  1. Halcon将裁剪后的图像还原为原始大小

    ************************************************************* * Halcon将裁剪后的图像还原为原始大小 * Author: LiGua ...

  2. php 图像裁剪(自定义裁剪图片大小)

    <?php /** * 图像裁剪 * @param $title string 原图路径 * @param $content string 需要裁剪的宽 * @param $encode str ...

  3. HTML canvas图像裁剪

    canvas drawImage方法的图像裁剪理解可能会比较耗时,记录一下,以便供人翻阅! context.drawImage(img,sx,sy,swidth,sheight,x,y,width,h ...

  4. 【开源】canvas图像裁剪、压缩、旋转

    前言 前段时间遇到了一个移动端对图像进行裁剪.压缩.旋转的需求. 考虑到已有各轮子的契合度都不高,于是自己重新造了一个轮子. 关于图像裁剪.压缩 在HTML5时代,canvas的功能已经非常强大了,可 ...

  5. canvas图像裁剪、压缩、旋转

    转载于:http://www.cnblogs.com/dailc/p/7843204.html 前言 前段时间遇到了一个移动端对图像进行裁剪.压缩.旋转的需求.考虑到已有各轮子的契合度都不高,于是自己 ...

  6. PIE SDK图像裁剪

    1.算法功能简介 图像裁剪的目的是获取选定的影像范围区域.图像裁切工具提供像素范围裁切.矢量裁切.栅格图像裁切和几何图元裁切四种方式. 像素范围裁切是基于像素坐标获取矩形裁切区域的裁切方式:矢量裁切是 ...

  7. STM32内存受限情况下摄像头驱动方式与图像裁剪的选择

    1.STM32图像接收接口 使用stm32芯片,128kB RAM,512kB Rom,资源有限,接摄像头采集图像,这种情况下,内存利用制约程序设计. STM32使用DCMI接口读取摄像头,协议如下. ...

  8. jQuery Jcrop 图像裁剪

    jQuery Jcrop 图像裁剪 http://code.ciaoca.com/jquery/jcrop/ cropper.js 实现HTML5 裁剪图片并上传(裁剪上传头像.) https://b ...

  9. mui---调用图像裁剪android

    mui调用图像裁剪android: var IMAGE_UNSPECIFIED = "image/*"; //相册显示的文件类型 var PHOTOZOOM = 2; // 获取完 ...

随机推荐

  1. leetcode -- Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  2. ORACLE数据库存储空间使用情况查询

    使用系统sys或者dba权限的账户创建视图如下: 1. 主要从数据库的表dba_data_files,dba_segments两张表中获取.2. 默认数据库保存的是byte单位,转换关系如下: 102 ...

  3. 2016网络大事记 mark

    记录2016年每天的大事件. 2016年01月07日     快播庭审.辩护人各种出彩. 2016年01月09日     乐视多个贴吧被爆.百度出面平息. 2016年01月10日     斗鱼TV造人 ...

  4. HTML中&nbsp; &ensp; &emsp; &thinsp;等6种空白空格的区别

    HTML提供了5种空格实体(space entity),它们拥有不同的宽度,非断行空格( )是常规空格的宽度,可运行于所有主流浏览器.其他几种空格 (       ‌‍)在不同浏览器中宽度各异.   ...

  5. input 禁止输入法

    <INPUT TYPE = text STYLE = "ime-mode:disabled" > 即可禁止输入法 js形式: active 代表输入法为中文inacti ...

  6. 微信小程序之本地缓存(十)

    [未经作者本人同意,请勿以任何形式转载] 目前,微信给每个小程序提供了10M的本地缓存空间(哎哟妈呀好大) 有了本地缓存,你的小程序可以做到: 离线应用(已测试在无网络的情况下,可以操作缓存数据) 流 ...

  7. 用C#实现封装

    用C#实现封装 1.属性对外公开类似于类的接口实现对字段的访问;2.字段为private只能在内部被直接访问,如果当属性为只读,那么可以将形参直接对字段赋值.(有没有更好的方法?);3.可以通过关键字 ...

  8. vue2.0 开发实践总结之入门篇

    vue2.0 据说也出了很久了,博主终于操了一次实刀. 整体项目采用  vue +  vue-router +  vuex (传说中的vue 全家桶 ),构建工具使用尤大大推出的vue-cli 后续文 ...

  9. java学习指南

    @Override public BaseResponse selectListPage(PageData page, Object obj) { BaseResponse response = ne ...

  10. MFC用户自定义消息

    之前做过佳能相机和位移平台的额二次开发,当时遇到一个棘手的问题,就是位移平台如何知道相机已经拍完照了,或者相机如何知道位移平台已经运行到指定位置,当时为了方便采用了定时器来定时检测位移平台的位置,结果 ...