求程序,关于PHP生成缩略图的程序

szjq 2006-08-11 03:29:28
谁有用PHP上传图片同时并生成缩略图的程序,急用,谢谢大家

还有就是缩略图的宽和高是自动按比例生成?还是要手工设置呢??最好是按比例自动生成的,谢谢大家
...全文
267 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
szjq 2006-08-15
  • 打赏
  • 举报
回复
OK,谢谢!
yzxlyd 2006-08-11
  • 打赏
  • 举报
回复
function HrResize($source_img,$target_dir,$target_name,$new_width,$new_height,$if_cut)

$target_dir这个就是目录了。
颓废的老猫 2006-08-11
  • 打赏
  • 举报
回复
顶上先
tigerhu76 2006-08-11
  • 打赏
  • 举报
回复
mark先
szjq 2006-08-11
  • 打赏
  • 举报
回复
imagejpeg这个能显示出生成的缩略图,可是要把这个缩略图保存到一个目录里面,怎么弄????
onlyyouyz 2006-08-11
  • 打赏
  • 举报
回复
<?php
function LoadGif ($imgname) {
$im = @ImageCreateFromGIF ($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$im = ImageCreate (150, 30); /* Create a blank image */
$bgc = ImageColorAllocate ($im, 255, 255, 255);
$tc = ImageColorAllocate ($im, 0, 0, 0);
ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
ImageString($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
function LoadJpeg ($imgname) {
$im = @ImageCreateFromJPEG ($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$im = ImageCreate (150, 30); /* Create a blank image */
$bgc = ImageColorAllocate ($im, 255, 255, 255);
$tc = ImageColorAllocate ($im, 0, 0, 0);
ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
ImageString ($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
function runSmall($imgname){
$size = GetImageSize($imgname);

$img_type = substr(trim($imgname), -3, 3);
$img_type = strtoupper($img_type);
if ($img_type == "JPG" ) {
$src_im = LoadJpeg($imgname);
}elseif ($img_type == "GIF"){
$src_im = LoadGif($imgname);
}
$dst_im = imagecreatetruecolor (400,300);
imagepalettecopy( $dst_im, $src_im);
imagecopyresized( $dst_im, $src_im, 0, 0, 0, 0,400,300,$size[0],$size[1]);
if (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($dst_im);
}

}
runSmall($imageUrl);
?>
xwsn 2006-08-11
  • 打赏
  • 举报
回复
mark
yzxlyd 2006-08-11
  • 打赏
  • 举报
回复
<?PHP
class HanroadClass
{
/**//**********************
利用PHP的GD库生成缩略图。
支持图片格式:jpg,gif,png

$source_img: 源图象完整路径
$target_dir: 目标图象目录
$target_name: 目标图象名称
$new_width: 目标图象宽
$new_height: 目标图象高
$if_cut: 是否裁图
1(裁图): 裁图则按设置的大小生成目标图象
0(不裁): 不裁则按比例生成目标图象
**********************/
function HrResize($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);
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") imagegif($new_img,$tar_url);
if($img_type=="png") imagepng($new_img,$tar_url);
}
}
?>

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