php大图生成小图代码
这是一款利用php自带的功能把指定的大图生成我们指定大小的缩略图代码,使用方便简单,只要把设置下面四个参数就可以生成自己想的大小的缩略图了,代码如下:
<?php
function bigtosmallimg($file, $path, $w = 120, $h = 90) {
$img = $path . $file;
$imgarr = getimagesize($img);
$sw = $imgarr[0]; //原图宽
$sh = $imgarr[1]; //原图高
$stype = $imgarr[2];
//按比例缩放
if ($sw / $sh > $w / $h) {
$mw = $w;
$mh = (int)$sh * ($w / $sw);
} else {
$mw = (int)$sw * ($h / $sh);
$mh = $h;
}
switch ($stype) { //根据上传好的图形文件类型新建一个用来生成缩略图的源文件。
case 1:
$srcf = imagecreatefromgif($img);
break;
case 2:
$srcf = imagecreatefromjpeg($img);
break;
case 3:
$srcf = imagecreatefrompng($img);
break;
default:
showmsg('程序调用错误。');
break;
}
$desf = imagecreatetruecolor($mw, $mh);
imagecopyresampled($desf, $srcf, 0, 0, 0, 0, $mw, $mh, $sw, $sh);
$sm_name = $path . "s_" . $file;
switch ($stype) {
case 1:
imagegif($desf, $sm_name);
break;
case 2:
imagejpeg($desf, $sm_name);
break;
case 3:
imagepng($desf, $sm_name);
break;
default:
showmsg('无法生成www.phprm.com' . $stype . '的缩略图。');
break;
}
imagedestroy($desf);
imagedestroy($srcf);
}
//此缩略图调用方法,代码如下:
bigtosmallimg($file, $path, $w = 120, $h = 90);
/*
$file = 图片的路径
$path = 生成后保存的路径
$w =图片宽度
$h =图片高度
*/本文地址:http://www.phprm.com/tuxiang/fs4481.html
转载随意,但请附上文章地址:-)