2013-02-28 82 views
0

我使用這個代碼,從形式的文章中獲取數據,保存物品的圖像,並顯示在一個新聞滑塊他們:上傳圖片併爲該圖片創建縮略圖?

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

我怎麼能增加一個功能保存縮略圖中的圖像這個代碼保存原始圖像的目錄相同嗎?

+0

作爲一個方面說明:'$ _REQUEST ['id']'可以讓你的數據庫變得如此乾淨而空虛,你永遠不會知道什麼命中您。在它前面添加'(int)':'$ id =(int)$ _ REQUEST ['id'];'。對於縮略圖,你必須先嚐試一些東西,或者你希望有人爲你製作代碼? – 2013-02-28 10:58:46

+0

謝謝米海那個筆記,(int)加了。 – user2053021 2013-02-28 11:02:24

回答

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類,它比我在這裏給出的函數更加健壯