PHP中改变图片的尺寸大小的代码


Posted in PHP onJuly 17, 2011

先介绍一个自己写的函数。

<?php 
$imgsrc = "http://www.nowamagic.net/images/3.jpg"; 
$width = 780; 
$height = 420; 
resizejpg($imgsrc,$imgdst,$width,$height); 
function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight) 
{ 
//$imgsrc jpg格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度 
//取得图片的宽度,高度值 
$arr = getimagesize($imgsrc); 
header("Content-type: image/jpg"); 
$imgWidth = $imgwidth; 
$imgHeight = $imgheight; 
// Create image and define colors 
$imgsrc = imagecreatefromjpeg($imgsrc); 
$image = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图 
imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]); 
imagepng($image); 
imagedestroy($image); 
} 
?>

imagecopyresampled
imagecopyresampled -- 重采样拷贝部分图像并调整大小。
int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。dst_im 和 src_im 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_im 和 src_im 相同的话)区域,但如果区域交迭的话则结果不可预知。
注: 因为调色板图像限制(255+1 种颜色)有个问题。重采样或过滤图像通常需要多于 255 种颜色,计算新的被重采样的像素及其颜色时采用了一种近似值。对调色板图像尝试分配一个新颜色时,如果失败我们选择了计算结果最接近(理论上)的颜色。这并不总是视觉上最接近的颜色。这可能会产生怪异的结果,例如空白(或者视觉上是空白)的图像。要跳过这个问题,请使用真彩色图像作为目标图像,例如用 imagecreatetruecolor() 创建的。
注: imagecopyresampled() 需要 GD 2.0.l 或更高版本。
一个简单的示例:
<?php 
// The file 
$filename = 'test.jpg'; 
$percent = 0.5; 
// Content type 
header('Content-Type: image/jpeg'); 
// Get new dimensions 
list($width, $height) = getimagesize($filename); 
$new_width = $width * $percent; 
$new_height = $height * $percent; 
// Resample 
$image_p = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
// Output 
imagejpeg($image_p, null, 100); 
?>

示例2:
<?php 
// The file 
$filename = 'test.jpg'; 
// Set a maximum height and width 
$width = 200; 
$height = 200; 
// Content type 
header('Content-Type: image/jpeg'); 
// Get new dimensions 
list($width_orig, $height_orig) = getimagesize($filename); 
$ratio_orig = $width_orig/$height_orig; 
if ($width/$height > $ratio_orig) { 
$width = $height*$ratio_orig; 
} else { 
$height = $width/$ratio_orig; 
} 
// Resample 
$image_p = imagecreatetruecolor($width, $height); 
$image = imagecreatefromjpeg($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 
// Output 
imagejpeg($image_p, null, 100); 
?>

有两种改变图像大小的方法:
ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。
ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑。(但该函数的速度比 ImageCopyResized() 慢)。
两个函数的参数是一样的,如下:
imageCopyResampled(dest,src,dy,dx,sx,sy,dw,dh,sw,sh); 
imageCopyResized(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);

例子:
<?PHP 
$src = ImageCreateFromJPEG('php.jpg'); 
$width = ImageSx($src); 
$height = ImageSy($src); 
$x = $widht/2; 
$y = $height/2; 
$dst = ImageCreateTrueColor($x,$y); 
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$widht,$height); 
header('Content-Type : image/png'); 
ImagePNG($det); 
?>

在php中如何改变jpg图像文件的尺寸大小
<? 
function resize_jpg($img,$w,$h){ 
// $thumb = imagecreate ($w, $h); 
$image = imagecreatefromjpeg($img); 
$imagedata = getimagesize($img); 
if ($h = "auto") $h = $w/($imagedata[0]/$imagedata[1]);//根据原图的纵横比得出高度! 
$thumb = imagecreatetruecolor ($w, $h); 
imagecopyresized ($thumb, $image, 0, 0, 0, 0, $w, $h, $imagedata[0], $imagedata[1]); 
imagejpeg($thumb); 
} 
//resize_jpg($file,$w,$h); 
resize_jpg("images/dsc01244.jpg",100,100); 
imagedestory($thumb); 
imagedestory($image); 
?>

