php上传图片生成链接,PHP上传图片并生成缩略图

主目录下有3文件

ratioimg.html

ratioimg.php

image文件夹,请新建此文件夹

ratioimg.html

-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

上传图片

ratioimg.php

-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

缩略图生成

/***************************************/

/*功 能:利用PHP的GD库生成高质量的缩略图*/

/*运行环境:PHP5.01/GD2*/

/*类说明:可以选择是/否裁图。

方法一:如果裁图则生成的图的尺寸与您输入的一样。

原则:尽可能多保持原图完整

方法二:如果裁图则生成的图的尺寸与原图片等比例缩小。

原则:保持缩略图的完整

如果不裁图,则按照原图比例生成新图

原则:根据比例以输入的长或者宽为基准*/

/*参 数:$img:源图片地址

$wid:新图的宽度

$hei:新图的高度

$c:是否裁图,1为是,0为否*/

/* Author: caowlong         */

/* QQ:      1226454285        */

/***************************************/

class resizeimage

{

var $type;   //图片类型

var $width;  //实际宽度

var $height; //实际高度

var $resize_width; //改变后的宽度

var $resize_height;//改变后的高度

var $cut;          //是否裁图

var $srcimg;       //源图象

var $dstimg;       //目标图象地址

var $im;           //临时创建的图象

function resizeimage($img,$c, $wid, $hei=”)

{

//echo $img.$wid.$hei.$c;

$this->srcimg = $img;

$this->resize_width = $wid;

$this->resize_height = $hei;

$this->cut = $c;

//图片的类型

$this->type = substr(strrchr($this->srcimg,”.”),1);

//初始化图象

$this->initi_img();

//目标图象地址

$this -> dst_img();

//imagesx imagesy 取得图像 宽、高

$this->width = imagesx($this->im);

$this->height = imagesy($this->im);

//生成图象

$this->newimg();

ImageDestroy ($this->im);

}

function newimg()

{

// +—————————————————-+

// | 增加LOGO到缩略图中 Modify By Ice

// +—————————————————-+

//Add Logo

//$logoImage = ImageCreateFromJPEG(‘t_a.jpg’);

//ImageAlphaBlending($this->im, true);

//$logoW = ImageSX($logoImage);

//$logoH = ImageSY($logoImage);

// +—————————————————-+

//改变后的图象的比例

//$resize_ratio = ($this->resize_width)/($this->resize_height);

//实际图象的比例

//$ratio = ($this->width)/($this->height);

if(($this->cut)==”1″)

//裁图

{

/*

//方法一:此方法按预定大小裁剪,如用此方法,需开启 “改变后的图象的比例” 和 “实际图象的比例” 两个计算公式

if($ratio>=$resize_ratio)

//高度优先

{

//imagecreatetruecolor — 新建一个真彩色图像

$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);

//imagecopyresampled — 重采样拷贝部分图像并调整大小

imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);

// +—————————————————-+

// | 增加LOGO到缩略图中 Modify By Ice

// +—————————————————-+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +—————————————————-+

//imagejpeg — 以 JPEG 格式将图像输出到浏览器或文件

ImageJpeg ($newimg,$this->dstimg);

echo “缩略图生成成功!”;

}

if($ratio

//宽度优先

{

$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);

imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));

// +—————————————————-+

// | 增加LOGO到缩略图中 Modify By Ice

// +—————————————————-+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +—————————————————-+

ImageJpeg ($newimg,$this->dstimg);

echo “缩略图生成成功!”;

}

*/

//方法二:此方法按比例裁剪 即约定宽,高度则自动按比例生成  默认是此方法,如需缩略图按120*120这样的尺寸选方法一即可

//imagecreatetruecolor — 新建一个真彩色图像

//$this->resize_width=50;

$this->resize_height=($this->height*$this->resize_width)/$this->width;

$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);

//imagecopyresampled — 重采样拷贝部分图像并调整大小

imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,$this->resize_width, $this->resize_height, $this->width, $this->height);

// +—————————————————-+

// | 增加LOGO到缩略图中 Modify By Ice

// +—————————————————-+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +—————————————————-+

//imagejpeg — 以 JPEG 格式将图像输出到浏览器或文件

ImageJpeg ($newimg,$this->dstimg);

echo “缩略图生成成功!”;

}

else

//不裁图

{

if($ratio>=$resize_ratio)

{

$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);

imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);

// +—————————————————-+

// | 增加LOGO到缩略图中 Modify By Ice

// +—————————————————-+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +—————————————————-+

ImageJpeg ($newimg,$this->dstimg);

echo “缩略图生成成功!”;

}

if($ratio

{

$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);

imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);

// +—————————————————-+

// | 增加LOGO到缩略图中 Modify By Ice

// +—————————————————-+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +—————————————————-+

ImageJpeg ($newimg,$this->dstimg);

echo “缩略图生成成功!”;

}

}

// +—————————————————-+

// | 释放资源 Modify By Ice

// +—————————————————-+

//ImageDestroy($logoImage);

// +—————————————————-+

}

//初始化图象

function initi_img()

{

if($this->type==”jpg”)

{

@ $this->im = imagecreatefromjpeg($this->srcimg);

}

if($this->type==”gif”)

{

@ $this->im = imagecreatefromgif($this->srcimg);

}

if($this->type==”png”)

{

@ $this->im = imagecreatefrompng($this->srcimg);

}

}

//图象目标地址

function dst_img()

{

$full_length = strlen($this->srcimg);

$type_length = strlen($this->type);

$name_length = $full_length-$type_length;

$name         = substr($this->srcimg,0,$name_length-1);

$this->dstimg = $name.”_small.”.$this->type;

}

}

/*

print_r($_FILES[“file”]);

foreach ($_FILES[“file”] as $key => $a)

{

echo $key.”=============”.$a.”
”;

}

*/

$tempimgname = strtolower($_FILES[“file”][name]);

//mb_convert_encoding转换传输字集encod

$tempimgname = mb_convert_encoding( $tempimgname, “gb2312” , “utf-8″);

//echo $tempimgname;

$tmpfiletype = substr(strrchr($tempimgname,”.”),1);

if($tmpfiletype==”jpg” || $tmpfiletype==”gif” || $tmpfiletype==”png”)

{

$filename=date(“YmdHis-“,time()).$tempimgname;

if(copy($_FILES[“file”][“tmp_name”],strtolower(“image\\”.$filename)))

{

//输出原来的文件名,大小,类型

//echo “文件”.strtolower($_FILES[“file”][“name”]).”
”.”大小”.$_FILES[“file”][“size”].”
”.”文件类型为”.$_FILES[“file”] [“type”].”
”.”文件上传成功!”.”
”;

echo “原图片:”.$filename.”
”.”缩略图:”.substr($filename,0,strpos($filename,’.’)).”_small”.substr($filename,strpos($filename,’.’)).”
”.”原图大小:”.$_FILES[“file”][“size”].”
”.”文件类型:”.$_FILES[“file”] [“type”].”
”.”文件上传成功!”.”
”;

}

else

{

echo “文件上传失败”.”
”;

}

$class = new resizeimage(“image\\”.$filename, 1,80,”);

}

else

{

echo “上传文件类型错误,请勿上传除jpg,gif,png以外的其他文件。”;

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值