上传图片并为该图片创建缩略图?

时间:2013-02-28 10:54:16

标签: php

我正在使用此代码从文章表格中获取数据,保存文章图像并在新闻滑块中显示:

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");
}
}

如何在保存原始图像的同一目录中添加保存图像缩略图的功能?

1 个答案:

答案 0 :(得分:0)

看一下这个链接: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类,它比我在这里提供的函数更强大