首页 > 开发 > PHP > 正文

可定制的PHP缩略图生成程式(需要GD库支持)

2024-05-04 22:59:23
字体:
来源:转载
供稿:网友

经典的php缩略图生成程式,基于gd库,可指定生成路径及生成目标的宽高细节

使用方法: 在支持gd库的php环境中,将以下代码另存为resize.php测试

<?

$filename
="image_name";

// 生成图片的宽度
$resizewidth=400;

// 生成图片的高度
$resizeheight=400;

//生成图片的路径
$uploaddir="c:/winnt/temp";

function
resizeimage($im,$maxwidth,$maxheight,$name){
global
$uploaddir;
$width = imagesx($im);
$height = imagesy($im);
if((
$maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if(
$maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$resizewidth=true;
}
if(
$maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$resizeheight=true;
}
if(
$resizewidth && $resizeheight){
if(
$widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif(
$resizewidth){
$ratio = $widthratio;
}elseif(
$resizeheight){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(
function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
imagejpeg ($newim,$uploaddir.$name . ".jpg");
imagedestroy ($newim);
}else{
imagejpeg ($im,$uploaddir.$name . ".jpg");
}
}



if(
$_files['image']['size']){
if(
$_files['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_files['image']['tmp_name']);
}elseif(
$_files['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_files['image']['tmp_name']);
}elseif(
$_files['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_files['image']['tmp_name']);
}
if(
$im){
if(
file_exists("$filename.jpg")){
unlink("$filename.jpg");
}
resizeimage($im,$resizewidth,$resizeheight,$filename);
imagedestroy ($im);
}
}
?>

<img src="<? echo($filename.".jpg?reload=".rand(0,999999)); ?>"><br><br>

<form enctype="multipart/form-data" method="post">
<br>
<input type="file" name="image" size="50" value="浏览"><p>
<input type="submit" value="上传图片">
</form>

</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表