如何将用户上传过来的图片该成统一大小? RT 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用php gd 新建画布,再把图片按比例压缩然后发在新建画布下就行了... <?php//调整图片大小/** *图片按比例调整大小的原理: *1、比较原图大小是否小于等于目标大小,如果是则直接采用原图宽高 *2、如果原图大小超过目标大小,则对比原图宽高大小 *3、如:宽>高,则宽=目标宽, 高=目标宽的比例 * 原高 *4、如:高>宽,则高=目标高,宽=目标高的比例 * 原宽 **/$image = $_REQUEST['image'];//$image = 'C:/Documents and Settings/TCM/My Documents/Water lilies.jpg';$max_width = $_REQUEST['max_width'];//$max_width = 200;$max_height = $_REQUEST['max_height'];//$max_height = 200;if (!$max_width) { $max_width = 80;}if (!$max_height) { $max_height = 60;}$size = getimagesize($image); //得到图像的大小$width = $size[0]; $height = $size[1];$x_ratio = $max_width / $width;$y_ratio = $max_height / $height;if (($width <= $max_width) && ($height <= $max_height)){ $tn_width = $width; $tn_height = $height;}elseif (($x_ratio * $height) < $max_height){ $tn_height = ceil($x_ratio * $height); $tn_width = $max_width;}else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height;}$src = imagecreatefromjpeg($image);$dst = imagecreatetruecolor($tn_width, $tn_height); //新建一个真彩色图像imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height); //重采样拷贝部分图像并调整大小header('Content-Type: image/jpeg');imagejpeg($dst,null,100);imagedestroy($src);imagedestroy($dst);?>本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/caleng/archive/2008/01/11/2037545.aspx 3L的就可以了原理就是先上传,再对上传上来的图片进行imagecopyresampled处理,生成新尺寸的图片,这种新生成的图片在尺寸上的变化同时也影响到图片的数据大小。 queryphp有处理图片大小类在lib/img.class.php可以不压扁图片缩放到指定大小。自动居中切图http://code.google.com/p/queryphp/downloads/list function thumb($source_img,$target_dir,$target_name,$new_width,$new_height,$if_cut) { $img_type = strtolower(substr(strrchr($source_img,"."),1)); $tar_url = $target_dir."/".$target_name.".".$img_type; if($img_type=="jpg")$temp_img = imagecreatefromjpeg($source_img); if($img_type=="gif")$temp_img = imagecreatefromgif($source_img); if($img_type=="png")$temp_img = imagecreatefrompng($source_img); $old_width = imagesx($temp_img); $old_height = imagesy($temp_img); $new_ratio = $new_width/$new_height; $old_ratio = $old_width/$old_height; if($if_cut=="1"){ $new_width = $new_width; $new_height = $new_height; if($old_ratio>=$new_ratio){ $old_width = $old_height*$new_ratio; $old_height = $old_height; }else{ $old_width = $old_width; $old_height = $old_width/$new_ratio; } }else{ $old_width = $old_width; $old_height = $old_height; if($old_ratio>=$new_ratio) { $new_width = $new_width; $new_height = $new_width/$old_ratio; }else{ $new_width = $new_height*$old_ratio; $new_height = $new_height; } } $new_img = imagecreatetruecolor($new_width,$new_height); if($img_type=="gif"){ imagealphablending($new_img, false); imagesavealpha($new_img, true); } if($img_type=="png"){ imagesavealpha($temp_img,true); imagealphablending($new_img,false); imagesavealpha($new_img,true); } imagecopyresampled($new_img,$temp_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height); if($img_type=="jpg")imagejpeg($new_img,$tar_url); if($img_type=="gif"){ $bgcolor=imagecolorallocate($new_img,0,0,0); $bgcolor=imagecolortransparent($new_img,$bgcolor); $bgcolor=imagecolorallocatealpha($new_img, 0, 0, 0,127); imagefill($new_img, 0, 0, $bgcolor); imagegif($new_img,$tar_url); } if($img_type=="png")imagepng($new_img,$tar_url); imagedestroy($source_img); imagedestroy($new_img); } 可以参考一下uchome里的图片处理方式,挺不错的//生成缩略图function makethumb($srcfile) { global $_SGLOBAL; //判断文件是否存在 if (!file_exists($srcfile)) { return ''; } $dstfile = $srcfile.'.thumb.jpg'; include_once(S_ROOT.'./data/data_setting.php'); //缩略图大小 $tow = intval($_SGLOBAL['setting']['thumbwidth']); $toh = intval($_SGLOBAL['setting']['thumbheight']); if($tow < 60) $tow = 60; if($toh < 60) $toh = 60; $make_max = 0; $maxtow = intval($_SGLOBAL['setting']['maxthumbwidth']); $maxtoh = intval($_SGLOBAL['setting']['maxthumbheight']); if($maxtow >= 300 && $maxtoh >= 300) { $make_max = 1; } //获取图片信息 $im = ''; if($data = getimagesize($srcfile)) { if($data[2] == 1) { $make_max = 0;//gif不处理 if(function_exists("imagecreatefromgif")) { $im = imagecreatefromgif($srcfile); } } elseif($data[2] == 2) { if(function_exists("imagecreatefromjpeg")) { $im = imagecreatefromjpeg($srcfile); } } elseif($data[2] == 3) { if(function_exists("imagecreatefrompng")) { $im = imagecreatefrompng($srcfile); } } } if(!$im) return ''; $srcw = imagesx($im); $srch = imagesy($im); $towh = $tow/$toh; $srcwh = $srcw/$srch; if($towh <= $srcwh){ $ftow = $tow; $ftoh = $ftow*($srch/$srcw); $fmaxtow = $maxtow; $fmaxtoh = $fmaxtow*($srch/$srcw); } else { $ftoh = $toh; $ftow = $ftoh*($srcw/$srch); $fmaxtoh = $maxtoh; $fmaxtow = $fmaxtoh*($srcw/$srch); } if($srcw <= $maxtow && $srch <= $maxtoh) { $make_max = 0;//不处理 } if($srcw > $tow || $srch > $toh) { if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) { imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch); //大图片 if($make_max && @$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh)) { imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch); } } elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) { imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch); //大图片 if($make_max && @$maxni = imagecreate($fmaxtow, $fmaxtoh)) { imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch); } } else { return ''; } if(function_exists('imagejpeg')) { imagejpeg($ni, $dstfile); //大图片 if($make_max) { imagejpeg($maxni, $srcfile); } } elseif(function_exists('imagepng')) { imagepng($ni, $dstfile); //大图片 if($make_max) { imagepng($maxni, $srcfile); } } imagedestroy($ni); if($make_max) { imagedestroy($maxni); } } imagedestroy($im); if(!file_exists($dstfile)) { return ''; } else { return $dstfile; }} 按指定大小进行等比缩放//缩放图片/** * * *等比缩放 * @param unknown_type $srcImage 源图片路径 * @param unknown_type $toFile 目标图片路径 * @param unknown_type $maxWidth 最大宽 * @param unknown_type $maxHeight 最大高 * @param unknown_type $imgQuality 图片质量 * @return unknown */function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100){ list($width, $height, $type, $attr) = getimagesize($srcImage); if($width < $maxWidth || $height < $maxHeight) return ; switch ($type) { case 1: $img = imagecreatefromgif($srcImage); break; case 2: $img = imagecreatefromjpeg($srcImage); break; case 3: $img = imagecreatefrompng($srcImage); break; } $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 if($scale < 1) { $newWidth = floor($scale*$width); $newHeight = floor($scale*$height); $newImg = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); $newName = ""; $toFile = preg_replace("/(\.gif|\.jpg|\.jpeg|\.png)/i","",$toFile); switch($type) { case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality)) return "$newName.gif"; break; case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) return "$newName.jpg"; break; case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) return "$newName.png"; break; default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) return "$newName.jpg"; break; } imagedestroy($newImg); } imagedestroy($img); return false;} 发个缩略图专题页面吧。http://www.111cn.net/search.php?keyword=%CB%F5%C2%D4%CD%BC&p=1 哥们,到处都看到你在推广queryPHP,没用过queryPHP,不过敬业精神值得敬佩, 如何下载网站上的文件啊? 请求高手解决(怎么把这样的数组转换为JSON) 跪求 看看这段html 求助,求解决方法 日了,我电脑里的Apache抽风了??? 服务器采用vps,php+mysql+apache 如何优化内存 svn自动跟新程序 lnmp面板 - AMH3.0 有谁在玩WordPress么?求助一个问题 求高效的缓存类,每秒访问1W次页面 服务器扛不住 memcache 正则表达式提取问题
<?php
//调整图片大小
/**
*图片按比例调整大小的原理:
*1、比较原图大小是否小于等于目标大小,如果是则直接采用原图宽高
*2、如果原图大小超过目标大小,则对比原图宽高大小
*3、如:宽>高,则宽=目标宽, 高=目标宽的比例 * 原高
*4、如:高>宽,则高=目标高,宽=目标高的比例 * 原宽
**/$image = $_REQUEST['image'];
//$image = 'C:/Documents and Settings/TCM/My Documents/Water lilies.jpg';$max_width = $_REQUEST['max_width'];
//$max_width = 200;
$max_height = $_REQUEST['max_height'];
//$max_height = 200;if (!$max_width)
{
$max_width = 80;
}if (!$max_height)
{
$max_height = 60;
}$size = getimagesize($image); //得到图像的大小
$width = $size[0];
$height = $size[1];$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;if (($width <= $max_width) && ($height <= $max_height))
{
$tn_width = $width;
$tn_height = $height;
}
elseif (($x_ratio * $height) < $max_height)
{
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else
{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}$src = imagecreatefromjpeg($image);
$dst = imagecreatetruecolor($tn_width, $tn_height); //新建一个真彩色图像
imagecopyresampled($dst, $src, 0, 0, 0, 0,
$tn_width, $tn_height, $width, $height); //重采样拷贝部分图像并调整大小
header('Content-Type: image/jpeg');
imagejpeg($dst,null,100);
imagedestroy($src);
imagedestroy($dst);
?>本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/caleng/archive/2008/01/11/2037545.aspx
原理就是先上传,再对上传上来的图片进行imagecopyresampled处理,生成新尺寸的图片,这种新生成的图片在尺寸上的变化同时也影响到图片的数据大小。
function thumb($source_img,$target_dir,$target_name,$new_width,$new_height,$if_cut) {
$img_type = strtolower(substr(strrchr($source_img,"."),1));
$tar_url = $target_dir."/".$target_name.".".$img_type;
if($img_type=="jpg")$temp_img = imagecreatefromjpeg($source_img);
if($img_type=="gif")$temp_img = imagecreatefromgif($source_img);
if($img_type=="png")$temp_img = imagecreatefrompng($source_img);
$old_width = imagesx($temp_img);
$old_height = imagesy($temp_img);
$new_ratio = $new_width/$new_height;
$old_ratio = $old_width/$old_height;
if($if_cut=="1"){
$new_width = $new_width;
$new_height = $new_height;
if($old_ratio>=$new_ratio){
$old_width = $old_height*$new_ratio;
$old_height = $old_height;
}else{
$old_width = $old_width;
$old_height = $old_width/$new_ratio;
}
}else{
$old_width = $old_width;
$old_height = $old_height;
if($old_ratio>=$new_ratio) {
$new_width = $new_width;
$new_height = $new_width/$old_ratio;
}else{
$new_width = $new_height*$old_ratio;
$new_height = $new_height;
}
}
$new_img = imagecreatetruecolor($new_width,$new_height);
if($img_type=="gif"){
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
}
if($img_type=="png"){
imagesavealpha($temp_img,true);
imagealphablending($new_img,false);
imagesavealpha($new_img,true);
}
imagecopyresampled($new_img,$temp_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height);
if($img_type=="jpg")imagejpeg($new_img,$tar_url);
if($img_type=="gif"){
$bgcolor=imagecolorallocate($new_img,0,0,0);
$bgcolor=imagecolortransparent($new_img,$bgcolor);
$bgcolor=imagecolorallocatealpha($new_img, 0, 0, 0,127);
imagefill($new_img, 0, 0, $bgcolor);
imagegif($new_img,$tar_url);
}
if($img_type=="png")imagepng($new_img,$tar_url);
imagedestroy($source_img);
imagedestroy($new_img);
}
//生成缩略图
function makethumb($srcfile) {
global $_SGLOBAL; //判断文件是否存在
if (!file_exists($srcfile)) {
return '';
}
$dstfile = $srcfile.'.thumb.jpg';
include_once(S_ROOT.'./data/data_setting.php'); //缩略图大小
$tow = intval($_SGLOBAL['setting']['thumbwidth']);
$toh = intval($_SGLOBAL['setting']['thumbheight']);
if($tow < 60) $tow = 60;
if($toh < 60) $toh = 60; $make_max = 0;
$maxtow = intval($_SGLOBAL['setting']['maxthumbwidth']);
$maxtoh = intval($_SGLOBAL['setting']['maxthumbheight']);
if($maxtow >= 300 && $maxtoh >= 300) {
$make_max = 1;
}
//获取图片信息
$im = '';
if($data = getimagesize($srcfile)) {
if($data[2] == 1) {
$make_max = 0;//gif不处理
if(function_exists("imagecreatefromgif")) {
$im = imagecreatefromgif($srcfile);
}
} elseif($data[2] == 2) {
if(function_exists("imagecreatefromjpeg")) {
$im = imagecreatefromjpeg($srcfile);
}
} elseif($data[2] == 3) {
if(function_exists("imagecreatefrompng")) {
$im = imagecreatefrompng($srcfile);
}
}
}
if(!$im) return '';
$srcw = imagesx($im);
$srch = imagesy($im);
$towh = $tow/$toh;
$srcwh = $srcw/$srch;
if($towh <= $srcwh){
$ftow = $tow;
$ftoh = $ftow*($srch/$srcw);
$fmaxtow = $maxtow;
$fmaxtoh = $fmaxtow*($srch/$srcw);
} else {
$ftoh = $toh;
$ftow = $ftoh*($srcw/$srch);
$fmaxtoh = $maxtoh;
$fmaxtow = $fmaxtoh*($srcw/$srch);
}
if($srcw <= $maxtow && $srch <= $maxtoh) {
$make_max = 0;//不处理
}
if($srcw > $tow || $srch > $toh) {
if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) {
imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
//大图片
if($make_max && @$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh)) {
imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
}
} elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) {
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
//大图片
if($make_max && @$maxni = imagecreate($fmaxtow, $fmaxtoh)) {
imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
}
} else {
return '';
}
if(function_exists('imagejpeg')) {
imagejpeg($ni, $dstfile);
//大图片
if($make_max) {
imagejpeg($maxni, $srcfile);
}
} elseif(function_exists('imagepng')) {
imagepng($ni, $dstfile);
//大图片
if($make_max) {
imagepng($maxni, $srcfile);
}
}
imagedestroy($ni);
if($make_max) {
imagedestroy($maxni);
}
}
imagedestroy($im); if(!file_exists($dstfile)) {
return '';
} else {
return $dstfile;
}
}
/**
* *
*等比缩放
* @param unknown_type $srcImage 源图片路径
* @param unknown_type $toFile 目标图片路径
* @param unknown_type $maxWidth 最大宽
* @param unknown_type $maxHeight 最大高
* @param unknown_type $imgQuality 图片质量
* @return unknown
*/function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{
list($width, $height, $type, $attr) = getimagesize($srcImage);
if($width < $maxWidth || $height < $maxHeight) return ;
switch ($type) {
case 1: $img = imagecreatefromgif($srcImage); break;
case 2: $img = imagecreatefromjpeg($srcImage); break;
case 3: $img = imagecreatefrompng($srcImage); break;
}
$scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
if($scale < 1) {
$newWidth = floor($scale*$width);
$newHeight = floor($scale*$height);
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newName = "";
$toFile = preg_replace("/(\.gif|\.jpg|\.jpeg|\.png)/i","",$toFile); switch($type) {
case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
return "$newName.gif"; break;
case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
return "$newName.png"; break;
default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
}
imagedestroy($newImg);
}
imagedestroy($img);
return false;
}
http://www.111cn.net/search.php?keyword=%CB%F5%C2%D4%CD%BC&p=1