函数代码:
<?php 
/* 
* 图片缩略图 
* $srcfile 来源图片, 
* $rate 缩放比,默认为缩小一半,或者具体宽度象素值 
* $filename 输出图片文件名jpg 
* 例如: resizeimage("zt32.gif",0.1); 
* 例如: resizeimage("zt32.gif",250 ); 
* 说明:调用时直接把函数的结果放在HTML文件IMG标签中的SRC属性里 
*/ 
function resizeimage($srcfile,$rate=.5, $filename = "" ){ 
$size=getimagesize($srcfile); 
switch($size[2]){ 
case 1: 
$img=imagecreatefromgif($srcfile); 
break; 
case 2: 
$img=imagecreatefromjpeg($srcfile); 
break; 
case 3: 
$img=imagecreatefrompng($srcfile); 
break; 
default: 
exit; 
} 
//源图片的宽度和高度 
$srcw=imagesx($img); 
$srch=imagesy($img); 
//目的图片的宽度和高度 
if($size[0] <= $rate || $size[1] <= $rate){ 
$dstw=$srcw; 
$dsth=$srch; 
}else{ 
if($rate <= 1){ 
$dstw=floor($srcw*$rate); 
$dsth=floor($srch*$rate); 
}else { 
$dstw=$rate; 
$rate = $rate/$srcw; 
$dsth=floor($srch*$rate); 
} 
} 
//echo "$dstw,$dsth,$srcw,$srch "; 
//新建一个真彩色图像 
$im=imagecreatetruecolor($dstw,$dsth); 
$black=imagecolorallocate($im,255,255,255); 
imagefilledrectangle($im,0,0,$dstw,$dsth,$black); 
imagecopyresized($im,$img,0,0,0,0,$dstw,$dsth,$srcw,$srch); 
// 以 JPEG 格式将图像输出到浏览器或文件 
if( $filename ) { 
//图片保存输出 
imagejpeg($im, $filename ); 
}else { 
//图片输出到浏览器 
imagejpeg($im); 
} 
//释放图片 
imagedestroy($im); 
imagedestroy($img); 
} 
?>
PHP 相关文章推荐
第十节 抽象方法和抽象类 [10]
Oct 09 PHP
基于PHP与XML的PDF文档生成技术
Oct 09 PHP
对text数据类型不支持代码页转换 从: 1252 到: 936
Apr 23 PHP
php学习之流程控制实现代码
Jun 09 PHP
PHP获取php,mysql,apche的版本信息示例代码
Jan 16 PHP
PHP解码unicode编码的中文字符代码分享
Aug 13 PHP
PHP清除字符串中所有无用标签的方法
Dec 01 PHP
PHP、Python和Javascript的装饰器模式对比
Feb 03 PHP
PHP缓存工具XCache安装与使用方法详解
Apr 09 PHP
PHP正则表达式处理函数(PCRE 函数)实例小结
May 09 PHP
laravel 多图上传及图片的存储例子
Oct 14 PHP
php判断IP地址是否在多个IP段内
Aug 18 PHP
php中用foreach来操作数组的代码
Jul 17 #PHP
PHP Undefined index报错的修复方法
Jul 17 #PHP
php max_execution_time执行时间问题
Jul 17 #PHP
PHP写杨辉三角实例代码
Jul 17 #PHP
php中截取中文字符串的代码小结
Jul 17 #PHP
9个PHP开发常用功能函数小结
Jul 15 #PHP
PHP 字符串正则替换函数preg_replace使用说明
Jul 15 #PHP
You might like
phpmyadmin中禁止外网使用的方法
2014/11/04 PHP
JavaScript 笔记二 Array和Date对象方法
2010/05/22 Javascript
一个可拖拽列宽表格实例演示
2012/11/26 Javascript
JavaScript NodeTree导航栏(菜单项JSON类型/自制)
2013/02/01 Javascript
js判断横竖屏及禁止浏览器滑动条示例
2014/04/29 Javascript
Dojo Javascript 编程规范 规范自己的JavaScript书写
2014/10/26 Javascript
JavaScript实现页面5秒后自动跳转的方法
2015/04/16 Javascript
JS实现完全语义化的网页选项卡效果代码
2015/09/15 Javascript
jQuery+Ajax实现无刷新操作
2016/01/04 Javascript
Jquery实现纵向横向菜单
2016/01/24 Javascript
js改变html的原有内容实现方法
2016/10/05 Javascript
vue移动端路由切换实例分析
2018/05/14 Javascript
微信小程序中使用ECharts 异步加载数据实现图表功能
2018/07/13 Javascript
详解vue 兼容IE报错解决方案
2018/12/29 Javascript
微信小程序环境下将文件上传到OSS的方法步骤
2019/05/31 Javascript
javascript实现移动端触屏拖拽功能
2020/07/29 Javascript
[17:36]VG战队纪录片
2014/08/21 DOTA
Python中用pycurl监控http响应时间脚本分享
2015/02/02 Python
python MySQLdb Windows下安装教程及问题解决方法
2015/05/09 Python
在Pandas中给多层索引降级的方法
2018/11/16 Python
Python 微信之获取好友昵称并制作wordcloud的实例
2019/02/21 Python
Python当中的array数组对象实例详解
2019/06/12 Python
通过实例解析python创建进程常用方法
2020/06/19 Python
Pythonic版二分查找实现过程原理解析
2020/08/11 Python
浅谈Python 钉钉报警必备知识系统讲解
2020/08/17 Python
python打包多类型文件的操作方法
2020/09/21 Python
python通过函数名调用函数的几种场景
2020/09/23 Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
2020/10/30 Python
盗窃罪辩护词范文
2015/05/21 职场文书
大学生学生会工作总结2015
2015/05/26 职场文书
2015年车间管理工作总结
2015/07/23 职场文书
队列队形口号
2015/12/25 职场文书
委托开发合同书(标准版)
2019/08/07 职场文书
python生成可执行exe控制Microsip自动填写号码并拨打功能
2021/06/21 Python
Python学习之包与模块详解
2022/03/19 Python
Nginx配置之禁止指定IP访问
2022/05/02 Servers