2014-11-25 90 views
0

我有圖片上傳腳本,當我們上傳圖片或圖片時,功能調整大小,
從原始圖片製作兩張圖片,一張是205 * 256,另一張是205 * 256是800 * 500.圖像 調整大小工作正常,但問題是,當我上傳和調整圖像大小,然後 原始圖像也移動到目錄...我希望只有調整大小的圖像在我的 目錄。 ...現在的問題是,我怎麼能從目錄中刪除原始圖像...我 不想在原始圖像的目錄..原始圖像不能移動到目錄..提前感謝!如何在調整大小後刪除原始圖像

<?php 
if($_POST['btnSubmit']) 
{ 
for($i=0;$i<count($_FILES["fileUpload"]["name"]);$i++) 
{ 
if(trim($_FILES["fileUpload"]["tmp_name"][$i] !="")) 
{ 
$images = $_FILES["fileUpload"]["tmp_name"][$i]; 
list($width, $height, $type, $attr) = getimagesize($images); 
if($width>600 && $height>480) 
{ 
$new_images = "Thumbnails_".$_FILES["fileUpload"]["name"][$i]; 
move_uploaded_file($_FILES["fileUpload"]["tmp_name"][$i],"$output_dir".$_FILES["fileUpload"] ["name"][$i]); 
$res = resize(800,500,$_FILES["fileUpload"]["name"][$i],$_FILES["fileUpload"]["type"][$i]); 
$width=205; 
$height=256; 
$size=GetimageSize($res); 
$images_orig = ImageCreateFromJPEG($res); 
$photoX = ImagesX($images_orig); 
$photoY = ImagesY($images_orig); 
$images_fin = ImageCreateTrueColor($width, $height); 
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); 
ImageJPEG($images_fin,"$output_dir".$new_images); 
ImageDestroy($images_orig); 
ImageDestroy($images_fin); 
echo "Resize Successful.<br>"; 
$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database"); 
$objDB = mysql_select_db("datbase"); 
$strSQL = "INSERT INTO gallery "; 
$strSQL .="(id,filename,Thumbnails) VALUES ('','".$_FILES["fileUpload"]["name"][$i]."','".$new_images."')"; 
$objQuery = mysql_query($strSQL); 


} 
else 
{ 
//upload the image with out changing 
} 

?> 



//This is the resize function 
<?php 
$output_dir = "MyResize/"; 
function resize($width, $height,$image,$type){ 
global $output_dir; 
$relPath = $output_dir.$image; 
list($w, $h) = getimagesize($relPath); 
/* calculate new image size with ratio */ 
$ratio = max($width/$w, $height/$h); 
$h = ceil($height/$ratio); 
$x = ($w - $width/$ratio)/2; 
$w = ceil($width/$ratio); 
/* new file name */ 
$thumb = $output_dir.$width.'x'.$height.'_'.$image; 
//echo $thumb; exit; 
/* read binary data from image file */ 
$imgString = file_get_contents($relPath); 
//echo $imgString ; exit; 
/* create image from string */ 
$image = imagecreatefromstring($imgString); 
//echo $image; exit; 
$tmp = imagecreatetruecolor($width, $height); 
imagecopyresampled($tmp, $image, 
0, 0, 
$x, 0, 
$width, $height, 
$w, $h); 
/* Save image */ 
switch ($type) { 
    case 'image/jpeg': 
    { 
     //echo $tmp; exit; 
     imagejpeg($tmp,$thumb,100); 
     break; 

    } 
    case 'image/jpg': 
    { 
     imagejpeg($tmp,$thumb,100); 
     break; 

    } 
    case 'image/png': 
    { 
     imagepng($tmp, $thumb, 0); 
     break; 
    } 
    case 'image/gif': 
    { 
     imagegif($tmp, $thumb); 
     break; 
    } 
    default: 
     break; 
} 
return $thumb; 
/* cleanup memory */ 
imagedestroy($image); 
imagedestroy($tmp); 
} 
?> 
+1

'的unlink刪除原始圖片($ originalImagePath);' – 2014-11-25 05:23:44

+0

刪除了cakephp標誌,因爲這是cleary,甚至沒有遠程cakephp相關。 – mark 2014-11-25 06:09:58

回答

2

刪除圖像,使用unlink('Path to the file')

到Unix下C的unlink類似()函數。將在故障產生的E_WARNING級錯誤

+0

我把取消鏈接($ output_dir)調整大小調用函數後,但它不起作用 – 2014-11-25 05:43:14

+0

$ output_dir應該包含路徑,直到圖像名稱,例如路徑/到/ test.jpg – Pankaj 2014-11-25 05:53:35

+0

感謝pankaj,yar tuse大浩! – 2014-11-25 06:22:24

2

第一作物的圖像並存儲該圖像,然後使用取消鏈接刪除原始圖片

unlink('Orignal image path'); 

的unlink將從文件夾

+0

我把取消鏈接($ output_dir)後調整大小的函數調用,但它不工作.. – 2014-11-25 05:42:02

+0

親愛的阿卜杜勒Mr.pankaj是正確的,你必須提供整個路徑..... – Affan 2014-11-25 07:22:35

相關問題