Ahoj,
začínám s OOP a mám takový problém. Udělal jsem si script na uploadování obraázků a vytváření jejich náhledů před GD knihovnu. V tom problém není. Součástí scriptu je i zapisování údajů o umístění obrázkku do DB, což mi tam vázne a nevím na čem. Obrázek se v pohodě i s vytvořeným náhledem uploadne, ale do databáze se nic nezapíše (ani to nehází žádné errory). Pokud byste někdo věděli, prosím o radu :) Je toho kus, ale důležité jsou SQL dotazy (hned na začátku) a pak jejich volání (řádky 58-59, 84-85, 90-91).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test GD knihovny</title>
</head>
<body>
<?php
class SQL {
function __construct($host, $username, $password, $db) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->connection = mysql_connect($this->host, $this->username, $this->password);
if(!$this->connection) {
$this->error .= '<p>Spojení s databází selhalo.</p>';
}
if(!mysql_select_db($this->db, $this->connection)) {
$this->error .= '<p>Zvolená databáze nebyla nalezena.</p>';
}
}
function insertSQL($table, $target, $values) {
$command = "INSERT INTO " . $table . "(" . $target . ") VALUES (" . $values . ")";
mysql_query($command, $connection);
}
function updateSQL($table, $value, $where) {
$command = "UPDATE " . $table . " SET " . $value . " WHERE " . $where;
mysql_query($command, $connection);
}
}
class ImageUpload {
public $error;
function __construct($host, $username, $password, $db, $table, $error = null) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->table = $table;
$this->error = $error;
}
function uploadImage($image_name, $db_target, $db_values, $db_update, $image_path, $sample, $db_update_sample, $sample_width, $sample_height) {
$file_name = $_FILES[$image_name]['name'];
$temp_name = $_FILES[$image_name]['tmp_name'];
$insert_id = new SQL($this->host, $this->username, $this->password, $this->db);
$insert_id->insertSQL($this->table, $db_target, $db_values);
$image_id = mysql_insert_id();
$source_name = mysql_insert_id();
if(ereg("^.+.jpg$", $file_name)){
$name = $source_name . ".jpg";
$sample_name = $source_name . "_sample.jpg";
$image_type = "jpg";
}
elseif(ereg("^.+.jpeg$", $file_name)){
$name = $source_name . ".jpg";
$sample_name = $source_name . "_sample.jpg";
$image_type = "jpg";
}
elseif(ereg("^.+.png$", $file_name)){
$name = $source_name . ".png";
$sample_name = $source_name . "_sample.png";
$image_type = "png";
}
$save_sample_target = $image_path . $sample_name;
$save_image_target = $image_path . $name;
move_uploaded_file($temp_name, $save_image_target);
$insert_path = new SQL($this->host, $this->username, $this->password, $this->db);
$insert_path->updateSQL($this->table, $db_update . '=' . $save_image_target, 'id=' . $image_id);
if($sample == 'ano') {
$this->createSample($save_image_target, $image_type, $sample_width, $sample_height);
$insert_sample_path = new SQL($this->host, $this->username, $this->password, $this->db);
$insert_sample_path->updateSQL($this->table, $db_update . '=' . $save_sample_target, 'id=' . $image_id);
}
}
function checkImage($image_name) {
$temp_name = $_FILES[$image_name]['tmp_name'];
$name = $_FILES[$image_name]['name'];
$type = $_FILES[$image_name]['type'];
$size = $_FILES[$image_name]['size'];
if($size < 2097152) {
if($type == "image/pjpg" || $type == "image/pjpeg" || $type == "image/ppng" || $type == "image/jpg" || $type == "image/jpeg" || $type == "image/png"){
if(ereg("^.+.jpg$", $name) || ereg("^.+.jpeg$", $name) || ereg("^.+.png$", $name)){
if(getimagesize($temp_name) != 0) {
return true;
} else {
$this->error .= "<p>Chybný formát souboru.</p>";
return false;
}
} else {
$this->error .= "<p>Chybný formát souboru.</p>";
return false;
}
} else {
$this->error .= "<p>Chybný formát souboru.</p>";
return false;
}
} else {
$this->error .= "<p>Velikost souboru překračuje povolené maximum.</p>";
return false;
}
}
function errorReport() {
if(isset($this->error)) {
return $this->error;
} else {
return false;
}
}
function createSample($image, $image_type, $max_width, $max_height) {
header ("Content-type:image/png");
$params = getimagesize($image);
if($image_type == 'jpg') {
$source_img = imagecreatefromjpeg($image);
}
elseif($image_type == 'png') {
$source_img = imagecreatefrompng($image);
}
if($params[0] > $params[1]) {
$img_width = $max_width;
$img_height = $params[1] / ($params[0] / $img_width);
$new_img = imagecreatetruecolor($img_width, $img_height);
} else {
$img_height = $max_height;
$img_width = $params[0] / ($params[1] / $img_height);
$new_img = imagecreatetruecolor($img_width, $img_height);
}
$pure_img_name = explode('.', $image);
$sample_name = $pure_img_name[0] . '_sample.png';
ImageCopyResampled($new_img, $source_img, 0, 0, 0, 0, $img_width, $img_height, $params[0], $params[1]);
Imagepng($new_img, $sample_name);
ImageDestroy($new_img);
ImageDestroy($source_img);
}
}
if(isset($_POST['submit'])) {
$test = new ImageUpload('localhost', 'root', 'smymawbx', 'obrazky', 'galerie');
if($test->checkImage('photo') == true) {
$test->uploadImage('photo', "'id','datum'", "'','NOW();'", 'cesta', 'obr/', 'ano', 'nahled', 160, 120);
header('location: index.php');
} else {
if($test->errorReport() != false) {
echo $test->errorReport();
}
}
}
?>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="photo" id="photo" />
<input type="submit" name="submit" value="Odeslat" />
</form>
</body>
</html>