2016-08-01 60 views
0

基本上我正在創建上傳圖像的縮略圖。我想將圖像名稱和縮略圖圖像存儲爲插入的ID。 這裏是我的代碼:如何用插入ID替換圖像名稱

//Query of insert into database 
$pid = mysql_insert_id(); 
    $newname = "$pid.jpg"; 

通過上面的代碼,我可以得到圖像的ID。 以下代碼用於創建縮略圖。

function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){ 
    //folder path setup 
    $target_path = $target_folder; 
    $thumb_path = $thumb_folder; 

    //file name setup 
    /*echo $_FILES[$field_name][$newname]; 
    die();*/ 
    $filename_err = explode(".",$_FILES[$field_name]['name']); 
    $filename_err_count = count($filename_err); 
    $file_ext = $filename_err[$filename_err_count-1]; 
    if($file_name != '') 
    { 
    $fileName = $newname .'.'.$file_ext; 
    } 
    else 
    { 
    $fileName = $_FILES[$field_name]['name']; 
    } 

    //upload image path 
    $upload_image = $target_path.basename($fileName); 

    //upload image 
    if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image)) 
    { 
    //thumbnail creation 
    if($thumb == TRUE) 
    { 
     $thumbnail = $thumb_path.$fileName; 
     list($width,$height) = getimagesize($upload_image); 
     $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height); 
     switch($file_ext){ 
     case 'jpg': 
      $source = imagecreatefromjpeg($upload_image); 
      break; 
     case 'jpeg': 
      $source = imagecreatefromjpeg($upload_image); 
      break; 
     case 'png': 
      $source = imagecreatefrompng($upload_image); 
      break; 
     case 'gif': 
      $source = imagecreatefromgif($upload_image); 
      break; 
     default: 
      $source = imagecreatefromjpeg($upload_image); 
     } 
     imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height); 
     switch($file_ext){ 
     case 'jpg' || 'jpeg': 
      imagejpeg($thumb_create,$thumbnail,100); 
      break; 
     case 'png': 
      imagepng($thumb_create,$thumbnail,100); 
      break; 
     case 'gif': 
      imagegif($thumb_create,$thumbnail,100); 
      break; 
     default: 
      imagejpeg($thumb_create,$thumbnail,100); 
     } 
    } 

    return $fileName; 
    } 
    else 
    { 
    return false; 
    } 
} 


if(!empty($_FILES['image']['name'])){ 

    //call thumbnail creation function and store thumbnail name 
    $upload_img = cwUpload('image','uploads/','',TRUE,'uploads/thumbs/','200','160'); 

    //full path of the thumbnail image 
    $thumb_src = 'uploads/thumbs/'.$upload_img; 

    //set success and error messages 
    $message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>"; 

}else{ 

    //if form is not submitted, below variable should be blank 
    $thumb_src = ''; 
    $message = ''; 
} 

    header("location: product_listing.php"); 
    exit(); 
} 
?> 

如何插入並保存圖像和縮略圖圖像作爲插入的ID?

回答

1

你想添加這個名字移動到文件夾時

$pid = mysql_insert_id(); 
$newname = $pid.'.jpg'; 

//upload image path 
$upload_image = $target_path.$newname; 

//upload image 
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image)) 
0
//Query of insert into database 
$pid = mysql_insert_id(); 
    $newname = $pid.'.jpg'; 

然後你就可以調整存儲在$ NEWNAME變量新名稱圖像

+0

你能不能給我一個基本的例子那?請.. –