上传图片并为该图片创建缩略图?
问题描述:
我使用这个代码,从形式的文章中获取数据,保存物品的图像,并显示在一个新闻滑块他们:上传图片并为该图片创建缩略图?
if(isset($_POST['submit']))
{
unset($_SESSION['firsttitle']);
unset($_SESSION['firstcontent']);
unset($_SESSION['title']);
unset($_SESSION['content']);
unset($_SESSION['thirdcontent']);
$_SESSION['title']=mysql_real_escape_string(trim($_POST['title']));
$_SESSION['content']=mysql_real_escape_string(trim($_POST['content']));
$weight=htmlentities(trim($_REQUEST['weight']),ENT_QUOTES);
$id=$_REQUEST['id'];
$time=mktime();
if($_FILES['picture']['name'])
{
$select="select * from ".$prev."news where id='$id'";
$re_select=mysql_query($select);
$d_select=mysql_fetch_array($re_select);
if($d_select['picture']!='')
{
unlink("../".$d_select['picture']);
}
$picture="upload/".$time.$_FILES['picture']['name'];
$path="../".$picture;
move_uploaded_file($_FILES['picture']['tmp_name'],$path);
}
else
{
$picture=$_POST['picturepath'];
}
if($id=='0' || !$_REQUEST['id'])
{
$insert="insert into ".$prev."news (title,content,weight,picture,status,postedtime) values('".$_SESSION['title']."','".$_SESSION['content']."','$weight','$picture','Y','$time')";
mysql_query($insert);
$lastid=mysql_insert_id();
unset($_SESSION['title']);
unset($_SESSION['content']);
@header("location:news-addnew.php?id=$lastid");
}
else
{
$update="update ".$prev."news set title='".$_SESSION['title']."',content='".$_SESSION['content']."',weight='$weight',picture='$picture',status='$status',postedtime='$time' where id='$id'";
mysql_query($update);
unset($_SESSION['title']);
unset($_SESSION['content']);
@header("location:news-addnew.php?id=$id");
}
}
我怎么能增加一个功能保存缩略图中的图像这个代码保存原始图像的目录相同吗?
答
看看这个链接:http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
这里只是调整大小功能:
function resize($filename, $newfile, $width,$height) {
// open the original file
$image = imagecreatefromjpeg($filename);
// create a new blank image
$new_image = imagecreatetruecolor($width, $height);
// copy the original, according to the specified width and height
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));
// save the new file to $newfile
imagejpeg ($new_image, $newfile);
}
更多细节该链接。你应该使用SimpleImage类,它比我在这里给出的函数更加健壮
作为一个方面说明:'$ _REQUEST ['id']'可以让你的数据库变得如此干净而空虚,你永远不会知道什么命中您。在它前面添加'(int)':'$ id =(int)$ _ REQUEST ['id'];'。对于缩略图,你必须先尝试一些东西,或者你希望有人为你制作代码? – 2013-02-28 10:58:46
谢谢米海那个笔记,(int)加了。 – user2053021 2013-02-28 11:02:24