菜鸟学会写图片裁剪和缩略图的php处理程序
时间:2010-01-30
来源:互联网
在设计php博客系统时,怎样写图片裁剪和缩略图的处理诚信度?现在教你一招:
1.当原图的宽或高任一比规定的尺寸小,只进行等比缩略处理,
2.当原图的宽与高都比规定尺寸大,先进行等比缩略处理,然后算出居中位置进行裁剪
<?php
/*
* $o_photo 原图路径.
* $d_photo 处理后图片路径.
* $width 定义宽.
* $height 定义高.
* 调用方法 cutphoto("test.jpg","temp.jpg",256,146);
*/
function cutphoto($o_photo,$d_photo,$width,$height){
$temp_img = imagecreatefromjpeg($o_photo);
$o_width = imagesx($temp_img); //取得原图宽.
$o_height = imagesy($temp_img); //取得原图高.
//判断处理方法.
if($width>$o_width || $height>$o_height){ //原图宽或高比规定的尺寸小,进行压缩.
$newwidth=$o_width;
$newheight=$o_height;
if($o_width>$width){
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
}
if($newheight>$height){
$newwidth=$newwidth*$height/$newheight;
$newheight=$height;
}
//缩略图片.
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
}else{ //原图宽与高都比规定尺寸大,进行压缩后裁剪.
if($o_height*$width/$o_width>$height){ //先确定width与规定相同,如果height比规定大,则ok.
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
$x=0;
$y=($newheight-$height)/2;
}else{ //否则确定height与规定相同,width自适应.
$newwidth=$o_width*$height/$o_height;
$newheight=$height;
$x=($newwidth-$width)/2;
$y=0;
}
//缩略图片.
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
$temp_img = imagecreatefromjpeg($d_photo);
$o_width = imagesx($temp_img); //取得缩略图宽.
$o_height = imagesy($temp_img); //取得缩略图高.
//裁剪图片.
$new_imgx = imagecreatetruecolor($width,$height);
imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);
imagejpeg($new_imgx , $d_photo);
imagedestroy($new_imgx);
}
}
?>
1.当原图的宽或高任一比规定的尺寸小,只进行等比缩略处理,
2.当原图的宽与高都比规定尺寸大,先进行等比缩略处理,然后算出居中位置进行裁剪
<?php
/*
* $o_photo 原图路径.
* $d_photo 处理后图片路径.
* $width 定义宽.
* $height 定义高.
* 调用方法 cutphoto("test.jpg","temp.jpg",256,146);
*/
function cutphoto($o_photo,$d_photo,$width,$height){
$temp_img = imagecreatefromjpeg($o_photo);
$o_width = imagesx($temp_img); //取得原图宽.
$o_height = imagesy($temp_img); //取得原图高.
//判断处理方法.
if($width>$o_width || $height>$o_height){ //原图宽或高比规定的尺寸小,进行压缩.
$newwidth=$o_width;
$newheight=$o_height;
if($o_width>$width){
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
}
if($newheight>$height){
$newwidth=$newwidth*$height/$newheight;
$newheight=$height;
}
//缩略图片.
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
}else{ //原图宽与高都比规定尺寸大,进行压缩后裁剪.
if($o_height*$width/$o_width>$height){ //先确定width与规定相同,如果height比规定大,则ok.
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
$x=0;
$y=($newheight-$height)/2;
}else{ //否则确定height与规定相同,width自适应.
$newwidth=$o_width*$height/$o_height;
$newheight=$height;
$x=($newwidth-$width)/2;
$y=0;
}
//缩略图片.
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
$temp_img = imagecreatefromjpeg($d_photo);
$o_width = imagesx($temp_img); //取得缩略图宽.
$o_height = imagesy($temp_img); //取得缩略图高.
//裁剪图片.
$new_imgx = imagecreatetruecolor($width,$height);
imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);
imagejpeg($new_imgx , $d_photo);
imagedestroy($new_imgx);
}
}
?>
作者: php华南培训 发布时间: 2010-01-30
标记
作者: energy162 发布时间: 2010-01-30
标记
这个标记的方法不错
这个标记的方法不错
作者: 福春 发布时间: 2010-01-30
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28