{"id":484,"date":"2019-01-31T16:53:35","date_gmt":"2019-01-31T16:53:35","guid":{"rendered":"https:\/\/www.dafap.fr\/blog\/?p=484"},"modified":"2019-01-31T17:18:42","modified_gmt":"2019-01-31T17:18:42","slug":"photos-en-base-de-donnees","status":"publish","type":"post","link":"https:\/\/www.dafap.fr\/blog\/photo\/photos-en-base-de-donnees","title":{"rendered":"Photos en base de donn\u00e9es"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Type de champs \u00e0 utiliser<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">On devra disposer au minimum du type de photo : JPEG, GIF, PNG, BMP ... et du champ contenant les donn\u00e9es binaires de la photo.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dans une base MySql, pour les donn\u00e9es binaire, on dispose des types suivants :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>tinyblob : 255 octets<\/li><li>blob : 64 Ko<\/li><li>mediumblob : 16 Mo<\/li><li>longblob : 4 Go<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Cr\u00e9ation de la table<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE `photos` (\n    `id` int NOT NULL,\n    `phototype` varchar(25) NOT NULL DEFAULT 'JPEG',\n    `photodata` blob NOT NULL\n);\nALTER TABLE `photos`\n  ADD PRIMARY KEY (`id`);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Afin de lier la photo \u00e0 une fiche d'un individu, d'un objet ou d'un paysage ... contenue dans une table <code>datas<\/code>, on aura int\u00e9r\u00eat \u00e0 d\u00e9clarer une <code>foreign key<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ALTER TABLE `datas`\n  ADD CONSTRAINT `photos_ibfk_1` FOREIGN KEY (`id`) REFERENCES `datas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Formulaire d'envoi d'une photo<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Le formulaire doit \u00eatre celui d'un envoi de fichier :<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;form name=\"formphoto\" enctype=\"multipart\/form-data\" method=\"post\">\n    &lt;input name=\"id\" type=\"hidden\" value=\"\">\n    &lt;label>Choisissez le fichier image (JPEG, GIF ou PNG)\n    &lt;input name=\"filephoto\" type=\"file\">&lt;\/label>\n    &lt;input name=\"submit\" type=\"submit\" value=\"Envoyer la photo\">\n&lt;\/form><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">R\u00e9ception cot\u00e9 serveur<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ne pas oublier que les variables se r\u00e9cup\u00e8reront dans la variable globale <code>$__POST<\/code> sauf le fichier photo qui sera dans la variable globale <code>$__FILES<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ret =is_uploaded_file($__FILES['filephoto']['tmp_name'];\nif (!$ret) {\n    throw new Exception('Probl\u00e8me de transfert.');\n}\n$filesize = $__FILES['filephoto']['size'];\nif ($filesize > SIZE_MAX) {\n    throw new Exception('Fichier photo trop gros');\n}\n$phototype = $__FILES['filephoto']['type'];\n$photodata = file_get_contents($__FILES['filephoto']['tmp_name']);\n$id = $__POST['id'];\n\/\/ si on travaille en https, il faut ajouter le context dans file_get_contents()\n$photodata = addslashes($photodata); \/\/ indispensable pour enregistrer dans MySql\n\/\/ proc\u00e9dure classique d'enregistrement des donn\u00e9es $id, $phototype, $photodata<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Affichage de la photo dans une page<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Comme il n'y a pas de fichier, on doit envoyer les donn\u00e9es binaires de la photo, encod\u00e9es base64. On pourra utiliser la fonction suivante :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function base64_encode_image($imgdata, $imgtype) {\n    return 'data:image\/' . $imgtype .';base64,' . stripslashes($imgdata);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">et on affectera ce r\u00e9sultat \u00e0 la propri\u00e9t\u00e9 <code>src<\/code> de la balise <code>&lt;img&gt;<\/code> comme ceci :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;img src=\"&lt;?= base64_encode_image($photodata, $phototype);?>\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Type de champs \u00e0 utiliser On devra disposer au minimum du type de photo : JPEG, GIF, PNG, BMP &#8230; et du champ contenant les donn\u00e9es binaires de la photo. Dans une base MySql, pour les donn\u00e9es binaire, on dispose des types suivants : tinyblob : 255 octets blob : 64 Ko mediumblob : 16 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,41,3],"tags":[],"class_list":["post-484","post","type-post","status-publish","format-standard","hentry","category-developpement-php","category-howto","category-photo"],"_links":{"self":[{"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/posts\/484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/comments?post=484"}],"version-history":[{"count":19,"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/posts\/484\/revisions"}],"predecessor-version":[{"id":506,"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/posts\/484\/revisions\/506"}],"wp:attachment":[{"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/media?parent=484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/categories?post=484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dafap.fr\/blog\/wp-json\/wp\/v2\/tags?post=484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}