![]() |
Guardar Imagenes en MySQL
hola!
estube viendo este hilo: http://www.clubdelphi.com/foros/show...&threadid=2450 el cual trata de como insertar imagenes en paradox y/o interbase. Asi que se me ocurrió hacer la siguiente pregunta, a los amigos de este lado del foro: ¿MySQL acepta guardar imagenes? |
Hola
Si lo Hace con los campos BLOB Suerte ;) |
y como sería en php?
|
Si no me equivoco seria con esto(esta algo largo ):
la clase es esta: <? class clsImageManager{ var $db_abstraction; var $added_image_id; function clsImageManager($dblayer) { $this->db_abstraction = $dblayer; if (!$this->db_abstraction->table_exists("images")) { $this->db_abstraction->create_table("images","id INT NOT NULL AUTO_INCREMENT, image_data LONGBLOB NOT NULL, image_width INT NOT NULL, image_height INT NOT NULL, image_type CHAR NOT NULL, image_size INT(7) NOT NULL , PRIMARY KEY (id)"); } } function addImage($img_holder) { $theInfo=getImageSize($img_holder); if (($theInfo[2]>=1) and ($theInfo[2]<=3)) { $fields=array("image_data","image_width","image_height","image_type","image_size"); $values=array(base64_encode(fread(fopen($img_holder, "r"), filesize($img_holder))),$theInfo[0],$theInfo[1],$theInfo[2],filesize($img_holder)); $this->db_abstraction->insert("images",$fields,$values); $this->added_image_id=$this->db_abstraction->insert_id(); } else { $this->added_image_id=false; } } function addDirectory($thePath) { $directory=dir($thePath); while ($entry=$directory->read()) { $this->addImage($entry); } } function removeImage($id) { $this->db_abstraction->delete("images","id=$id"); } function getImageSize($id) { $this->db_abstraction->select("*","images","id=$id"); list($id,$image_data,$image_width,$image_height,$image_type,$image_size)=$this->db_abstraction->fetch_array(); return array($image_width,$image_height,$image_type,"width=\"$image_width\" height=\"$image_height\""); } function getScaledImageSize($id,$maxW,$maxH) { $this->db_abstraction->select("*","images","id=$id"); list($id,$image_data,$image_width,$image_height,$image_type,$image_size)=$this->db_abstraction->fetch_array(); if ($image_width>$maxW) { $ratio=$maxW/$image_width; $image_width=$image_width*$ratio; $image_height=$image_height*$ratio; } if ($image_height>$maxH) { $ratio=$maxH/$image_height; $image_width=$image_width*$ratio; $image_height=$image_height*$ratio; } return array($image_width,$image_height,$image_type,"width=\"$image_width\" height=\"$image_height\""); } function showImage($id) { $this->db_abstraction->select("*","images","id=$id"); list($id,$image_data,$image_width,$image_height,$image_type,$image_size)=$this->db_abstraction->fetch_array(); switch ($image_type) { case 1: header("Content-type: image/gif"); print base64_decode($image_data); break; case 2: header("Content-type: image/jpeg"); print base64_decode($image_data); break; case 3: header("Content-type: image/png"); print base64_decode($image_data); break; } } } ?> y utiliza esta: <? class db_layout { var $classname="db_layout"; var $db_name; var $db_user; var $db_password; var $db_host; var $db_link_ptr; var $tables; var $fields; } class db_mysql extends db_layout { var $classname="db_mysql"; var $db_result; var $db_affected_rows; var $saved_results=array(); var $results_saved=0; function error($where="",$error,$errno) { echo "$where<br>"; die($error."<br>".$errno); } function error_msg() { return mysql_error(); } function PushResults() { $this->saved_results[$this->results_saved]=array($this->db_result,$this->db_affected_rows); $this->results_saved++; } function PopResults() { $this->results_saved--; $this->db_result=$this->saved_results[$this->results_saved][0]; $this->db_affected_rows=$this->saved_results[$this->results_saved][1]; } function db_mysql($host, $user, $passwd, $db, $create="") { $this->db_name=$db; $this->db_user=$user; $this->db_passwd=$passwd; $this->db_host=$host; $this->db_link_ptr=@mysql_connect($host,$user,$passwd) or $this->error("",mysql_error(),mysql_errno()); $this->dbhandler=@mysql_select_db($db); if (!$this->dbhandler) { if ($create=="1") { @mysql_create_db($db,$this->db_link_ptr) or $this->error("imposible crear la base de datos.",mysql_error(),mysql_errno());; $this->dbhandler=@mysql_select_db($db); } } } function reselect_db($db){ $this->dbhandler=@mysql_select_db($db); } function closeDB() { @mysql_close($this->db_link_ptr); } function create_table($tblName,$tblStruct) { if (is_array($tblStruct)) $theStruct=implode(",",$tblStruct); else $theStruct=$tblStruct; @mysql_query("create table $tblName ($theStruct)") or $this->error("create table $tblName ($theStruct)",mysql_error(),mysql_errno()); } function drop_table($tblName) { @mysql_query("drop table if exists $tblName") or $this->error("drop table $tblName",mysql_error(),mysql_errno()); } function raw_query($sql_stat) { $this->db_result=@mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno()); $this->db_affected_rows=@mysql_num_rows($this->db_result); } function count_records($table,$filter="") { $this->db_result=@mysql_query("select count(*) as num from $table".(($filter!="")?" where $filter" : "")); $xx=@mysql_result($this->db_result,0,"num"); return $xx; } function select($fields,$tables,$where="",$order_by="",$group_by="",$having="",$limit="") { $sql_stat=" select $fields from $tables "; if (!empty($where)) $sql_stat.="where $where "; if (!empty($group_by)) $sql_stat.="group by $group_by "; if (!empty($order_by)) $sql_stat.="order by $order_by "; if (!empty($having)) $sql_stat.="having $having "; if (!empty($limit)) $sql_stat.="limit $limit "; $this->db_result=@mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno()); $this->db_affected_rows=@mysql_num_rows($this->db_result); return $sql_stat; } function list_tables() { $this->db_result=@mysql_list_tables($this->db_name); $this->db_affected_rows=@mysql_num_rows($this->db_result); return $this->db_result; } function describe($tablename) { $this->result=@mysql_query("describe $tablename"); } function table_exists($tablename) { $this->pushresults(); $description=$this->describe($tablename); $this->popresults(); if ($description) $exists=true; else $exists=false; return $exists; } function tablename($tables, $tbl) { return mysql_tablename($tables,$tbl); } function insert_id() { return mysql_insert_id(); } function insert($table,$fields="",$values="") { $sql_stat="insert into $table "; if (is_array($fields)) $theFields=implode(",",$fields); else $theFields=$fields; if (is_array($values)) $theValues="'".implode("','",$values)."'"; else $theValues=$values; $theValues=str_replace("'now()'","now()",$theValues); if (!empty($theFields)) $sql_stat.="($theFields) "; $sql_stat.="values ($theValues)"; @mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno()); } function update($table,$newvals,$where="") { if (is_array($newvals)) $theValues=implode(",",$newvals); else $theValues=$newvals; $sql_stat="update $table set $theValues"; if (!empty($where)) $sql_stat.=" where $where"; @mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno()); } function delete($table,$where="") { $sql_stat="delete from $table "; if (!empty($where)) $sql_stat.="where $where "; $db_result2=@mysql_query($sql_stat) or $this->error($sql_stat,mysql_error(),mysql_errno()); $this->db_affected_rows=@mysql_affected_rows($this->db_result2); } function free() { @mysql_free_result($this->db_result) or $this->error("",mysql_error(),mysql_errno()); } function fetch_row() { $row=mysql_fetch_row($this->db_result); return $row; } function result($recno,$field) { return mysql_result($this->db_result,$recno,$field); } function num_fields(){ return mysql_num_fields($this->db_result); } function fetch_array() { $row=mysql_fetch_array($this->db_result); return $row; } function fetch_field() { $row=mysql_fetch_field($this->db_result); return $row; } } ?> Suerte ;):( :cool: |
esta es la clase que te pase
http://www.phpclasses.org/browse.html/package/979.html y ademas necesita de esta: http://www.phpclasses.org/goto/brows...ckage/976.html ---------------------------------------- otra forma: http://www.onlamp.com/pub/a/php/2000...php_mysql.html :D |
ok....muchas gracias...
le hecharé un ojo. |
La franja horaria es GMT +2. Ahora son las 15:20:48. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi