Home > Article > Backend Development > php生成缩略图的效能
php生成缩略图的功能
客户自己上传的图片大小不标准,想让传上来的图片统一变成150X113尺寸的。从网上搜到的程序在IE里用正常,到了别的浏览器(如搜狗浏览器等)就不行,这是什么原因?(感觉老是不执行)
代码如下:
session_start();
if (isset($_SESSION['adminname']) && isset($_SESSION['adminpass'])){
//****************************************
//生成缩略图========================================
$myname=date("YmdHis");
$filename="product/min/".$myname; //小图片文件名
$resizewidth=150; // 生成图片的宽度
$resizeheight=113; // 生成图片的高度
function ResizeImage($im,$maxwidth,$maxheight,$name){
$resizewidth = $resizeheight = false;
$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 $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,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}
if(isset($_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);
echo "上传成功,图片文件名是:";
echo $myname.".jpg";
}
}
//****************************************
}else{
echo "<script> window.location='login.php'</script>";
}
?>