PDA

View Full Version : نحوه نمایش عکس از بانک mysql



mani2000
جمعه 07 آبان 1395, 08:29 صبح
سلام من با این کد عکس رو در بانک ذخیره کردم





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

<?php
if(!isset($_FILES['upload_image']))
{
echo '<p>choose image</p>';
}
else
{
try {
upload();

echo '<p>success</p>';
echo '<p>ok</p>';
}
catch(Exception $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}
}


function upload(){


if(is_uploaded_file($_FILES['upload_image']['tmp_name']) && getimagesize($_FILES['upload_image']['tmp_name']) != false)
{



$size = getimagesize($_FILES['upload_image']['tmp_name']);



$type = $size['mime'];
$imgfp = fopen($_FILES['upload_image']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['upload_image']['name'];
$maxsize = 99999999;




if($_FILES['upload_image']['size'] < $maxsize )
{



$dbh = new PDO("mysql:host=localhost;dbname=bank", 'root', '');




$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);




$stmt = $dbh->prepare("INSERT INTO insert (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)");




$stmt->bindParam(1, $type);
$stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
$stmt->bindParam(3, $size);
$stmt->bindParam(4, $name);




$stmt->execute();
}
else
{
throw new Exception("error in hajm");
}
}
else
{
throw new Exception("error in format !");
}
}
?>
</div>






حالا چطور باید با دادن ایدی عکس اون رو نمایش بدم با کد php مشکل دارم
در ضمن میخوام هم توی مرورگر از سرور عکس رو بخونم هم توی برنامه اندرویدی
خواهش می کنم کمکم کنید
مرسی

tux-world
یک شنبه 09 آبان 1395, 08:44 صبح
فکر کنم اگه سرچ کنید میلیاردها بار گفته شده عکس رو تو بانک اطلاعاتی ذخیره نکنید. خسته شدیم از بس ما هم تکرار کردیم. این سوال و مشکل راه حلش اینه عکس رو به عنوان فایل یه جا ذخیره کنید لینکش رو تو دیتابیس داشته باشید. همین:گریه:

mani2000
یک شنبه 09 آبان 1395, 09:40 صبح
بله اول من عکس رو bolb ذخیره کردم اما لان که شما گفتین ادرس رو ذخیره کردم اینجوری




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

if (!empty($_FILES["uploadedimage"]["name"])) {





$file_name=$_FILES["uploadedimage"]["name"];


$temp_name=$_FILES["uploadedimage"]["tmp_name"];


$imgtype=$_FILES["uploadedimage"]["type"];


$ext= GetImageExtension($imgtype);


$imagename=date("d-m-Y")."-".time().$ext;


$target_path = "images/".$imagename;








if(move_uploaded_file($temp_name, $target_path)) {





$query_upload="INSERT into insertimage (image_path,image_name) VALUES('".$target_path."','".date("Y-m-d")."')";


mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());





}else{





exit("Error While uploading image on the server");


}





}

// Read the row we want to pull out of the database.






حالا مشکلم نمایش عکس با دادن id در اندروید و مرورگر هست
خواهش می کنم کمک کنید

mani2000
دوشنبه 10 آبان 1395, 11:04 صبح
کسی نیست؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟ ؟؟؟؟

Nevercom
دوشنبه 10 آبان 1395, 12:48 عصر
لطفاً کدهاتون رو داخا تگ CODE بزارید تا قابل خوندن باشه




if (!empty($_FILES["uploadedimage"]["name"])) {

$file_name = $_FILES["uploadedimage"]["name"];
$temp_name = $_FILES["uploadedimage"]["tmp_name"];
$imgtype = $_FILES["uploadedimage"]["type"];

$ext = GetImageExtension($imgtype);

$imagename = date("d-m-Y") . "-" . time() . $ext;

$target_path = "images/" . $imagename;

if (move_uploaded_file($temp_name, $target_path)) {

$query_upload =
"INSERT into insertimage (image_path,image_name) VALUES('" . $target_path . "','" . date("Y-m-d") . "')";

mysql_query($query_upload) or die("error in $query_upload == ----> " . mysql_error());

$id = mysql_insert_id();
echo json_encode(array(
"id" => $id,
"url" => "http://your-site.com/{$target_path}"
), JSON_UNESCAPED_UNICODE);

} else {
echo json_encode(array(
"errorCode" => -1,
"errorMessage" => "Error While uploading image on the server"
), JSON_UNESCAPED_UNICODE);
exit("Error While uploading image on the server");
}
}

// Read the row we want to pull out of the database.

مثلاً این مدلی میتونید اطلاعات رو به برنامه پاس بدید.

درواقع یک آرایه ایجاد می کنید با همه ی اطلاعاتی که بهش نیازه، با json_encode اون رو تبدیل به رشته ی JSON می کنید و بعد فقط echo کنید.

کد خودتون رو دست نزدم، ولی از افزونه‌ی mysql استفاده نکنید و بجاش از mysqli یا pdo استفاده کنید.

mani2000
سه شنبه 11 آبان 1395, 10:10 صبح
سلام ممنونم از وقتی که گذاشتید
این کد رو نوشتم نهایی


