PageRenderTime 158ms CodeModel.GetById 49ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/doc2img/trunk/inc/doc2img_convertir.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 306 lines | 184 code | 40 blank | 82 comment | 38 complexity | b1f976f80bd8782aec19bfce2c79879e MD5 | raw file
  1. <?php
  2. if (!defined('_ECRIRE_INC_VERSION')) return;
  3. /**
  4. * Fonction autonome convertissant un document donné en paramètre
  5. *
  6. * Ensemble des actions necessaires à la conversion d'un document en image :
  7. * - recupère les informations sur le documents (nom, repertoire, nature)
  8. * - determine les informations sur le documents final (nom, repertoire, extension)
  9. *
  10. * Documentation intéressante :
  11. * - http://valokuva.org/?p=7
  12. * - http://valokuva.org/?p=7#comment-19198
  13. *
  14. * @param int $id_document identifiant du document à convertir
  15. * @param string $type méthode à utiliser :
  16. * - full converti tout le document
  17. * - vignette converti la première page en vignette du document
  18. */
  19. function inc_doc2img_convertir($id_document,$opt='full') {
  20. spip_log('conversion du doc '.$id_document,'docimg');
  21. @set_time_limit(0);
  22. if(!in_array($opt,array('full','vignette'))){
  23. if(isset($opt['options']) && in_array($opt['options'],array('full','vignette'))){
  24. $type = $opt['options'];
  25. }else
  26. $type = 'full';
  27. }else
  28. $type = $opt;
  29. $ret = array();
  30. if(class_exists('Imagick')){
  31. include_spip('inc/documents');
  32. include_spip('inc/config');
  33. /**
  34. * Si cette action est lancée en CRON, on ne peut supprimer les documents ensuite
  35. * TODO trouver mieux
  36. */
  37. if(!isset($GLOBALS['visiteur_session'])
  38. OR !is_array($GLOBALS['visiteur_session']))
  39. $GLOBALS['visiteur_session'] = sql_fetsel('*','spip_auteurs','webmestre="oui"');
  40. $config = lire_config('doc2img',array());
  41. $format_cible = $config['format_cible'] ? $config['format_cible'] : 'png';
  42. $document = doc2img_document($id_document);
  43. /**
  44. * Chargement du document en mémoire
  45. * On détermine le nombre de pages du document
  46. * On libère la ressource automatiquement si on utilise la class
  47. * car on réouvre chaque page par la suite
  48. */
  49. $frame = 0;
  50. $resolution = $config['resolution'] ? $config['resolution'] : 150;
  51. $ajouter_documents = charger_fonction('ajouter_documents', 'action');
  52. if($type == 'full'){
  53. try{
  54. $image = new Imagick($document['fichier']);
  55. $identify = $image->identifyImage();
  56. spip_log($identify,"docimg");
  57. $identify2 = $image->getImageProperties();
  58. spip_log($identify2,"docimg");
  59. $nb_pages = $image->getNumberImages();
  60. spip_log($nb_pages.' pages','docimg');
  61. $image->clear();
  62. $image->destroy();
  63. include_spip('action/editer_document');
  64. /**
  65. * Est ce que ce document a déja été converti
  66. * Si oui, on supprime son ancienne conversion
  67. */
  68. $documents_doc2img = sql_select('L1.id_document',
  69. 'spip_documents AS L1 LEFT JOIN spip_documents_liens AS L2 ON L1.id_document=L2.id_document',
  70. 'L1.mode="doc2img" AND L2.objet="document" AND L2.id_objet='.intval($id_document));
  71. $documents_a_supprimer = array();
  72. while($document_doc2img = sql_fetch($documents_doc2img)){
  73. $documents_a_supprimer[] = $document_doc2img['id_document'];
  74. }
  75. if(count($documents_a_supprimer) > 0){
  76. $supprimer_document = charger_fonction('supprimer_document','action');
  77. foreach ($documents_a_supprimer as $id_document_supprimer) {
  78. $supprimer_document($id_document_supprimer);
  79. }
  80. }
  81. unset($documents_a_supprimer,$documents_doc2img,$identify);
  82. // chaque page est un fichier qu'on sauve dans la table doc2img indexé
  83. // par son numéro de page
  84. do {
  85. $image_frame = new Imagick();
  86. //on accede à la page $frame
  87. spip_log("page $frame",'docimg');
  88. $image_frame->readImage($document['fichier'].'['.$frame.']');
  89. $image_frame->setImageFormat($format_cible);
  90. if(is_numeric($resolution) && ($resolution <= '600') && ($resolution > $identify['resolution']['x'])){
  91. $image_frame->setResolution($resolution,$resolution);
  92. }
  93. if(is_numeric($config['compression']) && ($config['compression'] > 50) && ($config['compression'] <= 100)){
  94. $image_frame->setImageCompressionQuality($config['compression']);
  95. }
  96. //calcule des dimensions
  97. //$dimensions = doc2img_ratio($image_frame,$config);
  98. //nom du fichier cible, c'est à dire la frame (image) indexée
  99. $frame_name = $document['name'].'-'.$frame.'.'.$format_cible;
  100. $dest = $document['cible_url'].$frame_name;
  101. //on sauvegarde la page
  102. //$image_frame->resizeImage($dimensions['largeur'], $dimensions['hauteur'],Imagick::FILTER_LANCZOS,1);
  103. $image_frame->writeImage($dest);
  104. /**
  105. * On ajoute le document dans la table spip_documents avec comme type "doc2img"
  106. * Il sera automatiquement lié au document original
  107. */
  108. $files = array(array('tmp_name'=>$dest,'name'=>$frame_name));
  109. $x = $ajouter_documents('new', $files,'document', $id_document, 'doc2img');
  110. unset($files);
  111. if(($frame == 0) && ($config['logo_auto']=='on') && in_array($format_cible,array('png','jpg'))){
  112. $id_vignette = $document['id_vignette'];
  113. $frame_tmp = $document['cible_url'].$document['name'].'-logo.'.$format_cible;
  114. $image_frame->writeImage($frame_tmp);
  115. $files = array(array('tmp_name'=>$frame_tmp,'name'=>$frame_name));
  116. if(is_numeric($id_vignette) && ($id_vignette > 0)){
  117. $vignette = $ajouter_documents($id_vignette, $files,'', 0, 'vignette');
  118. }else{
  119. $vignette = $ajouter_documents('new', $files,'', 0, 'vignette');
  120. }
  121. if (is_numeric(reset($vignette))
  122. AND $id_vignette = reset($vignette))
  123. document_modifier($id_document,array("id_vignette" => intval($id_vignette)));
  124. spip_unlink($document['cible_url'].$frame_tmp);
  125. unset($vignette,$files);
  126. }
  127. spip_unlink($document['cible_url'].$frame_name);
  128. unset($frame_name,$dest);
  129. $frame++;
  130. document_modifier(reset($x),array('page'=>$frame));
  131. unset($x);
  132. $image_frame->clear();
  133. $image_frame->destroy();
  134. } while($frame < $nb_pages);
  135. }
  136. catch ( ImagickException $e ){
  137. spip_log('On a une erreur','docimg');
  138. spip_log($e,'docimg');
  139. }
  140. include_spip('inc/invalideur');
  141. suivre_invalideur('id_document="$id_document"');
  142. }else{
  143. try{
  144. do {
  145. if(in_array($format_cible,array('png','jpg'))){
  146. $image_frame = new Imagick();
  147. if(is_numeric($resolution) && ($resolution <= '600') && ($resolution > $identify['resolution']['x'])){
  148. $image_frame->setResolution($resolution,$resolution);
  149. }
  150. $image_frame->readImage($document['fichier'].'['.$frame.']');
  151. $image_frame->setImageFormat($format_cible);
  152. if(is_numeric($config['compression']) && ($config['compression'] > 50) && ($config['compression'] <= 100)){
  153. $image_frame->setImageCompressionQuality($config['compression']);
  154. }
  155. //nom du fichier cible, c'est à dire la frame (image) indexée
  156. $frame_name = $document['name'].'-logo.'.$format_cible;
  157. //on sauvegarde la page
  158. $image_frame->writeImage($document['cible_url'].$frame_name);
  159. $image_frame->clear();
  160. $image_frame->destroy();
  161. $files = array(array('tmp_name'=>$document['cible_url'].$frame_name,'name'=>$frame_name));
  162. $id_vignette = $document['id_vignette'];
  163. if(is_numeric($id_vignette) && ($id_vignette > 0)){
  164. $x = $ajouter_documents($id_vignette, $files,'', 0, 'vignette');
  165. }else{
  166. $x = $ajouter_documents('new', $files,'', 0, 'vignette');
  167. }
  168. if (is_numeric(reset($x))
  169. AND $id_vignette = reset($x)){
  170. spip_log('On modifie','docimg');
  171. include_spip('action/editer_document');
  172. document_modifier($id_document,array("id_vignette" => intval($id_vignette)));
  173. }
  174. spip_unlink($document['cible_url'].$frame_name);
  175. }else{
  176. spip_log("DOC2IMG : le format de sortie sélectionné dans la configuration ne permet pas de créer une vignette",'docimg');
  177. }
  178. $frame++;
  179. } while($frame < 1 );
  180. }catch ( ImagickException $e ){
  181. spip_log('On a une erreur','docimg');
  182. spip_log($e,'docimg');
  183. }
  184. include_spip('inc/invalideur');
  185. suivre_invalideur('id_document="$id_document"');
  186. }
  187. $ret['success'] = true;
  188. return $ret;
  189. }else{
  190. spip_log('Erreur Doc2Img : La class doc2img n est pas disponible');
  191. return false;
  192. }
  193. }
  194. /**
  195. * Calcul les ratios de taille de l'image finale
  196. *
  197. * Vérifie que le document donné en paramètre est bien listé dans les types de documents
  198. * autorisés à la conversion via CFG
  199. *
  200. * @param $id_document identifiant du document à controler
  201. * @return booleen $resultat : true document convertible, false sinon
  202. */
  203. function doc2img_ratio($handle,$config=array()) {
  204. $ratio['largeur'] = $ratio['hauteur'] = 1;
  205. /**
  206. * Récupération des dimensions du document d'origine
  207. */
  208. $dimensions['largeur'] = $handle->getImageWidth();
  209. $dimensions['hauteur'] = $handle->getImageHeight();
  210. //si une largeur seuil a été définie
  211. if ($largeur = $config['largeur']) {
  212. $ratio['largeur'] = $largeur / $dimensions['largeur'];
  213. }
  214. //si une hauteur seuil a été définie
  215. if ($hauteur = $config['hauteur']) {
  216. $ratio['hauteur'] = $hauteur / $dimensions['hauteur'];
  217. }
  218. /**
  219. * Ajustement des ratios si proportion demandée
  220. * Si agrandissement demandé on prend le plus grand ratio,
  221. * sinon le plus petit
  222. */
  223. if ($config['proportion'] == "on") {
  224. $ratio['largeur'] = ($config['agrandir'] == 'on') ? max($ratio['hauteur'], $ratio['largeur']) : min($ratio['hauteur'], $ratio['largeur']);
  225. $ratio['hauteur'] = $ratio['largeur'];
  226. }
  227. /**
  228. * Définition des dimensions définitives
  229. */
  230. $dimensions['largeur'] = $ratio['largeur'] * $dimensions['largeur'];
  231. $dimensions['hauteur'] = $ratio['hauteur'] * $dimensions['hauteur'];
  232. return $dimensions;
  233. }
  234. /**
  235. * Fonction pour connaitre les infos fichiers du document
  236. *
  237. * Calcul un tableau :
  238. * - avec informations sur le documents (nom, repertoire, nature)
  239. * - determine les informations des documents finaux (nom, respertoire, extension)
  240. *
  241. * @param $id_document identifiant du document à convertir
  242. * @return $document : liste de données caractérisant le document
  243. */
  244. function doc2img_document($id_document) {
  245. //on recupere l'url du document
  246. $fichier = sql_fetsel(
  247. 'fichier,extension,id_vignette',
  248. 'spip_documents',
  249. 'id_document='.$id_document
  250. );
  251. //chemin relatif du fichier
  252. $fichier_reel = get_spip_doc($fichier['fichier']);
  253. //url relative du repertoire contenant le fichier , on retire aussi le / en fin
  254. $document['fichier'] = $fichier_reel;
  255. //information sur le nom du fichier
  256. $document['extension'] = $fichier['extension'];
  257. $document['name'] = basename($fichier_reel);
  258. $document['id_vignette'] = $fichier['id_vignette'];
  259. //creation du repertoire cible
  260. //url relative du repertoire cible
  261. if(!is_dir(_DIR_VAR."cache-doc2img"))
  262. sous_repertoire(_DIR_VAR,"cache-doc2img");
  263. $document['cible_url'] = _DIR_VAR."cache-doc2img".'/';
  264. return $document;
  265. }
  266. ?>