Image.class.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. class Image {
  3. public static function manifest(){
  4. return array(
  5. 'uid' => 'image',
  6. 'label' => 'Image',
  7. 'description' => 'Fichier image de type jpg/jpeg/png/bmp/gif/svg',
  8. 'fromExtension' => array('jpg','jpeg','png','bmp','gif','svg'),
  9. 'upload' => array(
  10. 'url' => 'action.php?action=upload_resource',
  11. 'element' => '#resource p img:eq(0)',
  12. 'callback' => '$(\'#resource img:eq(0)\').attr(\'src\',r.url);'
  13. )
  14. );
  15. }
  16. //Import depuis un glisser déposé du fichier
  17. public static function fromFile($resource){
  18. $resource->save();
  19. $ext = getExt($resource->label);
  20. $name = $resource->id.'.'.$ext;
  21. file_put_contents(SKETCH_PATH.$name,$resource->content);
  22. $resource->content = $name;
  23. return $resource;
  24. }
  25. //Import depuis un flux json compressé de la ressource
  26. public static function fromJson($resource){
  27. $resource->save();
  28. $stream = base64_decode($resource->content);
  29. $resource->content = $resource->id.'.png';
  30. file_put_contents(SKETCH_PATH.$resource->content,$stream);
  31. return $resource;
  32. }
  33. //export en fichier JSON compressé de la ressource
  34. public static function toJson($resource){
  35. $resource = $resource->toArray();
  36. $resource['content'] = base64_encode(file_get_contents(SKETCH_PATH.$resource['content']));
  37. return $resource;
  38. }
  39. public static function toFile($resource){
  40. $ext = getExt($resource->content);
  41. $infos = self::manifest();
  42. return array(
  43. 'name'=> slugify($resource->label).'.'.$ext,
  44. 'content' => file_exists(SKETCH_PATH.$resource->content) ? file_get_contents(SKETCH_PATH.$resource->content) : ''
  45. );
  46. }
  47. public static function toHtml($resource,$sketch){
  48. global $myUser;
  49. $infos = self::manifest();
  50. $image = $resource->content ==''?'img/default_image.png':'action.php?action=get_resource_image&id='.$resource->id;
  51. $response['content'] = '<img style="width:100%;height:auto;" class="dropzone" src="'.$image.'" />';
  52. if($myUser->id == $sketch->owner)
  53. $response['upload'] = $infos['upload'];
  54. return $response;
  55. }
  56. }
  57. ?>