我已经创建了一个创建缩略图的php函数,它可以像现在一样工作但现在我想在mymysqli数据baze中插入这个缩略图和一些数据。我试着这样做,但它不起作用。我对mysql不好,需要帮助。
require 'config.php';
if(preg_match('/[.](jpg)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path_to_thumbs_directory . $filename);
$host = 'localhost';
$user = 'timoleon_pandess';
$pass = 'pass';
mysql_connect($host, $user, $pass);
mysql_select_db('timoleon_pandessia');
$insert_path="INSERT INTO `ng17p_jshopping_products`(`product_id`, `parent_id`, `product_ean`, `product_availability`, `product_template`, `product_price`, `min_price`, `different_prices`, `product_weight`, `image`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9],[value-10])";
$var=mysql_query($inser_path);
?>
答案 0 :(得分:0)
将数据库路径放入图像,例如:'\ img \ image2.png'
将表格更改为商店图片的类型“longb”(将触发LONGBLOB)。 读这个 http://forums.mysql.com/read.php?20,17671,27914
为什么要在DB中插入图像? 这样做
Insert into table (`path`) Values ('image.png');
\image.png
- 服务器上的图像路径。从DB读取时,您可以添加完整路径\img\avatar\image.png
我不明白,你使用插件查询就像是用php编写的吗?该值需要包含在''例如:不是,[value-4]
而是,'[value-4]'
中。我认为价值将是一个变量$value4
。
INSERT INTO `ng17p_jshopping_products` (`product_id`, `parent_id`, `product_ean`, `product_availability`, `product_template`, `product_price`, `min_price`, `different_prices`, `product_weight`, `image`) VALUES ('[value-1]','[value-2]','[value-3]','[value-4]','[value-5]','[value-6]','[value-7]','[value-8]','[value-9]','[value-10]')
答案 1 :(得分:0)
你需要添加变量女巫将包含图像的文件名。
const char* empty() { return ""; }
char* message;
if(m_message)
message = m_message;
else
message = empty();
插入时
$filename=$path_to_thumbs_directory.$filename;
如果你完成所有操作并且在尝试更改代码以使用mysql i 时它会起作用,请在此处阅读$query="INSERT INTO `ng17p_jshopping_products` (`product_id`,`parent_id`,`product_ean`, `product_availability`,`product_template`, `product_price`, `min_price`, `different_prices`, `product_weight`, `image`) VALUES ('[value-1]','[value-2]','[value-3]','[value-4]','[value-5]','[value-6]','[value-7]','[value-8]','[value-9]','$filename')";
$res=mysql_query($query);
if($res==1) { $id=mysql_insert_id;
$query="Select image from ng17p_jshopping_products where id='$id'";
$res=mysql_query($query);
$row=mysql_fetch_array($res);
echo 'It is OK <img src="'.$row['image'].'">';
} else echo 'Error';
答案 2 :(得分:0)
你必须使用ob_start(); ob_get_clean();获取图像内容并将其放入数据库中。
首先在您的数据库中创建一个字段来存储图像内容:
ALTER TABLE `ng17p_jshopping_products` ADD `image_content` LONGBLOB NOT NULL ;
并使用您的代码:
<?php
require 'config.php';
if(preg_match('/[.](jpg)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
$host = 'localhost';
$user = 'timoleon_pandess';
$pass = 'pass';
mysql_connect($host, $user, $pass);
mysql_select_db('timoleon_pandessia');
ob_start();
imagejpeg($nm);
$image_content = mysql_real_escape_string(base64_encode(ob_get_clean()));
imagedestroy($nm);
$insert_path="INSERT INTO `ng17p_jshopping_products`(`product_id`, `parent_id`, `product_ean`, `product_availability`, `product_template`, `product_price`, `min_price`, `different_prices`, `product_weight`, `image`,`image_content`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9],[value-10],'".$image_content."')";
$var=mysql_query($inser_path);
mysql_close();
?>
要显示图像,您必须使用以下功能:
<?php
function show_image($id) {
$host = 'localhost';
$user = 'timoleon_pandess';
$pass = 'pass';
mysql_connect($host, $user, $pass);
mysql_select_db('timoleon_pandessia');
// code to retrieve the image from database with $id id to $image_to_show variable
// ....
header('Content-Type: image/jpeg');
echo stripslashes(base64_decode($image_to_show["image_content"]));
}
?>
答案 3 :(得分:0)
这足以在项目与图像之间建立连接。设置为单元格,你将图像的路径存储到varchar(255)而不是blob或其他情况下的其他情况(存储文件不在硬盘上但在DB中)
$bd_host="localhost";
$bd_user="timoleon_pandess";
$bd_password="pass";
$bd_base="_base";
$con=mysql_connect($bd_host, $bd_user, $bd_password);
if(!con) {
echo 'Ошибка, невозможно подключиться к серверу базы данных.';
exit;
}
mysql_select_db($bd_base, $con);
mysql_query('SET NAMES utf8');
$filename='/img/img.png'; // you need to catch it when you make and save thumbnail
$query="insert into ng17p_jshopping_products (`image`) Values ('$filename')";
mysql_query($query);
如果您的脚本没有制作图像或将其保存在需要的地方,这是另一个问题!