hello, i developed a script that uploads an image to mysql and it 'tryes' in the same time to create a thumbnail inside a given path. The first part work the second works only when the 'browsed' image is in the same directory with .php files...
my script works like this :
- write the image into BLOB field inside mysql (no matter the path where the image is selected from ),
- AND creates the thumbnail inside a given directory($path) BUT only if the image submitted was inside the same directory where php files are 🙁
all i want is to be able create that thumbnail 🙁 from whereever i load the images ..
i will put here the whole content of my files so you can try it yourself...it can be useful because part of it really works and i feel i am close to make it all work (with your help) :
########################## sql table
CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
picture MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);
######################## init.php
<?php
// MySQL Settings
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'pass';
$db_database = 'db';
// Connect to the database
$link = mysql_connect ($db_host, $db_user, $db_pass) or die ('Could not connect to the database.');
mysql_selectdb ($db_database) or die ('Could not select database.');
?>
############################## addform.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Upload FORM</title>
</head>
<body>
<br><br>
<form method="post" enctype="multipart/form-data" action="add.php">
<input name="image" type="file">
<input name="upload" type="submit" value=" Upload ">
</form>
<br><br>
<?php if (isset($ext_error)) { ?>Error: <?php echo $ext_error; ?><?php } ?>
<?php if (isset($size_error)) { ?>Error: <?php echo $size_error; ?><?php } ?>
<?php if (isset($ok))
{
echo $ok;
?>
<br>
<img src="<?php echo getRandomImage($path_to_images, $default_img) ?>" alt="">
<br><font size="2">(random) </font>
<?php
}
?>
</body>
</html>
############################## add.php
<?php
include 'init.php';
if(!isset($_POST['upload']))
{
include 'addform.php';
exit;
}
else
{
// The original name of the file on the client machine.
$file_name = $_FILES['image']['name'];
$info = pathinfo($file_name);
$ext = $info['extension'];
$extension = strtolower($ext);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "gif") && ($extension != "png"))
{
$ext_error = 'Unknown extension!';
include 'addform.php';
exit;
}
// max size 200 kB
define ("max_size","200");
// The size, in bytes, of the uploaded file.
$file_size = $_FILES['image']['size'];
if ($file_size > max_size*1000)
{
$size_error = 'You have exceeded the size limit!';
include 'addform.php';
exit;
}
// The temporary filename of the file in which the uploaded file was stored on the server.
$temp_name = $_FILES['image']['tmp_name'];
// The mime type of the file, if the browser provided this information. An example would be "image/gif".
// This mime type is however not checked on the PHP side and therefore don't take its value for granted.
$file_type = $_FILES['image']['type'];
$var = fopen($temp_name, 'r');
$img = addslashes(fread($var, filesize($temp_name)));
fclose($var);
$user = 'testeee';
$path="thumbs\\$user";
if (!is_dir($path)) { mkdir($path,0777,TRUE); }
include 'fct.php';
$thumbname = "$path\\thumb_$file_name";
createthumb($_FILES['image']['name'],$thumbname,120,150);
if (!get_magic_quotes_gpc())
{
$file_name = addslashes($_FILES['image']['name']);
}
$query = "INSERT INTO upload (name, size, type, picture) ".
"VALUES ('$file_name', '$file_size', '$file_type', '$img')";
mysql_query($query) or die ('Could not insert into dB!');
$ok = 'Thumbnail created Successfully!';
$path_to_images = "thumbs/$user/";
$default_img = "";
include 'addform.php';
exit;
}
?>
############################## fct.php
<?php
function createthumb($name,$filename,$new_w,$new_h){
$system=explode('.',$name);
if (preg_match('/jpg|jpeg/',$system[1])){
$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png/',$system[1])){
$src_img=imagecreatefrompng($name);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2) {
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}
else {
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1])) {
imagepng($dst_img,$filename);
}
else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
function getRandomImage($path, $img) {
if ( $list = getImagesList($path) ) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($list);
$img = $list[$num];
}
return $path . $img;
}
function getImagesList($path) {
$ctr = 0;
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// can add checks for other image file types here
if ( preg_match("/(\.gif|\.jpg)$/", $img_file) ) {
$images[$ctr] = $img_file;
$ctr++;
}
}
closedir($img_dir);
return $images;
}
return false;
}
?>
#####################################################
please check yourselfs to see the errors...
Warning: imagecreatefromjpeg(pic4.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\wamp\www\fct.php on line 6
Warning: imagesx(): supplied argument is not a valid Image resource in C:\wamp\www\fct.php on line 12
Warning: imagesy(): supplied argument is not a valid Image resource in C:\wamp\www\fct.php on line 13
Warning: Division by zero in C:\wamp\www\fct.php on line 26
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\fct.php on line 29
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\fct.php on line 30
Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\fct.php on line 36
Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\fct.php on line 39
Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\fct.php on line 40
#############
when selecting the image from other directory i get those errors !!!
help!
10x 🙁