if (!empty($_FILES["uploadedimage"]["name"])) {

$file_name = $_FILES["uploadedimage"]["name"];
$temp_name = $_FILES["uploadedimage"]["tmp_name"];
$imgtype = $_FILES["uploadedimage"]["type"];

$ext = GetImageExtension($imgtype);

$imagename = date("d-m-Y") . "-" . time() . $ext;

$target_path = "images/" . $imagename;

if (move_uploaded_file($temp_name, $target_path)) {

$query_upload =
"INSERT into insertimage (image_path,image_name) VALUES('" . $target_path . "','" . date("Y-m-d") . "')";

mysql_query($query_upload) or die("error in $query_upload == ----> " . mysql_error());

$id = mysql_insert_id();
echo json_encode(array(
"image_id" => $id,
"url" => "http://localhost/uupp/save.php/{$target_path}"
), JSON_UNESCAPED_UNICODE);

} else {
echo json_encode(array(
"errorCode" => -1,
"errorMessage" => "Error While uploading image on the server"
), JSON_UNESCAPED_UNICODE);
exit("Error While uploading image on the server");
}
}

// Read the row we want to pull out of the database.


$result = mysql_query("select image_path from insertimage where image_id = $id");





// If successful, fetch the row as an array and store the data from the "image" column into a variable.


if ($result) {


if ($row = mysql_fetch_array($result)) {


$img = $row["image_path"];


}
}



// Set the content type of this page to image/jpeg since the image we are pulling out is a jpg image.


header("Content-type: image/jpeg");





// Echo out the image.


echo "$img";


اما
هنگام نمایش عکس چیزی رو نشون نمیده و صفحه مرورگر سیاه میشه و این پیغام میاد
the image"http://localhost/uupp/save.php" cannot be display because it contains error.
و در اندروید برنامه بسته شد
ممنون میشم لطف کنید و کمک کنید
مرسی

mani2000
چهارشنبه 12 آبان 1395, 08:52 صبح
کسی نیست؟؟

Nevercom
چهارشنبه 12 آبان 1395, 10:16 صبح
این تغییری که دادید اون چیزی نیست که من راهنمایی کردم .

نیاز نیست Content-Type رو به image/jpeg تغییر بدید.

شما فقط لازم هست آدرس کامل اون فایل رو echo کنید، حالا به هر روشی، فقط مطمئن باشید چیزی که از این اسکریپت خارج میشه یک متن هست، مثلاً http://your-site.i/images/image.jpg

mani2000
چهارشنبه 12 آبان 1395, 18:51 عصر
سلام اینو الان داد بهم توی مرورگر
{"image_id":12,"url":"http:\/\/localhost\/save.php\/images\/03-13-2016-1478155433.jpg"}images/03-13-2016-1478155433.jpg
اما من الان اومدم این کد رو نوشتم
if (isset($_GET["password"])) {
$password = $_GET['password'];

// get a product from products table
$result = mysql_query("SELECT * FROM product WHERE password = $password");

if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {

$result = mysql_fetch_array($result);

$product = array();
$product["password"] = $result["password"];
$product["name"] = $result["name"];
$product["famil"] = $result["famil"];
$product["image_path"] = $result["image_path"];
// success
$response["success"] = 1;

// user node
$response["product"] = array();

array_push($response["product"], $product);

// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";

// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";

// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}

و توی اندروید اینو
private final String imageURL = "http://10.0.2.2/php/save.php
imageView = (ImageView) findViewById(R.id.imageViewShow);


private void getImage() {
String id = editTextId.getText().toString().trim();
class GetImage extends AsyncTask<String,Void,Bitmap>{


ImageView bmImage;
ProgressDialog loading;

public GetImage(ImageView bmImage) {
this.bmImage = bmImage;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
loading.dismiss();
bmImage.setImageBitmap(bitmap);
}

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(ViewImage.this,"Downloading Image","Please wait...",true,true);
}

@Override
protected Bitmap doInBackground(String... strings) {
//image filde dovome dar table ast
String url = imageURL+ strings[3];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(url).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return mIcon;
}
}

GetImage gi = new GetImage(imageView);
gi.execute(id);
}

و بعد اینو صدا زدم
getImage();

int success = 0;
try {
success = json.getInt(TAG_SUCCESS);
} catch (JSONException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
if (success == 1) {
// successfully received product details
JSONArray productObj = null;
try {
productObj = json.getJSONArray(TAG_PRODUCT);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} // JSON
// Array

// get first product object from JSON Array
JSONObject product = null;
try {
product = productObj.getJSONObject(0);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
TextView txtName = (TextView) findViewById(R.id.textView1);
TextView txtfamil = (TextView) findViewById(R.id.textView2);

// display product data in EditText
try {
txtName.setText("نام"+product.getString(TAG_NAME));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
txtfamil.setText("فامیل"+product.getString(TAG_Famil));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

getImage();

برنامه بسته شد
خواهش میکنم راهنمایی کنید اگه ممکنه....
مرسی...

mani2000
جمعه 14 آبان 1395, 07:41 صبح
کسی نیست؟؟؟؟