如何用插入ID替换图像名称

时间:2016-08-01 12:06:26

标签: php

基本上我正在创建上传图片的缩略图。我想将图像名称和缩略图图像存储为插入的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?

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分:0)

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

然后,您可以使用存储在$ newname变量

中的新名称调整图像大小