PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/atk4-addons/filestore/lib/Model/Image.php

https://bitbucket.org/gRgsoft/1
PHP | 142 lines | 98 code | 19 blank | 25 comment | 9 complexity | fd516c631dab3c3bcbcd41e66c7a1b14 MD5 | raw file
  1. <?php
  2. namespace filestore;
  3. class Model_Image extends Model_File {
  4. //protected $entity_code='filestore_image';
  5. public $default_thumb_width=140;
  6. public $default_thumb_height=140;
  7. // Temporarily, to be replaced in 4.1 to use Model_File
  8. // TODO: replace with file_model_name and no auto-prefixing with filestore/
  9. public $entity_file='File';
  10. function init(){
  11. parent::init();
  12. $this->i=$this->join('filestore_image.original_file_id');
  13. /*
  14. $this->hasOne('filestore/'.$this->entity_file,'original_file_id')
  15. ->caption('Original File');
  16. */
  17. $this->i->hasOne('filestore/'.$this->entity_file,'thumb_file_id')
  18. ->caption('Thumbnail');
  19. $this->addExpression('thumb_url')->set(array($this,'getThumbURLExpr'));
  20. }
  21. /* Produces expression which calculates full URL of image */
  22. function getThumbURLExpr($m,$q){
  23. $m=$this->add('filestore/Model_'.$this->entity_file);
  24. $m->addCondition('id',$this->i->fieldExpr('thumb_file_id'));
  25. return $m->fieldQuery('url');
  26. }
  27. function toStringSQL($source_field, $dest_fieldname){
  28. return $source_field.' '.$dest_fieldname;
  29. }
  30. function performImport(){
  31. parent::performImport();
  32. $this->createThumbnails();
  33. // Now that the origninal is imported, lets generate thumbnails
  34. /*
  35. $this->performImport();
  36. $this->update();
  37. $this->afterImport();
  38. */
  39. return $this;
  40. }
  41. function createThumbnails(){
  42. if($this->id)$this->load($this->id);// temporary
  43. $this->createThumbnail('thumb_file_id',$this->default_thumb_height,$this->default_thumb_width);
  44. }
  45. function imagickCrop($i,$width,$height){
  46. $geo = $i->getImageGeometry();
  47. // crop the image
  48. if(($geo['width']/$width) < ($geo['height']/$height))
  49. {
  50. $i->cropImage($geo['width'], floor($height*$geo['width']/$width), 0, (($geo['height']-($height*$geo['width']/$width))/2));
  51. }
  52. else
  53. {
  54. $i->cropImage(ceil($width*$geo['height']/$height), $geo['height'], (($geo['width']-($width*$geo['height']/$height))/2), 0);
  55. }
  56. // thumbnail the image
  57. $i->ThumbnailImage($width,$height,true);
  58. }
  59. function createThumbnail($field,$x,$y){
  60. // Create entry for thumbnail.
  61. $thumb=$this->ref($field,'link');
  62. if(!$thumb->loaded()){
  63. $thumb->set('filestore_volume_id',$this->get('filestore_volume_id'));
  64. $thumb->set('original_filename','thumb_'.$this->get('original_filename'));
  65. $thumb->set('filestore_type_id',$this->get('filestore_type_id'));
  66. $thumb['filename']=$thumb->generateFilename();
  67. }
  68. if(class_exists('\Imagick',false)){
  69. $image=new \Imagick($this->getPath());
  70. //$image->resizeImage($x,$y,\Imagick::FILTER_LANCZOS,1,true);
  71. //$image->cropThumbnailImage($x,$y);
  72. $this->imagickCrop($image,$x,$y);
  73. $this->hook("beforeThumbSave", array($thumb));
  74. $image->writeImage($thumb->getPath());
  75. $thumb["filesize"] = filesize($thumb->getPath());
  76. }elseif(function_exists('imagecreatefromjpeg')){
  77. list($width, $height, $type) = getimagesize($this->getPath());
  78. ini_set("memory_limit","1000M");
  79. $a=array(null,'gif','jpeg','png');
  80. $type=@$a[$type];
  81. if(!$type)throw $this->exception('This file type is not supported');
  82. //saving the image into memory (for manipulation with GD Library)
  83. $fx="imagecreatefrom".$type;
  84. $myImage = $fx($this->getPath());
  85. $thumbSize = $x; // only supports rectangles
  86. if($x!=$y && 0)throw $this->exception('Model_Image currently does not support non-rectangle thumbnails with GD extension')
  87. ->addMoreInfo('x',$x)
  88. ->addMoreInfo('y',$y);
  89. // calculating the part of the image to use for thumbnail
  90. if ($width > $height) {
  91. $y = 0;
  92. $x = ($width - $height) / 2;
  93. $smallestSide = $height;
  94. } else {
  95. $x = 0;
  96. $y = ($height - $width) / 2;
  97. $smallestSide = $width;
  98. }
  99. // copying the part into thumbnail
  100. $myThumb = imagecreatetruecolor($thumbSize, $thumbSize);
  101. imagecopyresampled($myThumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
  102. //final output
  103. imagejpeg($myThumb, $thumb->getPath());
  104. imageDestroy($myThumb);
  105. imageDestroy($myImage);
  106. }else{
  107. // No Imagemagick support. Ignore resize
  108. $thumb->import($this->getPath(),'copy');
  109. }
  110. $thumb->save(); // update size and chmod
  111. }
  112. function afterImport(){
  113. // Called after original is imported. You can do your resizes here
  114. $f=$this->getPath();
  115. $gd_info=getimagesize($f);
  116. }
  117. function setMaxResize(){
  118. }
  119. function beforeDelete(){
  120. parent::beforeDelete();
  121. $this->ref('thumb_file_id')->tryDelete();
  122. }
  123. }