@@ -299,7 +299,7 @@ |
||
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | /** |
| 302 | - * @param resource Returns the image resource in any. |
|
| 302 | + * @param resource resource the image resource in any. |
|
| 303 | 303 | * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" |
| 304 | 304 | */ |
| 305 | 305 | public function setResource($resource) { |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | public function widthTopLeft() { |
| 135 | 135 | $o = $this->getOrientation(); |
| 136 | - $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 136 | + $this->logger->debug('OC_Image->widthTopLeft() Orientation: '.$o, array('app' => 'core')); |
|
| 137 | 137 | switch ($o) { |
| 138 | 138 | case -1: |
| 139 | 139 | case 1: |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | public function heightTopLeft() { |
| 159 | 159 | $o = $this->getOrientation(); |
| 160 | - $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 160 | + $this->logger->debug('OC_Image->heightTopLeft() Orientation: '.$o, array('app' => 'core')); |
|
| 161 | 161 | switch ($o) { |
| 162 | 162 | case -1: |
| 163 | 163 | case 1: |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | if ($mimeType === null) { |
| 185 | 185 | $mimeType = $this->mimeType(); |
| 186 | 186 | } |
| 187 | - header('Content-Type: ' . $mimeType); |
|
| 187 | + header('Content-Type: '.$mimeType); |
|
| 188 | 188 | return $this->_output(null, $mimeType); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | } |
| 203 | 203 | if ($filePath === null) { |
| 204 | 204 | if ($this->filePath === null) { |
| 205 | - $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 205 | + $this->logger->error(__METHOD__.'(): called with no path.', array('app' => 'core')); |
|
| 206 | 206 | return false; |
| 207 | 207 | } else { |
| 208 | 208 | $filePath = $this->filePath; |
@@ -226,10 +226,10 @@ discard block |
||
| 226 | 226 | } |
| 227 | 227 | $isWritable = is_writable(dirname($filePath)); |
| 228 | 228 | if (!$isWritable) { |
| 229 | - $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 229 | + $this->logger->error(__METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.', array('app' => 'core')); |
|
| 230 | 230 | return false; |
| 231 | 231 | } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
| 232 | - $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 232 | + $this->logger->error(__METHOD__.'(): File \''.$filePath.'\' is not writable.', array('app' => 'core')); |
|
| 233 | 233 | return false; |
| 234 | 234 | } |
| 235 | 235 | } |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | $imageType = IMAGETYPE_BMP; |
| 258 | 258 | break; |
| 259 | 259 | default: |
| 260 | - throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 260 | + throw new Exception('\OC_Image::_output(): "'.$mimeType.'" is not supported when forcing a specific output format'); |
|
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | |
@@ -436,7 +436,7 @@ discard block |
||
| 436 | 436 | return; |
| 437 | 437 | } |
| 438 | 438 | |
| 439 | - $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 439 | + $exif = @exif_read_data('data://image/jpeg;base64,'.base64_encode($data)); |
|
| 440 | 440 | if (!$exif) { |
| 441 | 441 | return; |
| 442 | 442 | } |
@@ -454,7 +454,7 @@ discard block |
||
| 454 | 454 | */ |
| 455 | 455 | public function fixOrientation() { |
| 456 | 456 | $o = $this->getOrientation(); |
| 457 | - $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 457 | + $this->logger->debug('OC_Image->fixOrientation() Orientation: '.$o, array('app' => 'core')); |
|
| 458 | 458 | $rotate = 0; |
| 459 | 459 | $flip = false; |
| 460 | 460 | switch ($o) { |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | $rotate = 90; |
| 490 | 490 | break; |
| 491 | 491 | } |
| 492 | - if($flip && function_exists('imageflip')) { |
|
| 492 | + if ($flip && function_exists('imageflip')) { |
|
| 493 | 493 | imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
| 494 | 494 | } |
| 495 | 495 | if ($rotate) { |
@@ -551,7 +551,7 @@ discard block |
||
| 551 | 551 | imagealphablending($this->resource, true); |
| 552 | 552 | imagesavealpha($this->resource, true); |
| 553 | 553 | } else { |
| 554 | - $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 554 | + $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: '.$imagePath, array('app' => 'core')); |
|
| 555 | 555 | } |
| 556 | 556 | break; |
| 557 | 557 | case IMAGETYPE_JPEG: |
@@ -559,10 +559,10 @@ discard block |
||
| 559 | 559 | if (getimagesize($imagePath) !== false) { |
| 560 | 560 | $this->resource = @imagecreatefromjpeg($imagePath); |
| 561 | 561 | } else { |
| 562 | - $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 562 | + $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: '.$imagePath, array('app' => 'core')); |
|
| 563 | 563 | } |
| 564 | 564 | } else { |
| 565 | - $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 565 | + $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: '.$imagePath, array('app' => 'core')); |
|
| 566 | 566 | } |
| 567 | 567 | break; |
| 568 | 568 | case IMAGETYPE_PNG: |
@@ -572,21 +572,21 @@ discard block |
||
| 572 | 572 | imagealphablending($this->resource, true); |
| 573 | 573 | imagesavealpha($this->resource, true); |
| 574 | 574 | } else { |
| 575 | - $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 575 | + $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: '.$imagePath, array('app' => 'core')); |
|
| 576 | 576 | } |
| 577 | 577 | break; |
| 578 | 578 | case IMAGETYPE_XBM: |
| 579 | 579 | if (imagetypes() & IMG_XPM) { |
| 580 | 580 | $this->resource = @imagecreatefromxbm($imagePath); |
| 581 | 581 | } else { |
| 582 | - $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 582 | + $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagePath, array('app' => 'core')); |
|
| 583 | 583 | } |
| 584 | 584 | break; |
| 585 | 585 | case IMAGETYPE_WBMP: |
| 586 | 586 | if (imagetypes() & IMG_WBMP) { |
| 587 | 587 | $this->resource = @imagecreatefromwbmp($imagePath); |
| 588 | 588 | } else { |
| 589 | - $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 589 | + $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: '.$imagePath, array('app' => 'core')); |
|
| 590 | 590 | } |
| 591 | 591 | break; |
| 592 | 592 | case IMAGETYPE_BMP: |
@@ -696,7 +696,7 @@ discard block |
||
| 696 | 696 | */ |
| 697 | 697 | private function imagecreatefrombmp($fileName) { |
| 698 | 698 | if (!($fh = fopen($fileName, 'rb'))) { |
| 699 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 699 | + $this->logger->warning('imagecreatefrombmp: Can not open '.$fileName, array('app' => 'core')); |
|
| 700 | 700 | return false; |
| 701 | 701 | } |
| 702 | 702 | // read file header |
@@ -704,7 +704,7 @@ discard block |
||
| 704 | 704 | // check for bitmap |
| 705 | 705 | if ($meta['type'] != 19778) { |
| 706 | 706 | fclose($fh); |
| 707 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 707 | + $this->logger->warning('imagecreatefrombmp: Can not open '.$fileName.' is not a bitmap!', array('app' => 'core')); |
|
| 708 | 708 | return false; |
| 709 | 709 | } |
| 710 | 710 | // read image header |
@@ -728,7 +728,7 @@ discard block |
||
| 728 | 728 | $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
| 729 | 729 | if ($meta['imagesize'] < 1) { |
| 730 | 730 | fclose($fh); |
| 731 | - $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 731 | + $this->logger->warning('imagecreatefrombmp: Can not obtain file size of '.$fileName.' is not a bitmap!', array('app' => 'core')); |
|
| 732 | 732 | return false; |
| 733 | 733 | } |
| 734 | 734 | } |
@@ -738,7 +738,7 @@ discard block |
||
| 738 | 738 | // read color palette |
| 739 | 739 | $palette = array(); |
| 740 | 740 | if ($meta['bits'] < 16) { |
| 741 | - $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 741 | + $palette = unpack('l'.$meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | 742 | // in rare cases the color value is signed |
| 743 | 743 | if ($palette[1] < 0) { |
| 744 | 744 | foreach ($palette as $i => $color) { |
@@ -751,7 +751,7 @@ discard block |
||
| 751 | 751 | if ($im == false) { |
| 752 | 752 | fclose($fh); |
| 753 | 753 | $this->logger->warning( |
| 754 | - 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 754 | + 'imagecreatefrombmp: imagecreatetruecolor failed for file "'.$fileName.'" with dimensions '.$meta['width'].'x'.$meta['height'], |
|
| 755 | 755 | array('app' => 'core')); |
| 756 | 756 | return false; |
| 757 | 757 | } |
@@ -760,7 +760,7 @@ discard block |
||
| 760 | 760 | $p = 0; |
| 761 | 761 | $vide = chr(0); |
| 762 | 762 | $y = $meta['height'] - 1; |
| 763 | - $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 763 | + $error = 'imagecreatefrombmp: '.$fileName.' has not enough data!'; |
|
| 764 | 764 | // loop through the image data beginning with the lower left corner |
| 765 | 765 | while ($y >= 0) { |
| 766 | 766 | $x = 0; |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | $this->logger->warning($error, array('app' => 'core')); |
| 773 | 773 | return $im; |
| 774 | 774 | } |
| 775 | - $color = @unpack('V', $part . $vide); |
|
| 775 | + $color = @unpack('V', $part.$vide); |
|
| 776 | 776 | break; |
| 777 | 777 | case 16: |
| 778 | 778 | if (!($part = substr($data, $p, 2))) { |
@@ -784,16 +784,16 @@ discard block |
||
| 784 | 784 | $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
| 785 | 785 | break; |
| 786 | 786 | case 8: |
| 787 | - $color = @unpack('n', $vide . substr($data, $p, 1)); |
|
| 787 | + $color = @unpack('n', $vide.substr($data, $p, 1)); |
|
| 788 | 788 | $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
| 789 | 789 | break; |
| 790 | 790 | case 4: |
| 791 | - $color = @unpack('n', $vide . substr($data, floor($p), 1)); |
|
| 791 | + $color = @unpack('n', $vide.substr($data, floor($p), 1)); |
|
| 792 | 792 | $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
| 793 | 793 | $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
| 794 | 794 | break; |
| 795 | 795 | case 1: |
| 796 | - $color = @unpack('n', $vide . substr($data, floor($p), 1)); |
|
| 796 | + $color = @unpack('n', $vide.substr($data, floor($p), 1)); |
|
| 797 | 797 | switch (($p * 8) % 8) { |
| 798 | 798 | case 0: |
| 799 | 799 | $color[1] = $color[1] >> 7; |
@@ -824,7 +824,7 @@ discard block |
||
| 824 | 824 | break; |
| 825 | 825 | default: |
| 826 | 826 | fclose($fh); |
| 827 | - $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 827 | + $this->logger->warning('imagecreatefrombmp: '.$fileName.' has '.$meta['bits'].' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | 828 | return false; |
| 829 | 829 | } |
| 830 | 830 | imagesetpixel($im, $x, $y, $color[1]); |
@@ -846,7 +846,7 @@ discard block |
||
| 846 | 846 | */ |
| 847 | 847 | public function resize($maxSize) { |
| 848 | 848 | if (!$this->valid()) { |
| 849 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 849 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 850 | 850 | return false; |
| 851 | 851 | } |
| 852 | 852 | $widthOrig = imagesx($this->resource); |
@@ -861,7 +861,7 @@ discard block |
||
| 861 | 861 | $newHeight = $maxSize; |
| 862 | 862 | } |
| 863 | 863 | |
| 864 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 864 | + $this->preciseResize((int) round($newWidth), (int) round($newHeight)); |
|
| 865 | 865 | return true; |
| 866 | 866 | } |
| 867 | 867 | |
@@ -872,7 +872,7 @@ discard block |
||
| 872 | 872 | */ |
| 873 | 873 | public function preciseResize(int $width, int $height): bool { |
| 874 | 874 | if (!$this->valid()) { |
| 875 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 875 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 876 | 876 | return false; |
| 877 | 877 | } |
| 878 | 878 | $widthOrig = imagesx($this->resource); |
@@ -880,7 +880,7 @@ discard block |
||
| 880 | 880 | $process = imagecreatetruecolor($width, $height); |
| 881 | 881 | |
| 882 | 882 | if ($process == false) { |
| 883 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 883 | + $this->logger->error(__METHOD__.'(): Error creating true color image', array('app' => 'core')); |
|
| 884 | 884 | imagedestroy($process); |
| 885 | 885 | return false; |
| 886 | 886 | } |
@@ -894,7 +894,7 @@ discard block |
||
| 894 | 894 | |
| 895 | 895 | imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
| 896 | 896 | if ($process == false) { |
| 897 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 897 | + $this->logger->error(__METHOD__.'(): Error re-sampling process image', array('app' => 'core')); |
|
| 898 | 898 | imagedestroy($process); |
| 899 | 899 | return false; |
| 900 | 900 | } |
@@ -952,7 +952,7 @@ discard block |
||
| 952 | 952 | |
| 953 | 953 | imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
| 954 | 954 | if ($process == false) { |
| 955 | - $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 955 | + $this->logger->error('OC_Image->centerCrop, Error re-sampling process image '.$width.'x'.$height, array('app' => 'core')); |
|
| 956 | 956 | imagedestroy($process); |
| 957 | 957 | return false; |
| 958 | 958 | } |
@@ -972,12 +972,12 @@ discard block |
||
| 972 | 972 | */ |
| 973 | 973 | public function crop(int $x, int $y, int $w, int $h): bool { |
| 974 | 974 | if (!$this->valid()) { |
| 975 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 975 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 976 | 976 | return false; |
| 977 | 977 | } |
| 978 | 978 | $process = imagecreatetruecolor($w, $h); |
| 979 | 979 | if ($process == false) { |
| 980 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 980 | + $this->logger->error(__METHOD__.'(): Error creating true color image', array('app' => 'core')); |
|
| 981 | 981 | imagedestroy($process); |
| 982 | 982 | return false; |
| 983 | 983 | } |
@@ -991,7 +991,7 @@ discard block |
||
| 991 | 991 | |
| 992 | 992 | imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
| 993 | 993 | if ($process == false) { |
| 994 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 994 | + $this->logger->error(__METHOD__.'(): Error re-sampling process image '.$w.'x'.$h, array('app' => 'core')); |
|
| 995 | 995 | imagedestroy($process); |
| 996 | 996 | return false; |
| 997 | 997 | } |
@@ -1011,7 +1011,7 @@ discard block |
||
| 1011 | 1011 | */ |
| 1012 | 1012 | public function fitIn($maxWidth, $maxHeight) { |
| 1013 | 1013 | if (!$this->valid()) { |
| 1014 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1014 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 1015 | 1015 | return false; |
| 1016 | 1016 | } |
| 1017 | 1017 | $widthOrig = imagesx($this->resource); |
@@ -1021,7 +1021,7 @@ discard block |
||
| 1021 | 1021 | $newWidth = min($maxWidth, $ratio * $maxHeight); |
| 1022 | 1022 | $newHeight = min($maxHeight, $maxWidth / $ratio); |
| 1023 | 1023 | |
| 1024 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1024 | + $this->preciseResize((int) round($newWidth), (int) round($newHeight)); |
|
| 1025 | 1025 | return true; |
| 1026 | 1026 | } |
| 1027 | 1027 | |
@@ -1034,7 +1034,7 @@ discard block |
||
| 1034 | 1034 | */ |
| 1035 | 1035 | public function scaleDownToFit($maxWidth, $maxHeight) { |
| 1036 | 1036 | if (!$this->valid()) { |
| 1037 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1037 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 1038 | 1038 | return false; |
| 1039 | 1039 | } |
| 1040 | 1040 | $widthOrig = imagesx($this->resource); |
@@ -1092,7 +1092,7 @@ discard block |
||
| 1092 | 1092 | if ($bit <= 8) { |
| 1093 | 1093 | for ($i = 0; $i < $colorsNum; $i++) { |
| 1094 | 1094 | $colors = imagecolorsforindex($im, $i); |
| 1095 | - $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1095 | + $rgbQuad .= chr($colors['blue']).chr($colors['green']).chr($colors['red'])."\0"; |
|
| 1096 | 1096 | } |
| 1097 | 1097 | $bmpData = ''; |
| 1098 | 1098 | if ($compression == 0 || $bit < 8) { |
@@ -1125,7 +1125,7 @@ discard block |
||
| 1125 | 1125 | $index = imagecolorat($im, $i, $j); |
| 1126 | 1126 | if ($index !== $lastIndex || $sameNum > 255) { |
| 1127 | 1127 | if ($sameNum != 0) { |
| 1128 | - $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1128 | + $bmpData .= chr($sameNum).chr($lastIndex); |
|
| 1129 | 1129 | } |
| 1130 | 1130 | $lastIndex = $index; |
| 1131 | 1131 | $sameNum = 1; |
@@ -1166,15 +1166,15 @@ discard block |
||
| 1166 | 1166 | $sizeData = strlen($bmpData); |
| 1167 | 1167 | $colorsNum = 0; |
| 1168 | 1168 | } |
| 1169 | - $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1169 | + $fileHeader = 'BM'.pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1170 | 1170 | $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
| 1171 | 1171 | if ($fileName != '') { |
| 1172 | 1172 | $fp = fopen($fileName, 'wb'); |
| 1173 | - fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1173 | + fwrite($fp, $fileHeader.$infoHeader.$rgbQuad.$bmpData); |
|
| 1174 | 1174 | fclose($fp); |
| 1175 | 1175 | return true; |
| 1176 | 1176 | } |
| 1177 | - echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1177 | + echo $fileHeader.$infoHeader.$rgbQuad.$bmpData; |
|
| 1178 | 1178 | return true; |
| 1179 | 1179 | } |
| 1180 | 1180 | } |
@@ -43,556 +43,556 @@ discard block |
||
| 43 | 43 | * Class for basic image manipulation |
| 44 | 44 | */ |
| 45 | 45 | class OC_Image implements \OCP\IImage { |
| 46 | - /** @var false|resource */ |
|
| 47 | - protected $resource = false; // tmp resource. |
|
| 48 | - /** @var int */ |
|
| 49 | - protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident. |
|
| 50 | - /** @var string */ |
|
| 51 | - protected $mimeType = 'image/png'; // Default to png |
|
| 52 | - /** @var int */ |
|
| 53 | - protected $bitDepth = 24; |
|
| 54 | - /** @var null|string */ |
|
| 55 | - protected $filePath = null; |
|
| 56 | - /** @var finfo */ |
|
| 57 | - private $fileInfo; |
|
| 58 | - /** @var \OCP\ILogger */ |
|
| 59 | - private $logger; |
|
| 60 | - /** @var \OCP\IConfig */ |
|
| 61 | - private $config; |
|
| 62 | - /** @var array */ |
|
| 63 | - private $exif; |
|
| 46 | + /** @var false|resource */ |
|
| 47 | + protected $resource = false; // tmp resource. |
|
| 48 | + /** @var int */ |
|
| 49 | + protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident. |
|
| 50 | + /** @var string */ |
|
| 51 | + protected $mimeType = 'image/png'; // Default to png |
|
| 52 | + /** @var int */ |
|
| 53 | + protected $bitDepth = 24; |
|
| 54 | + /** @var null|string */ |
|
| 55 | + protected $filePath = null; |
|
| 56 | + /** @var finfo */ |
|
| 57 | + private $fileInfo; |
|
| 58 | + /** @var \OCP\ILogger */ |
|
| 59 | + private $logger; |
|
| 60 | + /** @var \OCP\IConfig */ |
|
| 61 | + private $config; |
|
| 62 | + /** @var array */ |
|
| 63 | + private $exif; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Constructor. |
|
| 67 | - * |
|
| 68 | - * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by |
|
| 69 | - * an imagecreate* function. |
|
| 70 | - * @param \OCP\ILogger $logger |
|
| 71 | - * @param \OCP\IConfig $config |
|
| 72 | - * @throws \InvalidArgumentException in case the $imageRef parameter is not null |
|
| 73 | - */ |
|
| 74 | - public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) { |
|
| 75 | - $this->logger = $logger; |
|
| 76 | - if ($logger === null) { |
|
| 77 | - $this->logger = \OC::$server->getLogger(); |
|
| 78 | - } |
|
| 79 | - $this->config = $config; |
|
| 80 | - if ($config === null) { |
|
| 81 | - $this->config = \OC::$server->getConfig(); |
|
| 82 | - } |
|
| 65 | + /** |
|
| 66 | + * Constructor. |
|
| 67 | + * |
|
| 68 | + * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by |
|
| 69 | + * an imagecreate* function. |
|
| 70 | + * @param \OCP\ILogger $logger |
|
| 71 | + * @param \OCP\IConfig $config |
|
| 72 | + * @throws \InvalidArgumentException in case the $imageRef parameter is not null |
|
| 73 | + */ |
|
| 74 | + public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) { |
|
| 75 | + $this->logger = $logger; |
|
| 76 | + if ($logger === null) { |
|
| 77 | + $this->logger = \OC::$server->getLogger(); |
|
| 78 | + } |
|
| 79 | + $this->config = $config; |
|
| 80 | + if ($config === null) { |
|
| 81 | + $this->config = \OC::$server->getConfig(); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - if (\OC_Util::fileInfoLoaded()) { |
|
| 85 | - $this->fileInfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 86 | - } |
|
| 84 | + if (\OC_Util::fileInfoLoaded()) { |
|
| 85 | + $this->fileInfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - if ($imageRef !== null) { |
|
| 89 | - throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.'); |
|
| 90 | - } |
|
| 91 | - } |
|
| 88 | + if ($imageRef !== null) { |
|
| 89 | + throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.'); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Determine whether the object contains an image resource. |
|
| 95 | - * |
|
| 96 | - * @return bool |
|
| 97 | - */ |
|
| 98 | - public function valid() { // apparently you can't name a method 'empty'... |
|
| 99 | - return is_resource($this->resource); |
|
| 100 | - } |
|
| 93 | + /** |
|
| 94 | + * Determine whether the object contains an image resource. |
|
| 95 | + * |
|
| 96 | + * @return bool |
|
| 97 | + */ |
|
| 98 | + public function valid() { // apparently you can't name a method 'empty'... |
|
| 99 | + return is_resource($this->resource); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Returns the MIME type of the image or an empty string if no image is loaded. |
|
| 104 | - * |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function mimeType() { |
|
| 108 | - return $this->valid() ? $this->mimeType : ''; |
|
| 109 | - } |
|
| 102 | + /** |
|
| 103 | + * Returns the MIME type of the image or an empty string if no image is loaded. |
|
| 104 | + * |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function mimeType() { |
|
| 108 | + return $this->valid() ? $this->mimeType : ''; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Returns the width of the image or -1 if no image is loaded. |
|
| 113 | - * |
|
| 114 | - * @return int |
|
| 115 | - */ |
|
| 116 | - public function width() { |
|
| 117 | - return $this->valid() ? imagesx($this->resource) : -1; |
|
| 118 | - } |
|
| 111 | + /** |
|
| 112 | + * Returns the width of the image or -1 if no image is loaded. |
|
| 113 | + * |
|
| 114 | + * @return int |
|
| 115 | + */ |
|
| 116 | + public function width() { |
|
| 117 | + return $this->valid() ? imagesx($this->resource) : -1; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Returns the height of the image or -1 if no image is loaded. |
|
| 122 | - * |
|
| 123 | - * @return int |
|
| 124 | - */ |
|
| 125 | - public function height() { |
|
| 126 | - return $this->valid() ? imagesy($this->resource) : -1; |
|
| 127 | - } |
|
| 120 | + /** |
|
| 121 | + * Returns the height of the image or -1 if no image is loaded. |
|
| 122 | + * |
|
| 123 | + * @return int |
|
| 124 | + */ |
|
| 125 | + public function height() { |
|
| 126 | + return $this->valid() ? imagesy($this->resource) : -1; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * Returns the width when the image orientation is top-left. |
|
| 131 | - * |
|
| 132 | - * @return int |
|
| 133 | - */ |
|
| 134 | - public function widthTopLeft() { |
|
| 135 | - $o = $this->getOrientation(); |
|
| 136 | - $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 137 | - switch ($o) { |
|
| 138 | - case -1: |
|
| 139 | - case 1: |
|
| 140 | - case 2: // Not tested |
|
| 141 | - case 3: |
|
| 142 | - case 4: // Not tested |
|
| 143 | - return $this->width(); |
|
| 144 | - case 5: // Not tested |
|
| 145 | - case 6: |
|
| 146 | - case 7: // Not tested |
|
| 147 | - case 8: |
|
| 148 | - return $this->height(); |
|
| 149 | - } |
|
| 150 | - return $this->width(); |
|
| 151 | - } |
|
| 129 | + /** |
|
| 130 | + * Returns the width when the image orientation is top-left. |
|
| 131 | + * |
|
| 132 | + * @return int |
|
| 133 | + */ |
|
| 134 | + public function widthTopLeft() { |
|
| 135 | + $o = $this->getOrientation(); |
|
| 136 | + $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 137 | + switch ($o) { |
|
| 138 | + case -1: |
|
| 139 | + case 1: |
|
| 140 | + case 2: // Not tested |
|
| 141 | + case 3: |
|
| 142 | + case 4: // Not tested |
|
| 143 | + return $this->width(); |
|
| 144 | + case 5: // Not tested |
|
| 145 | + case 6: |
|
| 146 | + case 7: // Not tested |
|
| 147 | + case 8: |
|
| 148 | + return $this->height(); |
|
| 149 | + } |
|
| 150 | + return $this->width(); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Returns the height when the image orientation is top-left. |
|
| 155 | - * |
|
| 156 | - * @return int |
|
| 157 | - */ |
|
| 158 | - public function heightTopLeft() { |
|
| 159 | - $o = $this->getOrientation(); |
|
| 160 | - $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 161 | - switch ($o) { |
|
| 162 | - case -1: |
|
| 163 | - case 1: |
|
| 164 | - case 2: // Not tested |
|
| 165 | - case 3: |
|
| 166 | - case 4: // Not tested |
|
| 167 | - return $this->height(); |
|
| 168 | - case 5: // Not tested |
|
| 169 | - case 6: |
|
| 170 | - case 7: // Not tested |
|
| 171 | - case 8: |
|
| 172 | - return $this->width(); |
|
| 173 | - } |
|
| 174 | - return $this->height(); |
|
| 175 | - } |
|
| 153 | + /** |
|
| 154 | + * Returns the height when the image orientation is top-left. |
|
| 155 | + * |
|
| 156 | + * @return int |
|
| 157 | + */ |
|
| 158 | + public function heightTopLeft() { |
|
| 159 | + $o = $this->getOrientation(); |
|
| 160 | + $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 161 | + switch ($o) { |
|
| 162 | + case -1: |
|
| 163 | + case 1: |
|
| 164 | + case 2: // Not tested |
|
| 165 | + case 3: |
|
| 166 | + case 4: // Not tested |
|
| 167 | + return $this->height(); |
|
| 168 | + case 5: // Not tested |
|
| 169 | + case 6: |
|
| 170 | + case 7: // Not tested |
|
| 171 | + case 8: |
|
| 172 | + return $this->width(); |
|
| 173 | + } |
|
| 174 | + return $this->height(); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Outputs the image. |
|
| 179 | - * |
|
| 180 | - * @param string $mimeType |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - public function show($mimeType = null) { |
|
| 184 | - if ($mimeType === null) { |
|
| 185 | - $mimeType = $this->mimeType(); |
|
| 186 | - } |
|
| 187 | - header('Content-Type: ' . $mimeType); |
|
| 188 | - return $this->_output(null, $mimeType); |
|
| 189 | - } |
|
| 177 | + /** |
|
| 178 | + * Outputs the image. |
|
| 179 | + * |
|
| 180 | + * @param string $mimeType |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + public function show($mimeType = null) { |
|
| 184 | + if ($mimeType === null) { |
|
| 185 | + $mimeType = $this->mimeType(); |
|
| 186 | + } |
|
| 187 | + header('Content-Type: ' . $mimeType); |
|
| 188 | + return $this->_output(null, $mimeType); |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - /** |
|
| 192 | - * Saves the image. |
|
| 193 | - * |
|
| 194 | - * @param string $filePath |
|
| 195 | - * @param string $mimeType |
|
| 196 | - * @return bool |
|
| 197 | - */ |
|
| 191 | + /** |
|
| 192 | + * Saves the image. |
|
| 193 | + * |
|
| 194 | + * @param string $filePath |
|
| 195 | + * @param string $mimeType |
|
| 196 | + * @return bool |
|
| 197 | + */ |
|
| 198 | 198 | |
| 199 | - public function save($filePath = null, $mimeType = null) { |
|
| 200 | - if ($mimeType === null) { |
|
| 201 | - $mimeType = $this->mimeType(); |
|
| 202 | - } |
|
| 203 | - if ($filePath === null) { |
|
| 204 | - if ($this->filePath === null) { |
|
| 205 | - $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 206 | - return false; |
|
| 207 | - } else { |
|
| 208 | - $filePath = $this->filePath; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - return $this->_output($filePath, $mimeType); |
|
| 212 | - } |
|
| 199 | + public function save($filePath = null, $mimeType = null) { |
|
| 200 | + if ($mimeType === null) { |
|
| 201 | + $mimeType = $this->mimeType(); |
|
| 202 | + } |
|
| 203 | + if ($filePath === null) { |
|
| 204 | + if ($this->filePath === null) { |
|
| 205 | + $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 206 | + return false; |
|
| 207 | + } else { |
|
| 208 | + $filePath = $this->filePath; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + return $this->_output($filePath, $mimeType); |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Outputs/saves the image. |
|
| 216 | - * |
|
| 217 | - * @param string $filePath |
|
| 218 | - * @param string $mimeType |
|
| 219 | - * @return bool |
|
| 220 | - * @throws Exception |
|
| 221 | - */ |
|
| 222 | - private function _output($filePath = null, $mimeType = null) { |
|
| 223 | - if ($filePath) { |
|
| 224 | - if (!file_exists(dirname($filePath))) { |
|
| 225 | - mkdir(dirname($filePath), 0777, true); |
|
| 226 | - } |
|
| 227 | - $isWritable = is_writable(dirname($filePath)); |
|
| 228 | - if (!$isWritable) { |
|
| 229 | - $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 230 | - return false; |
|
| 231 | - } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
|
| 232 | - $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 233 | - return false; |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - if (!$this->valid()) { |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 214 | + /** |
|
| 215 | + * Outputs/saves the image. |
|
| 216 | + * |
|
| 217 | + * @param string $filePath |
|
| 218 | + * @param string $mimeType |
|
| 219 | + * @return bool |
|
| 220 | + * @throws Exception |
|
| 221 | + */ |
|
| 222 | + private function _output($filePath = null, $mimeType = null) { |
|
| 223 | + if ($filePath) { |
|
| 224 | + if (!file_exists(dirname($filePath))) { |
|
| 225 | + mkdir(dirname($filePath), 0777, true); |
|
| 226 | + } |
|
| 227 | + $isWritable = is_writable(dirname($filePath)); |
|
| 228 | + if (!$isWritable) { |
|
| 229 | + $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 230 | + return false; |
|
| 231 | + } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
|
| 232 | + $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 233 | + return false; |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + if (!$this->valid()) { |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - $imageType = $this->imageType; |
|
| 241 | - if ($mimeType !== null) { |
|
| 242 | - switch ($mimeType) { |
|
| 243 | - case 'image/gif': |
|
| 244 | - $imageType = IMAGETYPE_GIF; |
|
| 245 | - break; |
|
| 246 | - case 'image/jpeg': |
|
| 247 | - $imageType = IMAGETYPE_JPEG; |
|
| 248 | - break; |
|
| 249 | - case 'image/png': |
|
| 250 | - $imageType = IMAGETYPE_PNG; |
|
| 251 | - break; |
|
| 252 | - case 'image/x-xbitmap': |
|
| 253 | - $imageType = IMAGETYPE_XBM; |
|
| 254 | - break; |
|
| 255 | - case 'image/bmp': |
|
| 256 | - case 'image/x-ms-bmp': |
|
| 257 | - $imageType = IMAGETYPE_BMP; |
|
| 258 | - break; |
|
| 259 | - default: |
|
| 260 | - throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 261 | - } |
|
| 262 | - } |
|
| 240 | + $imageType = $this->imageType; |
|
| 241 | + if ($mimeType !== null) { |
|
| 242 | + switch ($mimeType) { |
|
| 243 | + case 'image/gif': |
|
| 244 | + $imageType = IMAGETYPE_GIF; |
|
| 245 | + break; |
|
| 246 | + case 'image/jpeg': |
|
| 247 | + $imageType = IMAGETYPE_JPEG; |
|
| 248 | + break; |
|
| 249 | + case 'image/png': |
|
| 250 | + $imageType = IMAGETYPE_PNG; |
|
| 251 | + break; |
|
| 252 | + case 'image/x-xbitmap': |
|
| 253 | + $imageType = IMAGETYPE_XBM; |
|
| 254 | + break; |
|
| 255 | + case 'image/bmp': |
|
| 256 | + case 'image/x-ms-bmp': |
|
| 257 | + $imageType = IMAGETYPE_BMP; |
|
| 258 | + break; |
|
| 259 | + default: |
|
| 260 | + throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - switch ($imageType) { |
|
| 265 | - case IMAGETYPE_GIF: |
|
| 266 | - $retVal = imagegif($this->resource, $filePath); |
|
| 267 | - break; |
|
| 268 | - case IMAGETYPE_JPEG: |
|
| 269 | - $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality()); |
|
| 270 | - break; |
|
| 271 | - case IMAGETYPE_PNG: |
|
| 272 | - $retVal = imagepng($this->resource, $filePath); |
|
| 273 | - break; |
|
| 274 | - case IMAGETYPE_XBM: |
|
| 275 | - if (function_exists('imagexbm')) { |
|
| 276 | - $retVal = imagexbm($this->resource, $filePath); |
|
| 277 | - } else { |
|
| 278 | - throw new Exception('\OC_Image::_output(): imagexbm() is not supported.'); |
|
| 279 | - } |
|
| 264 | + switch ($imageType) { |
|
| 265 | + case IMAGETYPE_GIF: |
|
| 266 | + $retVal = imagegif($this->resource, $filePath); |
|
| 267 | + break; |
|
| 268 | + case IMAGETYPE_JPEG: |
|
| 269 | + $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality()); |
|
| 270 | + break; |
|
| 271 | + case IMAGETYPE_PNG: |
|
| 272 | + $retVal = imagepng($this->resource, $filePath); |
|
| 273 | + break; |
|
| 274 | + case IMAGETYPE_XBM: |
|
| 275 | + if (function_exists('imagexbm')) { |
|
| 276 | + $retVal = imagexbm($this->resource, $filePath); |
|
| 277 | + } else { |
|
| 278 | + throw new Exception('\OC_Image::_output(): imagexbm() is not supported.'); |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - break; |
|
| 282 | - case IMAGETYPE_WBMP: |
|
| 283 | - $retVal = imagewbmp($this->resource, $filePath); |
|
| 284 | - break; |
|
| 285 | - case IMAGETYPE_BMP: |
|
| 286 | - $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); |
|
| 287 | - break; |
|
| 288 | - default: |
|
| 289 | - $retVal = imagepng($this->resource, $filePath); |
|
| 290 | - } |
|
| 291 | - return $retVal; |
|
| 292 | - } |
|
| 281 | + break; |
|
| 282 | + case IMAGETYPE_WBMP: |
|
| 283 | + $retVal = imagewbmp($this->resource, $filePath); |
|
| 284 | + break; |
|
| 285 | + case IMAGETYPE_BMP: |
|
| 286 | + $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); |
|
| 287 | + break; |
|
| 288 | + default: |
|
| 289 | + $retVal = imagepng($this->resource, $filePath); |
|
| 290 | + } |
|
| 291 | + return $retVal; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * Prints the image when called as $image(). |
|
| 296 | - */ |
|
| 297 | - public function __invoke() { |
|
| 298 | - return $this->show(); |
|
| 299 | - } |
|
| 294 | + /** |
|
| 295 | + * Prints the image when called as $image(). |
|
| 296 | + */ |
|
| 297 | + public function __invoke() { |
|
| 298 | + return $this->show(); |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * @param resource Returns the image resource in any. |
|
| 303 | - * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" |
|
| 304 | - */ |
|
| 305 | - public function setResource($resource) { |
|
| 306 | - if (get_resource_type($resource) === 'gd') { |
|
| 307 | - $this->resource = $resource; |
|
| 308 | - return; |
|
| 309 | - } |
|
| 310 | - throw new \InvalidArgumentException('Supplied resource is not of type "gd".'); |
|
| 311 | - } |
|
| 301 | + /** |
|
| 302 | + * @param resource Returns the image resource in any. |
|
| 303 | + * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" |
|
| 304 | + */ |
|
| 305 | + public function setResource($resource) { |
|
| 306 | + if (get_resource_type($resource) === 'gd') { |
|
| 307 | + $this->resource = $resource; |
|
| 308 | + return; |
|
| 309 | + } |
|
| 310 | + throw new \InvalidArgumentException('Supplied resource is not of type "gd".'); |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - /** |
|
| 314 | - * @return resource Returns the image resource in any. |
|
| 315 | - */ |
|
| 316 | - public function resource() { |
|
| 317 | - return $this->resource; |
|
| 318 | - } |
|
| 313 | + /** |
|
| 314 | + * @return resource Returns the image resource in any. |
|
| 315 | + */ |
|
| 316 | + public function resource() { |
|
| 317 | + return $this->resource; |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - /** |
|
| 321 | - * @return string Returns the mimetype of the data. Returns the empty string |
|
| 322 | - * if the data is not valid. |
|
| 323 | - */ |
|
| 324 | - public function dataMimeType() { |
|
| 325 | - if (!$this->valid()) { |
|
| 326 | - return ''; |
|
| 327 | - } |
|
| 320 | + /** |
|
| 321 | + * @return string Returns the mimetype of the data. Returns the empty string |
|
| 322 | + * if the data is not valid. |
|
| 323 | + */ |
|
| 324 | + public function dataMimeType() { |
|
| 325 | + if (!$this->valid()) { |
|
| 326 | + return ''; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - switch ($this->mimeType) { |
|
| 330 | - case 'image/png': |
|
| 331 | - case 'image/jpeg': |
|
| 332 | - case 'image/gif': |
|
| 333 | - return $this->mimeType; |
|
| 334 | - default: |
|
| 335 | - return 'image/png'; |
|
| 336 | - } |
|
| 337 | - } |
|
| 329 | + switch ($this->mimeType) { |
|
| 330 | + case 'image/png': |
|
| 331 | + case 'image/jpeg': |
|
| 332 | + case 'image/gif': |
|
| 333 | + return $this->mimeType; |
|
| 334 | + default: |
|
| 335 | + return 'image/png'; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * @return null|string Returns the raw image data. |
|
| 341 | - */ |
|
| 342 | - public function data() { |
|
| 343 | - if (!$this->valid()) { |
|
| 344 | - return null; |
|
| 345 | - } |
|
| 346 | - ob_start(); |
|
| 347 | - switch ($this->mimeType) { |
|
| 348 | - case "image/png": |
|
| 349 | - $res = imagepng($this->resource); |
|
| 350 | - break; |
|
| 351 | - case "image/jpeg": |
|
| 352 | - $quality = $this->getJpegQuality(); |
|
| 353 | - if ($quality !== null) { |
|
| 354 | - $res = imagejpeg($this->resource, null, $quality); |
|
| 355 | - } else { |
|
| 356 | - $res = imagejpeg($this->resource); |
|
| 357 | - } |
|
| 358 | - break; |
|
| 359 | - case "image/gif": |
|
| 360 | - $res = imagegif($this->resource); |
|
| 361 | - break; |
|
| 362 | - default: |
|
| 363 | - $res = imagepng($this->resource); |
|
| 364 | - $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core')); |
|
| 365 | - break; |
|
| 366 | - } |
|
| 367 | - if (!$res) { |
|
| 368 | - $this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core')); |
|
| 369 | - } |
|
| 370 | - return ob_get_clean(); |
|
| 371 | - } |
|
| 339 | + /** |
|
| 340 | + * @return null|string Returns the raw image data. |
|
| 341 | + */ |
|
| 342 | + public function data() { |
|
| 343 | + if (!$this->valid()) { |
|
| 344 | + return null; |
|
| 345 | + } |
|
| 346 | + ob_start(); |
|
| 347 | + switch ($this->mimeType) { |
|
| 348 | + case "image/png": |
|
| 349 | + $res = imagepng($this->resource); |
|
| 350 | + break; |
|
| 351 | + case "image/jpeg": |
|
| 352 | + $quality = $this->getJpegQuality(); |
|
| 353 | + if ($quality !== null) { |
|
| 354 | + $res = imagejpeg($this->resource, null, $quality); |
|
| 355 | + } else { |
|
| 356 | + $res = imagejpeg($this->resource); |
|
| 357 | + } |
|
| 358 | + break; |
|
| 359 | + case "image/gif": |
|
| 360 | + $res = imagegif($this->resource); |
|
| 361 | + break; |
|
| 362 | + default: |
|
| 363 | + $res = imagepng($this->resource); |
|
| 364 | + $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core')); |
|
| 365 | + break; |
|
| 366 | + } |
|
| 367 | + if (!$res) { |
|
| 368 | + $this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core')); |
|
| 369 | + } |
|
| 370 | + return ob_get_clean(); |
|
| 371 | + } |
|
| 372 | 372 | |
| 373 | - /** |
|
| 374 | - * @return string - base64 encoded, which is suitable for embedding in a VCard. |
|
| 375 | - */ |
|
| 376 | - public function __toString() { |
|
| 377 | - return base64_encode($this->data()); |
|
| 378 | - } |
|
| 373 | + /** |
|
| 374 | + * @return string - base64 encoded, which is suitable for embedding in a VCard. |
|
| 375 | + */ |
|
| 376 | + public function __toString() { |
|
| 377 | + return base64_encode($this->data()); |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * @return int|null |
|
| 382 | - */ |
|
| 383 | - protected function getJpegQuality() { |
|
| 384 | - $quality = $this->config->getAppValue('preview', 'jpeg_quality', 90); |
|
| 385 | - if ($quality !== null) { |
|
| 386 | - $quality = min(100, max(10, (int) $quality)); |
|
| 387 | - } |
|
| 388 | - return $quality; |
|
| 389 | - } |
|
| 380 | + /** |
|
| 381 | + * @return int|null |
|
| 382 | + */ |
|
| 383 | + protected function getJpegQuality() { |
|
| 384 | + $quality = $this->config->getAppValue('preview', 'jpeg_quality', 90); |
|
| 385 | + if ($quality !== null) { |
|
| 386 | + $quality = min(100, max(10, (int) $quality)); |
|
| 387 | + } |
|
| 388 | + return $quality; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | - /** |
|
| 392 | - * (I'm open for suggestions on better method name ;) |
|
| 393 | - * Get the orientation based on EXIF data. |
|
| 394 | - * |
|
| 395 | - * @return int The orientation or -1 if no EXIF data is available. |
|
| 396 | - */ |
|
| 397 | - public function getOrientation() { |
|
| 398 | - if ($this->exif !== null) { |
|
| 399 | - return $this->exif['Orientation']; |
|
| 400 | - } |
|
| 391 | + /** |
|
| 392 | + * (I'm open for suggestions on better method name ;) |
|
| 393 | + * Get the orientation based on EXIF data. |
|
| 394 | + * |
|
| 395 | + * @return int The orientation or -1 if no EXIF data is available. |
|
| 396 | + */ |
|
| 397 | + public function getOrientation() { |
|
| 398 | + if ($this->exif !== null) { |
|
| 399 | + return $this->exif['Orientation']; |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | - if ($this->imageType !== IMAGETYPE_JPEG) { |
|
| 403 | - $this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core')); |
|
| 404 | - return -1; |
|
| 405 | - } |
|
| 406 | - if (!is_callable('exif_read_data')) { |
|
| 407 | - $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 408 | - return -1; |
|
| 409 | - } |
|
| 410 | - if (!$this->valid()) { |
|
| 411 | - $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 412 | - return -1; |
|
| 413 | - } |
|
| 414 | - if (is_null($this->filePath) || !is_readable($this->filePath)) { |
|
| 415 | - $this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core')); |
|
| 416 | - return -1; |
|
| 417 | - } |
|
| 418 | - $exif = @exif_read_data($this->filePath, 'IFD0'); |
|
| 419 | - if (!$exif) { |
|
| 420 | - return -1; |
|
| 421 | - } |
|
| 422 | - if (!isset($exif['Orientation'])) { |
|
| 423 | - return -1; |
|
| 424 | - } |
|
| 425 | - $this->exif = $exif; |
|
| 426 | - return $exif['Orientation']; |
|
| 427 | - } |
|
| 402 | + if ($this->imageType !== IMAGETYPE_JPEG) { |
|
| 403 | + $this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core')); |
|
| 404 | + return -1; |
|
| 405 | + } |
|
| 406 | + if (!is_callable('exif_read_data')) { |
|
| 407 | + $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 408 | + return -1; |
|
| 409 | + } |
|
| 410 | + if (!$this->valid()) { |
|
| 411 | + $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 412 | + return -1; |
|
| 413 | + } |
|
| 414 | + if (is_null($this->filePath) || !is_readable($this->filePath)) { |
|
| 415 | + $this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core')); |
|
| 416 | + return -1; |
|
| 417 | + } |
|
| 418 | + $exif = @exif_read_data($this->filePath, 'IFD0'); |
|
| 419 | + if (!$exif) { |
|
| 420 | + return -1; |
|
| 421 | + } |
|
| 422 | + if (!isset($exif['Orientation'])) { |
|
| 423 | + return -1; |
|
| 424 | + } |
|
| 425 | + $this->exif = $exif; |
|
| 426 | + return $exif['Orientation']; |
|
| 427 | + } |
|
| 428 | 428 | |
| 429 | - public function readExif($data) { |
|
| 430 | - if (!is_callable('exif_read_data')) { |
|
| 431 | - $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 432 | - return; |
|
| 433 | - } |
|
| 434 | - if (!$this->valid()) { |
|
| 435 | - $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 436 | - return; |
|
| 437 | - } |
|
| 429 | + public function readExif($data) { |
|
| 430 | + if (!is_callable('exif_read_data')) { |
|
| 431 | + $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 432 | + return; |
|
| 433 | + } |
|
| 434 | + if (!$this->valid()) { |
|
| 435 | + $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 436 | + return; |
|
| 437 | + } |
|
| 438 | 438 | |
| 439 | - $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 440 | - if (!$exif) { |
|
| 441 | - return; |
|
| 442 | - } |
|
| 443 | - if (!isset($exif['Orientation'])) { |
|
| 444 | - return; |
|
| 445 | - } |
|
| 446 | - $this->exif = $exif; |
|
| 447 | - } |
|
| 439 | + $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 440 | + if (!$exif) { |
|
| 441 | + return; |
|
| 442 | + } |
|
| 443 | + if (!isset($exif['Orientation'])) { |
|
| 444 | + return; |
|
| 445 | + } |
|
| 446 | + $this->exif = $exif; |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - /** |
|
| 450 | - * (I'm open for suggestions on better method name ;) |
|
| 451 | - * Fixes orientation based on EXIF data. |
|
| 452 | - * |
|
| 453 | - * @return bool |
|
| 454 | - */ |
|
| 455 | - public function fixOrientation() { |
|
| 456 | - $o = $this->getOrientation(); |
|
| 457 | - $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 458 | - $rotate = 0; |
|
| 459 | - $flip = false; |
|
| 460 | - switch ($o) { |
|
| 461 | - case -1: |
|
| 462 | - return false; //Nothing to fix |
|
| 463 | - case 1: |
|
| 464 | - $rotate = 0; |
|
| 465 | - break; |
|
| 466 | - case 2: |
|
| 467 | - $rotate = 0; |
|
| 468 | - $flip = true; |
|
| 469 | - break; |
|
| 470 | - case 3: |
|
| 471 | - $rotate = 180; |
|
| 472 | - break; |
|
| 473 | - case 4: |
|
| 474 | - $rotate = 180; |
|
| 475 | - $flip = true; |
|
| 476 | - break; |
|
| 477 | - case 5: |
|
| 478 | - $rotate = 90; |
|
| 479 | - $flip = true; |
|
| 480 | - break; |
|
| 481 | - case 6: |
|
| 482 | - $rotate = 270; |
|
| 483 | - break; |
|
| 484 | - case 7: |
|
| 485 | - $rotate = 270; |
|
| 486 | - $flip = true; |
|
| 487 | - break; |
|
| 488 | - case 8: |
|
| 489 | - $rotate = 90; |
|
| 490 | - break; |
|
| 491 | - } |
|
| 492 | - if($flip && function_exists('imageflip')) { |
|
| 493 | - imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
|
| 494 | - } |
|
| 495 | - if ($rotate) { |
|
| 496 | - $res = imagerotate($this->resource, $rotate, 0); |
|
| 497 | - if ($res) { |
|
| 498 | - if (imagealphablending($res, true)) { |
|
| 499 | - if (imagesavealpha($res, true)) { |
|
| 500 | - imagedestroy($this->resource); |
|
| 501 | - $this->resource = $res; |
|
| 502 | - return true; |
|
| 503 | - } else { |
|
| 504 | - $this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core')); |
|
| 505 | - return false; |
|
| 506 | - } |
|
| 507 | - } else { |
|
| 508 | - $this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core')); |
|
| 509 | - return false; |
|
| 510 | - } |
|
| 511 | - } else { |
|
| 512 | - $this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core')); |
|
| 513 | - return false; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - return false; |
|
| 517 | - } |
|
| 449 | + /** |
|
| 450 | + * (I'm open for suggestions on better method name ;) |
|
| 451 | + * Fixes orientation based on EXIF data. |
|
| 452 | + * |
|
| 453 | + * @return bool |
|
| 454 | + */ |
|
| 455 | + public function fixOrientation() { |
|
| 456 | + $o = $this->getOrientation(); |
|
| 457 | + $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 458 | + $rotate = 0; |
|
| 459 | + $flip = false; |
|
| 460 | + switch ($o) { |
|
| 461 | + case -1: |
|
| 462 | + return false; //Nothing to fix |
|
| 463 | + case 1: |
|
| 464 | + $rotate = 0; |
|
| 465 | + break; |
|
| 466 | + case 2: |
|
| 467 | + $rotate = 0; |
|
| 468 | + $flip = true; |
|
| 469 | + break; |
|
| 470 | + case 3: |
|
| 471 | + $rotate = 180; |
|
| 472 | + break; |
|
| 473 | + case 4: |
|
| 474 | + $rotate = 180; |
|
| 475 | + $flip = true; |
|
| 476 | + break; |
|
| 477 | + case 5: |
|
| 478 | + $rotate = 90; |
|
| 479 | + $flip = true; |
|
| 480 | + break; |
|
| 481 | + case 6: |
|
| 482 | + $rotate = 270; |
|
| 483 | + break; |
|
| 484 | + case 7: |
|
| 485 | + $rotate = 270; |
|
| 486 | + $flip = true; |
|
| 487 | + break; |
|
| 488 | + case 8: |
|
| 489 | + $rotate = 90; |
|
| 490 | + break; |
|
| 491 | + } |
|
| 492 | + if($flip && function_exists('imageflip')) { |
|
| 493 | + imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
|
| 494 | + } |
|
| 495 | + if ($rotate) { |
|
| 496 | + $res = imagerotate($this->resource, $rotate, 0); |
|
| 497 | + if ($res) { |
|
| 498 | + if (imagealphablending($res, true)) { |
|
| 499 | + if (imagesavealpha($res, true)) { |
|
| 500 | + imagedestroy($this->resource); |
|
| 501 | + $this->resource = $res; |
|
| 502 | + return true; |
|
| 503 | + } else { |
|
| 504 | + $this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core')); |
|
| 505 | + return false; |
|
| 506 | + } |
|
| 507 | + } else { |
|
| 508 | + $this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core')); |
|
| 509 | + return false; |
|
| 510 | + } |
|
| 511 | + } else { |
|
| 512 | + $this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core')); |
|
| 513 | + return false; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + return false; |
|
| 517 | + } |
|
| 518 | 518 | |
| 519 | - /** |
|
| 520 | - * Loads an image from an open file handle. |
|
| 521 | - * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. |
|
| 522 | - * |
|
| 523 | - * @param resource $handle |
|
| 524 | - * @return resource|false An image resource or false on error |
|
| 525 | - */ |
|
| 526 | - public function loadFromFileHandle($handle) { |
|
| 527 | - $contents = stream_get_contents($handle); |
|
| 528 | - if ($this->loadFromData($contents)) { |
|
| 529 | - return $this->resource; |
|
| 530 | - } |
|
| 531 | - return false; |
|
| 532 | - } |
|
| 519 | + /** |
|
| 520 | + * Loads an image from an open file handle. |
|
| 521 | + * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. |
|
| 522 | + * |
|
| 523 | + * @param resource $handle |
|
| 524 | + * @return resource|false An image resource or false on error |
|
| 525 | + */ |
|
| 526 | + public function loadFromFileHandle($handle) { |
|
| 527 | + $contents = stream_get_contents($handle); |
|
| 528 | + if ($this->loadFromData($contents)) { |
|
| 529 | + return $this->resource; |
|
| 530 | + } |
|
| 531 | + return false; |
|
| 532 | + } |
|
| 533 | 533 | |
| 534 | - /** |
|
| 535 | - * Loads an image from a local file. |
|
| 536 | - * |
|
| 537 | - * @param bool|string $imagePath The path to a local file. |
|
| 538 | - * @return bool|resource An image resource or false on error |
|
| 539 | - */ |
|
| 540 | - public function loadFromFile($imagePath = false) { |
|
| 541 | - // exif_imagetype throws "read error!" if file is less than 12 byte |
|
| 542 | - if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { |
|
| 543 | - return false; |
|
| 544 | - } |
|
| 545 | - $iType = exif_imagetype($imagePath); |
|
| 546 | - switch ($iType) { |
|
| 547 | - case IMAGETYPE_GIF: |
|
| 548 | - if (imagetypes() & IMG_GIF) { |
|
| 549 | - $this->resource = imagecreatefromgif($imagePath); |
|
| 550 | - // Preserve transparency |
|
| 551 | - imagealphablending($this->resource, true); |
|
| 552 | - imagesavealpha($this->resource, true); |
|
| 553 | - } else { |
|
| 554 | - $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 555 | - } |
|
| 556 | - break; |
|
| 557 | - case IMAGETYPE_JPEG: |
|
| 558 | - if (imagetypes() & IMG_JPG) { |
|
| 559 | - if (getimagesize($imagePath) !== false) { |
|
| 560 | - $this->resource = @imagecreatefromjpeg($imagePath); |
|
| 561 | - } else { |
|
| 562 | - $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 563 | - } |
|
| 564 | - } else { |
|
| 565 | - $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 566 | - } |
|
| 567 | - break; |
|
| 568 | - case IMAGETYPE_PNG: |
|
| 569 | - if (imagetypes() & IMG_PNG) { |
|
| 570 | - $this->resource = @imagecreatefrompng($imagePath); |
|
| 571 | - // Preserve transparency |
|
| 572 | - imagealphablending($this->resource, true); |
|
| 573 | - imagesavealpha($this->resource, true); |
|
| 574 | - } else { |
|
| 575 | - $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 576 | - } |
|
| 577 | - break; |
|
| 578 | - case IMAGETYPE_XBM: |
|
| 579 | - if (imagetypes() & IMG_XPM) { |
|
| 580 | - $this->resource = @imagecreatefromxbm($imagePath); |
|
| 581 | - } else { |
|
| 582 | - $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 583 | - } |
|
| 584 | - break; |
|
| 585 | - case IMAGETYPE_WBMP: |
|
| 586 | - if (imagetypes() & IMG_WBMP) { |
|
| 587 | - $this->resource = @imagecreatefromwbmp($imagePath); |
|
| 588 | - } else { |
|
| 589 | - $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 590 | - } |
|
| 591 | - break; |
|
| 592 | - case IMAGETYPE_BMP: |
|
| 593 | - $this->resource = $this->imagecreatefrombmp($imagePath); |
|
| 594 | - break; |
|
| 595 | - /* |
|
| 534 | + /** |
|
| 535 | + * Loads an image from a local file. |
|
| 536 | + * |
|
| 537 | + * @param bool|string $imagePath The path to a local file. |
|
| 538 | + * @return bool|resource An image resource or false on error |
|
| 539 | + */ |
|
| 540 | + public function loadFromFile($imagePath = false) { |
|
| 541 | + // exif_imagetype throws "read error!" if file is less than 12 byte |
|
| 542 | + if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { |
|
| 543 | + return false; |
|
| 544 | + } |
|
| 545 | + $iType = exif_imagetype($imagePath); |
|
| 546 | + switch ($iType) { |
|
| 547 | + case IMAGETYPE_GIF: |
|
| 548 | + if (imagetypes() & IMG_GIF) { |
|
| 549 | + $this->resource = imagecreatefromgif($imagePath); |
|
| 550 | + // Preserve transparency |
|
| 551 | + imagealphablending($this->resource, true); |
|
| 552 | + imagesavealpha($this->resource, true); |
|
| 553 | + } else { |
|
| 554 | + $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 555 | + } |
|
| 556 | + break; |
|
| 557 | + case IMAGETYPE_JPEG: |
|
| 558 | + if (imagetypes() & IMG_JPG) { |
|
| 559 | + if (getimagesize($imagePath) !== false) { |
|
| 560 | + $this->resource = @imagecreatefromjpeg($imagePath); |
|
| 561 | + } else { |
|
| 562 | + $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 563 | + } |
|
| 564 | + } else { |
|
| 565 | + $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 566 | + } |
|
| 567 | + break; |
|
| 568 | + case IMAGETYPE_PNG: |
|
| 569 | + if (imagetypes() & IMG_PNG) { |
|
| 570 | + $this->resource = @imagecreatefrompng($imagePath); |
|
| 571 | + // Preserve transparency |
|
| 572 | + imagealphablending($this->resource, true); |
|
| 573 | + imagesavealpha($this->resource, true); |
|
| 574 | + } else { |
|
| 575 | + $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 576 | + } |
|
| 577 | + break; |
|
| 578 | + case IMAGETYPE_XBM: |
|
| 579 | + if (imagetypes() & IMG_XPM) { |
|
| 580 | + $this->resource = @imagecreatefromxbm($imagePath); |
|
| 581 | + } else { |
|
| 582 | + $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 583 | + } |
|
| 584 | + break; |
|
| 585 | + case IMAGETYPE_WBMP: |
|
| 586 | + if (imagetypes() & IMG_WBMP) { |
|
| 587 | + $this->resource = @imagecreatefromwbmp($imagePath); |
|
| 588 | + } else { |
|
| 589 | + $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 590 | + } |
|
| 591 | + break; |
|
| 592 | + case IMAGETYPE_BMP: |
|
| 593 | + $this->resource = $this->imagecreatefrombmp($imagePath); |
|
| 594 | + break; |
|
| 595 | + /* |
|
| 596 | 596 | case IMAGETYPE_TIFF_II: // (intel byte order) |
| 597 | 597 | break; |
| 598 | 598 | case IMAGETYPE_TIFF_MM: // (motorola byte order) |
@@ -616,581 +616,581 @@ discard block |
||
| 616 | 616 | case IMAGETYPE_PSD: |
| 617 | 617 | break; |
| 618 | 618 | */ |
| 619 | - default: |
|
| 619 | + default: |
|
| 620 | 620 | |
| 621 | - // this is mostly file created from encrypted file |
|
| 622 | - $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); |
|
| 623 | - $iType = IMAGETYPE_PNG; |
|
| 624 | - $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core')); |
|
| 625 | - break; |
|
| 626 | - } |
|
| 627 | - if ($this->valid()) { |
|
| 628 | - $this->imageType = $iType; |
|
| 629 | - $this->mimeType = image_type_to_mime_type($iType); |
|
| 630 | - $this->filePath = $imagePath; |
|
| 631 | - } |
|
| 632 | - return $this->resource; |
|
| 633 | - } |
|
| 621 | + // this is mostly file created from encrypted file |
|
| 622 | + $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); |
|
| 623 | + $iType = IMAGETYPE_PNG; |
|
| 624 | + $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core')); |
|
| 625 | + break; |
|
| 626 | + } |
|
| 627 | + if ($this->valid()) { |
|
| 628 | + $this->imageType = $iType; |
|
| 629 | + $this->mimeType = image_type_to_mime_type($iType); |
|
| 630 | + $this->filePath = $imagePath; |
|
| 631 | + } |
|
| 632 | + return $this->resource; |
|
| 633 | + } |
|
| 634 | 634 | |
| 635 | - /** |
|
| 636 | - * Loads an image from a string of data. |
|
| 637 | - * |
|
| 638 | - * @param string $str A string of image data as read from a file. |
|
| 639 | - * @return bool|resource An image resource or false on error |
|
| 640 | - */ |
|
| 641 | - public function loadFromData($str) { |
|
| 642 | - if (is_resource($str)) { |
|
| 643 | - return false; |
|
| 644 | - } |
|
| 645 | - $this->resource = @imagecreatefromstring($str); |
|
| 646 | - if ($this->fileInfo) { |
|
| 647 | - $this->mimeType = $this->fileInfo->buffer($str); |
|
| 648 | - } |
|
| 649 | - if (is_resource($this->resource)) { |
|
| 650 | - imagealphablending($this->resource, false); |
|
| 651 | - imagesavealpha($this->resource, true); |
|
| 652 | - } |
|
| 635 | + /** |
|
| 636 | + * Loads an image from a string of data. |
|
| 637 | + * |
|
| 638 | + * @param string $str A string of image data as read from a file. |
|
| 639 | + * @return bool|resource An image resource or false on error |
|
| 640 | + */ |
|
| 641 | + public function loadFromData($str) { |
|
| 642 | + if (is_resource($str)) { |
|
| 643 | + return false; |
|
| 644 | + } |
|
| 645 | + $this->resource = @imagecreatefromstring($str); |
|
| 646 | + if ($this->fileInfo) { |
|
| 647 | + $this->mimeType = $this->fileInfo->buffer($str); |
|
| 648 | + } |
|
| 649 | + if (is_resource($this->resource)) { |
|
| 650 | + imagealphablending($this->resource, false); |
|
| 651 | + imagesavealpha($this->resource, true); |
|
| 652 | + } |
|
| 653 | 653 | |
| 654 | - if (!$this->resource) { |
|
| 655 | - $this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core')); |
|
| 656 | - return false; |
|
| 657 | - } |
|
| 658 | - return $this->resource; |
|
| 659 | - } |
|
| 654 | + if (!$this->resource) { |
|
| 655 | + $this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core')); |
|
| 656 | + return false; |
|
| 657 | + } |
|
| 658 | + return $this->resource; |
|
| 659 | + } |
|
| 660 | 660 | |
| 661 | - /** |
|
| 662 | - * Loads an image from a base64 encoded string. |
|
| 663 | - * |
|
| 664 | - * @param string $str A string base64 encoded string of image data. |
|
| 665 | - * @return bool|resource An image resource or false on error |
|
| 666 | - */ |
|
| 667 | - public function loadFromBase64($str) { |
|
| 668 | - if (!is_string($str)) { |
|
| 669 | - return false; |
|
| 670 | - } |
|
| 671 | - $data = base64_decode($str); |
|
| 672 | - if ($data) { // try to load from string data |
|
| 673 | - $this->resource = @imagecreatefromstring($data); |
|
| 674 | - if ($this->fileInfo) { |
|
| 675 | - $this->mimeType = $this->fileInfo->buffer($data); |
|
| 676 | - } |
|
| 677 | - if (!$this->resource) { |
|
| 678 | - $this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core')); |
|
| 679 | - return false; |
|
| 680 | - } |
|
| 681 | - return $this->resource; |
|
| 682 | - } else { |
|
| 683 | - return false; |
|
| 684 | - } |
|
| 685 | - } |
|
| 661 | + /** |
|
| 662 | + * Loads an image from a base64 encoded string. |
|
| 663 | + * |
|
| 664 | + * @param string $str A string base64 encoded string of image data. |
|
| 665 | + * @return bool|resource An image resource or false on error |
|
| 666 | + */ |
|
| 667 | + public function loadFromBase64($str) { |
|
| 668 | + if (!is_string($str)) { |
|
| 669 | + return false; |
|
| 670 | + } |
|
| 671 | + $data = base64_decode($str); |
|
| 672 | + if ($data) { // try to load from string data |
|
| 673 | + $this->resource = @imagecreatefromstring($data); |
|
| 674 | + if ($this->fileInfo) { |
|
| 675 | + $this->mimeType = $this->fileInfo->buffer($data); |
|
| 676 | + } |
|
| 677 | + if (!$this->resource) { |
|
| 678 | + $this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core')); |
|
| 679 | + return false; |
|
| 680 | + } |
|
| 681 | + return $this->resource; |
|
| 682 | + } else { |
|
| 683 | + return false; |
|
| 684 | + } |
|
| 685 | + } |
|
| 686 | 686 | |
| 687 | - /** |
|
| 688 | - * Create a new image from file or URL |
|
| 689 | - * |
|
| 690 | - * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm |
|
| 691 | - * @version 1.00 |
|
| 692 | - * @param string $fileName <p> |
|
| 693 | - * Path to the BMP image. |
|
| 694 | - * </p> |
|
| 695 | - * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors. |
|
| 696 | - */ |
|
| 697 | - private function imagecreatefrombmp($fileName) { |
|
| 698 | - if (!($fh = fopen($fileName, 'rb'))) { |
|
| 699 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 700 | - return false; |
|
| 701 | - } |
|
| 702 | - // read file header |
|
| 703 | - $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14)); |
|
| 704 | - // check for bitmap |
|
| 705 | - if ($meta['type'] != 19778) { |
|
| 706 | - fclose($fh); |
|
| 707 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 708 | - return false; |
|
| 709 | - } |
|
| 710 | - // read image header |
|
| 711 | - $meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40)); |
|
| 712 | - // read additional 16bit header |
|
| 713 | - if ($meta['bits'] == 16) { |
|
| 714 | - $meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12)); |
|
| 715 | - } |
|
| 716 | - // set bytes and padding |
|
| 717 | - $meta['bytes'] = $meta['bits'] / 8; |
|
| 718 | - $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call |
|
| 719 | - $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4))); |
|
| 720 | - if ($meta['decal'] == 4) { |
|
| 721 | - $meta['decal'] = 0; |
|
| 722 | - } |
|
| 723 | - // obtain imagesize |
|
| 724 | - if ($meta['imagesize'] < 1) { |
|
| 725 | - $meta['imagesize'] = $meta['filesize'] - $meta['offset']; |
|
| 726 | - // in rare cases filesize is equal to offset so we need to read physical size |
|
| 727 | - if ($meta['imagesize'] < 1) { |
|
| 728 | - $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
|
| 729 | - if ($meta['imagesize'] < 1) { |
|
| 730 | - fclose($fh); |
|
| 731 | - $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 732 | - return false; |
|
| 733 | - } |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - // calculate colors |
|
| 737 | - $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors']; |
|
| 738 | - // read color palette |
|
| 739 | - $palette = array(); |
|
| 740 | - if ($meta['bits'] < 16) { |
|
| 741 | - $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | - // in rare cases the color value is signed |
|
| 743 | - if ($palette[1] < 0) { |
|
| 744 | - foreach ($palette as $i => $color) { |
|
| 745 | - $palette[$i] = $color + 16777216; |
|
| 746 | - } |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - // create gd image |
|
| 750 | - $im = imagecreatetruecolor($meta['width'], $meta['height']); |
|
| 751 | - if ($im == false) { |
|
| 752 | - fclose($fh); |
|
| 753 | - $this->logger->warning( |
|
| 754 | - 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 755 | - array('app' => 'core')); |
|
| 756 | - return false; |
|
| 757 | - } |
|
| 687 | + /** |
|
| 688 | + * Create a new image from file or URL |
|
| 689 | + * |
|
| 690 | + * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm |
|
| 691 | + * @version 1.00 |
|
| 692 | + * @param string $fileName <p> |
|
| 693 | + * Path to the BMP image. |
|
| 694 | + * </p> |
|
| 695 | + * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors. |
|
| 696 | + */ |
|
| 697 | + private function imagecreatefrombmp($fileName) { |
|
| 698 | + if (!($fh = fopen($fileName, 'rb'))) { |
|
| 699 | + $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 700 | + return false; |
|
| 701 | + } |
|
| 702 | + // read file header |
|
| 703 | + $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14)); |
|
| 704 | + // check for bitmap |
|
| 705 | + if ($meta['type'] != 19778) { |
|
| 706 | + fclose($fh); |
|
| 707 | + $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 708 | + return false; |
|
| 709 | + } |
|
| 710 | + // read image header |
|
| 711 | + $meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40)); |
|
| 712 | + // read additional 16bit header |
|
| 713 | + if ($meta['bits'] == 16) { |
|
| 714 | + $meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12)); |
|
| 715 | + } |
|
| 716 | + // set bytes and padding |
|
| 717 | + $meta['bytes'] = $meta['bits'] / 8; |
|
| 718 | + $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call |
|
| 719 | + $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4))); |
|
| 720 | + if ($meta['decal'] == 4) { |
|
| 721 | + $meta['decal'] = 0; |
|
| 722 | + } |
|
| 723 | + // obtain imagesize |
|
| 724 | + if ($meta['imagesize'] < 1) { |
|
| 725 | + $meta['imagesize'] = $meta['filesize'] - $meta['offset']; |
|
| 726 | + // in rare cases filesize is equal to offset so we need to read physical size |
|
| 727 | + if ($meta['imagesize'] < 1) { |
|
| 728 | + $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
|
| 729 | + if ($meta['imagesize'] < 1) { |
|
| 730 | + fclose($fh); |
|
| 731 | + $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 732 | + return false; |
|
| 733 | + } |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + // calculate colors |
|
| 737 | + $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors']; |
|
| 738 | + // read color palette |
|
| 739 | + $palette = array(); |
|
| 740 | + if ($meta['bits'] < 16) { |
|
| 741 | + $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | + // in rare cases the color value is signed |
|
| 743 | + if ($palette[1] < 0) { |
|
| 744 | + foreach ($palette as $i => $color) { |
|
| 745 | + $palette[$i] = $color + 16777216; |
|
| 746 | + } |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + // create gd image |
|
| 750 | + $im = imagecreatetruecolor($meta['width'], $meta['height']); |
|
| 751 | + if ($im == false) { |
|
| 752 | + fclose($fh); |
|
| 753 | + $this->logger->warning( |
|
| 754 | + 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 755 | + array('app' => 'core')); |
|
| 756 | + return false; |
|
| 757 | + } |
|
| 758 | 758 | |
| 759 | - $data = fread($fh, $meta['imagesize']); |
|
| 760 | - $p = 0; |
|
| 761 | - $vide = chr(0); |
|
| 762 | - $y = $meta['height'] - 1; |
|
| 763 | - $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 764 | - // loop through the image data beginning with the lower left corner |
|
| 765 | - while ($y >= 0) { |
|
| 766 | - $x = 0; |
|
| 767 | - while ($x < $meta['width']) { |
|
| 768 | - switch ($meta['bits']) { |
|
| 769 | - case 32: |
|
| 770 | - case 24: |
|
| 771 | - if (!($part = substr($data, $p, 3))) { |
|
| 772 | - $this->logger->warning($error, array('app' => 'core')); |
|
| 773 | - return $im; |
|
| 774 | - } |
|
| 775 | - $color = @unpack('V', $part . $vide); |
|
| 776 | - break; |
|
| 777 | - case 16: |
|
| 778 | - if (!($part = substr($data, $p, 2))) { |
|
| 779 | - fclose($fh); |
|
| 780 | - $this->logger->warning($error, array('app' => 'core')); |
|
| 781 | - return $im; |
|
| 782 | - } |
|
| 783 | - $color = @unpack('v', $part); |
|
| 784 | - $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
|
| 785 | - break; |
|
| 786 | - case 8: |
|
| 787 | - $color = @unpack('n', $vide . substr($data, $p, 1)); |
|
| 788 | - $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 789 | - break; |
|
| 790 | - case 4: |
|
| 791 | - $color = @unpack('n', $vide . substr($data, floor($p), 1)); |
|
| 792 | - $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
|
| 793 | - $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 794 | - break; |
|
| 795 | - case 1: |
|
| 796 | - $color = @unpack('n', $vide . substr($data, floor($p), 1)); |
|
| 797 | - switch (($p * 8) % 8) { |
|
| 798 | - case 0: |
|
| 799 | - $color[1] = $color[1] >> 7; |
|
| 800 | - break; |
|
| 801 | - case 1: |
|
| 802 | - $color[1] = ($color[1] & 0x40) >> 6; |
|
| 803 | - break; |
|
| 804 | - case 2: |
|
| 805 | - $color[1] = ($color[1] & 0x20) >> 5; |
|
| 806 | - break; |
|
| 807 | - case 3: |
|
| 808 | - $color[1] = ($color[1] & 0x10) >> 4; |
|
| 809 | - break; |
|
| 810 | - case 4: |
|
| 811 | - $color[1] = ($color[1] & 0x8) >> 3; |
|
| 812 | - break; |
|
| 813 | - case 5: |
|
| 814 | - $color[1] = ($color[1] & 0x4) >> 2; |
|
| 815 | - break; |
|
| 816 | - case 6: |
|
| 817 | - $color[1] = ($color[1] & 0x2) >> 1; |
|
| 818 | - break; |
|
| 819 | - case 7: |
|
| 820 | - $color[1] = ($color[1] & 0x1); |
|
| 821 | - break; |
|
| 822 | - } |
|
| 823 | - $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 824 | - break; |
|
| 825 | - default: |
|
| 826 | - fclose($fh); |
|
| 827 | - $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | - return false; |
|
| 829 | - } |
|
| 830 | - imagesetpixel($im, $x, $y, $color[1]); |
|
| 831 | - $x++; |
|
| 832 | - $p += $meta['bytes']; |
|
| 833 | - } |
|
| 834 | - $y--; |
|
| 835 | - $p += $meta['decal']; |
|
| 836 | - } |
|
| 837 | - fclose($fh); |
|
| 838 | - return $im; |
|
| 839 | - } |
|
| 759 | + $data = fread($fh, $meta['imagesize']); |
|
| 760 | + $p = 0; |
|
| 761 | + $vide = chr(0); |
|
| 762 | + $y = $meta['height'] - 1; |
|
| 763 | + $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 764 | + // loop through the image data beginning with the lower left corner |
|
| 765 | + while ($y >= 0) { |
|
| 766 | + $x = 0; |
|
| 767 | + while ($x < $meta['width']) { |
|
| 768 | + switch ($meta['bits']) { |
|
| 769 | + case 32: |
|
| 770 | + case 24: |
|
| 771 | + if (!($part = substr($data, $p, 3))) { |
|
| 772 | + $this->logger->warning($error, array('app' => 'core')); |
|
| 773 | + return $im; |
|
| 774 | + } |
|
| 775 | + $color = @unpack('V', $part . $vide); |
|
| 776 | + break; |
|
| 777 | + case 16: |
|
| 778 | + if (!($part = substr($data, $p, 2))) { |
|
| 779 | + fclose($fh); |
|
| 780 | + $this->logger->warning($error, array('app' => 'core')); |
|
| 781 | + return $im; |
|
| 782 | + } |
|
| 783 | + $color = @unpack('v', $part); |
|
| 784 | + $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
|
| 785 | + break; |
|
| 786 | + case 8: |
|
| 787 | + $color = @unpack('n', $vide . substr($data, $p, 1)); |
|
| 788 | + $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 789 | + break; |
|
| 790 | + case 4: |
|
| 791 | + $color = @unpack('n', $vide . substr($data, floor($p), 1)); |
|
| 792 | + $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
|
| 793 | + $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 794 | + break; |
|
| 795 | + case 1: |
|
| 796 | + $color = @unpack('n', $vide . substr($data, floor($p), 1)); |
|
| 797 | + switch (($p * 8) % 8) { |
|
| 798 | + case 0: |
|
| 799 | + $color[1] = $color[1] >> 7; |
|
| 800 | + break; |
|
| 801 | + case 1: |
|
| 802 | + $color[1] = ($color[1] & 0x40) >> 6; |
|
| 803 | + break; |
|
| 804 | + case 2: |
|
| 805 | + $color[1] = ($color[1] & 0x20) >> 5; |
|
| 806 | + break; |
|
| 807 | + case 3: |
|
| 808 | + $color[1] = ($color[1] & 0x10) >> 4; |
|
| 809 | + break; |
|
| 810 | + case 4: |
|
| 811 | + $color[1] = ($color[1] & 0x8) >> 3; |
|
| 812 | + break; |
|
| 813 | + case 5: |
|
| 814 | + $color[1] = ($color[1] & 0x4) >> 2; |
|
| 815 | + break; |
|
| 816 | + case 6: |
|
| 817 | + $color[1] = ($color[1] & 0x2) >> 1; |
|
| 818 | + break; |
|
| 819 | + case 7: |
|
| 820 | + $color[1] = ($color[1] & 0x1); |
|
| 821 | + break; |
|
| 822 | + } |
|
| 823 | + $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 824 | + break; |
|
| 825 | + default: |
|
| 826 | + fclose($fh); |
|
| 827 | + $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | + return false; |
|
| 829 | + } |
|
| 830 | + imagesetpixel($im, $x, $y, $color[1]); |
|
| 831 | + $x++; |
|
| 832 | + $p += $meta['bytes']; |
|
| 833 | + } |
|
| 834 | + $y--; |
|
| 835 | + $p += $meta['decal']; |
|
| 836 | + } |
|
| 837 | + fclose($fh); |
|
| 838 | + return $im; |
|
| 839 | + } |
|
| 840 | 840 | |
| 841 | - /** |
|
| 842 | - * Resizes the image preserving ratio. |
|
| 843 | - * |
|
| 844 | - * @param integer $maxSize The maximum size of either the width or height. |
|
| 845 | - * @return bool |
|
| 846 | - */ |
|
| 847 | - public function resize($maxSize) { |
|
| 848 | - if (!$this->valid()) { |
|
| 849 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 850 | - return false; |
|
| 851 | - } |
|
| 852 | - $widthOrig = imagesx($this->resource); |
|
| 853 | - $heightOrig = imagesy($this->resource); |
|
| 854 | - $ratioOrig = $widthOrig / $heightOrig; |
|
| 841 | + /** |
|
| 842 | + * Resizes the image preserving ratio. |
|
| 843 | + * |
|
| 844 | + * @param integer $maxSize The maximum size of either the width or height. |
|
| 845 | + * @return bool |
|
| 846 | + */ |
|
| 847 | + public function resize($maxSize) { |
|
| 848 | + if (!$this->valid()) { |
|
| 849 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 850 | + return false; |
|
| 851 | + } |
|
| 852 | + $widthOrig = imagesx($this->resource); |
|
| 853 | + $heightOrig = imagesy($this->resource); |
|
| 854 | + $ratioOrig = $widthOrig / $heightOrig; |
|
| 855 | 855 | |
| 856 | - if ($ratioOrig > 1) { |
|
| 857 | - $newHeight = round($maxSize / $ratioOrig); |
|
| 858 | - $newWidth = $maxSize; |
|
| 859 | - } else { |
|
| 860 | - $newWidth = round($maxSize * $ratioOrig); |
|
| 861 | - $newHeight = $maxSize; |
|
| 862 | - } |
|
| 856 | + if ($ratioOrig > 1) { |
|
| 857 | + $newHeight = round($maxSize / $ratioOrig); |
|
| 858 | + $newWidth = $maxSize; |
|
| 859 | + } else { |
|
| 860 | + $newWidth = round($maxSize * $ratioOrig); |
|
| 861 | + $newHeight = $maxSize; |
|
| 862 | + } |
|
| 863 | 863 | |
| 864 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 865 | - return true; |
|
| 866 | - } |
|
| 864 | + $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 865 | + return true; |
|
| 866 | + } |
|
| 867 | 867 | |
| 868 | - /** |
|
| 869 | - * @param int $width |
|
| 870 | - * @param int $height |
|
| 871 | - * @return bool |
|
| 872 | - */ |
|
| 873 | - public function preciseResize(int $width, int $height): bool { |
|
| 874 | - if (!$this->valid()) { |
|
| 875 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 876 | - return false; |
|
| 877 | - } |
|
| 878 | - $widthOrig = imagesx($this->resource); |
|
| 879 | - $heightOrig = imagesy($this->resource); |
|
| 880 | - $process = imagecreatetruecolor($width, $height); |
|
| 868 | + /** |
|
| 869 | + * @param int $width |
|
| 870 | + * @param int $height |
|
| 871 | + * @return bool |
|
| 872 | + */ |
|
| 873 | + public function preciseResize(int $width, int $height): bool { |
|
| 874 | + if (!$this->valid()) { |
|
| 875 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 876 | + return false; |
|
| 877 | + } |
|
| 878 | + $widthOrig = imagesx($this->resource); |
|
| 879 | + $heightOrig = imagesy($this->resource); |
|
| 880 | + $process = imagecreatetruecolor($width, $height); |
|
| 881 | 881 | |
| 882 | - if ($process == false) { |
|
| 883 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 884 | - imagedestroy($process); |
|
| 885 | - return false; |
|
| 886 | - } |
|
| 882 | + if ($process == false) { |
|
| 883 | + $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 884 | + imagedestroy($process); |
|
| 885 | + return false; |
|
| 886 | + } |
|
| 887 | 887 | |
| 888 | - // preserve transparency |
|
| 889 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 890 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 891 | - imagealphablending($process, false); |
|
| 892 | - imagesavealpha($process, true); |
|
| 893 | - } |
|
| 888 | + // preserve transparency |
|
| 889 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 890 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 891 | + imagealphablending($process, false); |
|
| 892 | + imagesavealpha($process, true); |
|
| 893 | + } |
|
| 894 | 894 | |
| 895 | - imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
|
| 896 | - if ($process == false) { |
|
| 897 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 898 | - imagedestroy($process); |
|
| 899 | - return false; |
|
| 900 | - } |
|
| 901 | - imagedestroy($this->resource); |
|
| 902 | - $this->resource = $process; |
|
| 903 | - return true; |
|
| 904 | - } |
|
| 895 | + imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
|
| 896 | + if ($process == false) { |
|
| 897 | + $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 898 | + imagedestroy($process); |
|
| 899 | + return false; |
|
| 900 | + } |
|
| 901 | + imagedestroy($this->resource); |
|
| 902 | + $this->resource = $process; |
|
| 903 | + return true; |
|
| 904 | + } |
|
| 905 | 905 | |
| 906 | - /** |
|
| 907 | - * Crops the image to the middle square. If the image is already square it just returns. |
|
| 908 | - * |
|
| 909 | - * @param int $size maximum size for the result (optional) |
|
| 910 | - * @return bool for success or failure |
|
| 911 | - */ |
|
| 912 | - public function centerCrop($size = 0) { |
|
| 913 | - if (!$this->valid()) { |
|
| 914 | - $this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core')); |
|
| 915 | - return false; |
|
| 916 | - } |
|
| 917 | - $widthOrig = imagesx($this->resource); |
|
| 918 | - $heightOrig = imagesy($this->resource); |
|
| 919 | - if ($widthOrig === $heightOrig and $size == 0) { |
|
| 920 | - return true; |
|
| 921 | - } |
|
| 922 | - $ratioOrig = $widthOrig / $heightOrig; |
|
| 923 | - $width = $height = min($widthOrig, $heightOrig); |
|
| 906 | + /** |
|
| 907 | + * Crops the image to the middle square. If the image is already square it just returns. |
|
| 908 | + * |
|
| 909 | + * @param int $size maximum size for the result (optional) |
|
| 910 | + * @return bool for success or failure |
|
| 911 | + */ |
|
| 912 | + public function centerCrop($size = 0) { |
|
| 913 | + if (!$this->valid()) { |
|
| 914 | + $this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core')); |
|
| 915 | + return false; |
|
| 916 | + } |
|
| 917 | + $widthOrig = imagesx($this->resource); |
|
| 918 | + $heightOrig = imagesy($this->resource); |
|
| 919 | + if ($widthOrig === $heightOrig and $size == 0) { |
|
| 920 | + return true; |
|
| 921 | + } |
|
| 922 | + $ratioOrig = $widthOrig / $heightOrig; |
|
| 923 | + $width = $height = min($widthOrig, $heightOrig); |
|
| 924 | 924 | |
| 925 | - if ($ratioOrig > 1) { |
|
| 926 | - $x = ($widthOrig / 2) - ($width / 2); |
|
| 927 | - $y = 0; |
|
| 928 | - } else { |
|
| 929 | - $y = ($heightOrig / 2) - ($height / 2); |
|
| 930 | - $x = 0; |
|
| 931 | - } |
|
| 932 | - if ($size > 0) { |
|
| 933 | - $targetWidth = $size; |
|
| 934 | - $targetHeight = $size; |
|
| 935 | - } else { |
|
| 936 | - $targetWidth = $width; |
|
| 937 | - $targetHeight = $height; |
|
| 938 | - } |
|
| 939 | - $process = imagecreatetruecolor($targetWidth, $targetHeight); |
|
| 940 | - if ($process == false) { |
|
| 941 | - $this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core')); |
|
| 942 | - imagedestroy($process); |
|
| 943 | - return false; |
|
| 944 | - } |
|
| 925 | + if ($ratioOrig > 1) { |
|
| 926 | + $x = ($widthOrig / 2) - ($width / 2); |
|
| 927 | + $y = 0; |
|
| 928 | + } else { |
|
| 929 | + $y = ($heightOrig / 2) - ($height / 2); |
|
| 930 | + $x = 0; |
|
| 931 | + } |
|
| 932 | + if ($size > 0) { |
|
| 933 | + $targetWidth = $size; |
|
| 934 | + $targetHeight = $size; |
|
| 935 | + } else { |
|
| 936 | + $targetWidth = $width; |
|
| 937 | + $targetHeight = $height; |
|
| 938 | + } |
|
| 939 | + $process = imagecreatetruecolor($targetWidth, $targetHeight); |
|
| 940 | + if ($process == false) { |
|
| 941 | + $this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core')); |
|
| 942 | + imagedestroy($process); |
|
| 943 | + return false; |
|
| 944 | + } |
|
| 945 | 945 | |
| 946 | - // preserve transparency |
|
| 947 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 948 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 949 | - imagealphablending($process, false); |
|
| 950 | - imagesavealpha($process, true); |
|
| 951 | - } |
|
| 946 | + // preserve transparency |
|
| 947 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 948 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 949 | + imagealphablending($process, false); |
|
| 950 | + imagesavealpha($process, true); |
|
| 951 | + } |
|
| 952 | 952 | |
| 953 | - imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
|
| 954 | - if ($process == false) { |
|
| 955 | - $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 956 | - imagedestroy($process); |
|
| 957 | - return false; |
|
| 958 | - } |
|
| 959 | - imagedestroy($this->resource); |
|
| 960 | - $this->resource = $process; |
|
| 961 | - return true; |
|
| 962 | - } |
|
| 953 | + imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
|
| 954 | + if ($process == false) { |
|
| 955 | + $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 956 | + imagedestroy($process); |
|
| 957 | + return false; |
|
| 958 | + } |
|
| 959 | + imagedestroy($this->resource); |
|
| 960 | + $this->resource = $process; |
|
| 961 | + return true; |
|
| 962 | + } |
|
| 963 | 963 | |
| 964 | - /** |
|
| 965 | - * Crops the image from point $x$y with dimension $wx$h. |
|
| 966 | - * |
|
| 967 | - * @param int $x Horizontal position |
|
| 968 | - * @param int $y Vertical position |
|
| 969 | - * @param int $w Width |
|
| 970 | - * @param int $h Height |
|
| 971 | - * @return bool for success or failure |
|
| 972 | - */ |
|
| 973 | - public function crop(int $x, int $y, int $w, int $h): bool { |
|
| 974 | - if (!$this->valid()) { |
|
| 975 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 976 | - return false; |
|
| 977 | - } |
|
| 978 | - $process = imagecreatetruecolor($w, $h); |
|
| 979 | - if ($process == false) { |
|
| 980 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 981 | - imagedestroy($process); |
|
| 982 | - return false; |
|
| 983 | - } |
|
| 964 | + /** |
|
| 965 | + * Crops the image from point $x$y with dimension $wx$h. |
|
| 966 | + * |
|
| 967 | + * @param int $x Horizontal position |
|
| 968 | + * @param int $y Vertical position |
|
| 969 | + * @param int $w Width |
|
| 970 | + * @param int $h Height |
|
| 971 | + * @return bool for success or failure |
|
| 972 | + */ |
|
| 973 | + public function crop(int $x, int $y, int $w, int $h): bool { |
|
| 974 | + if (!$this->valid()) { |
|
| 975 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 976 | + return false; |
|
| 977 | + } |
|
| 978 | + $process = imagecreatetruecolor($w, $h); |
|
| 979 | + if ($process == false) { |
|
| 980 | + $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 981 | + imagedestroy($process); |
|
| 982 | + return false; |
|
| 983 | + } |
|
| 984 | 984 | |
| 985 | - // preserve transparency |
|
| 986 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 987 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 988 | - imagealphablending($process, false); |
|
| 989 | - imagesavealpha($process, true); |
|
| 990 | - } |
|
| 985 | + // preserve transparency |
|
| 986 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 987 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 988 | + imagealphablending($process, false); |
|
| 989 | + imagesavealpha($process, true); |
|
| 990 | + } |
|
| 991 | 991 | |
| 992 | - imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
|
| 993 | - if ($process == false) { |
|
| 994 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 995 | - imagedestroy($process); |
|
| 996 | - return false; |
|
| 997 | - } |
|
| 998 | - imagedestroy($this->resource); |
|
| 999 | - $this->resource = $process; |
|
| 1000 | - return true; |
|
| 1001 | - } |
|
| 992 | + imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
|
| 993 | + if ($process == false) { |
|
| 994 | + $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 995 | + imagedestroy($process); |
|
| 996 | + return false; |
|
| 997 | + } |
|
| 998 | + imagedestroy($this->resource); |
|
| 999 | + $this->resource = $process; |
|
| 1000 | + return true; |
|
| 1001 | + } |
|
| 1002 | 1002 | |
| 1003 | - /** |
|
| 1004 | - * Resizes the image to fit within a boundary while preserving ratio. |
|
| 1005 | - * |
|
| 1006 | - * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up |
|
| 1007 | - * |
|
| 1008 | - * @param integer $maxWidth |
|
| 1009 | - * @param integer $maxHeight |
|
| 1010 | - * @return bool |
|
| 1011 | - */ |
|
| 1012 | - public function fitIn($maxWidth, $maxHeight) { |
|
| 1013 | - if (!$this->valid()) { |
|
| 1014 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1015 | - return false; |
|
| 1016 | - } |
|
| 1017 | - $widthOrig = imagesx($this->resource); |
|
| 1018 | - $heightOrig = imagesy($this->resource); |
|
| 1019 | - $ratio = $widthOrig / $heightOrig; |
|
| 1003 | + /** |
|
| 1004 | + * Resizes the image to fit within a boundary while preserving ratio. |
|
| 1005 | + * |
|
| 1006 | + * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up |
|
| 1007 | + * |
|
| 1008 | + * @param integer $maxWidth |
|
| 1009 | + * @param integer $maxHeight |
|
| 1010 | + * @return bool |
|
| 1011 | + */ |
|
| 1012 | + public function fitIn($maxWidth, $maxHeight) { |
|
| 1013 | + if (!$this->valid()) { |
|
| 1014 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1015 | + return false; |
|
| 1016 | + } |
|
| 1017 | + $widthOrig = imagesx($this->resource); |
|
| 1018 | + $heightOrig = imagesy($this->resource); |
|
| 1019 | + $ratio = $widthOrig / $heightOrig; |
|
| 1020 | 1020 | |
| 1021 | - $newWidth = min($maxWidth, $ratio * $maxHeight); |
|
| 1022 | - $newHeight = min($maxHeight, $maxWidth / $ratio); |
|
| 1021 | + $newWidth = min($maxWidth, $ratio * $maxHeight); |
|
| 1022 | + $newHeight = min($maxHeight, $maxWidth / $ratio); |
|
| 1023 | 1023 | |
| 1024 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1025 | - return true; |
|
| 1026 | - } |
|
| 1024 | + $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1025 | + return true; |
|
| 1026 | + } |
|
| 1027 | 1027 | |
| 1028 | - /** |
|
| 1029 | - * Shrinks larger images to fit within specified boundaries while preserving ratio. |
|
| 1030 | - * |
|
| 1031 | - * @param integer $maxWidth |
|
| 1032 | - * @param integer $maxHeight |
|
| 1033 | - * @return bool |
|
| 1034 | - */ |
|
| 1035 | - public function scaleDownToFit($maxWidth, $maxHeight) { |
|
| 1036 | - if (!$this->valid()) { |
|
| 1037 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1038 | - return false; |
|
| 1039 | - } |
|
| 1040 | - $widthOrig = imagesx($this->resource); |
|
| 1041 | - $heightOrig = imagesy($this->resource); |
|
| 1028 | + /** |
|
| 1029 | + * Shrinks larger images to fit within specified boundaries while preserving ratio. |
|
| 1030 | + * |
|
| 1031 | + * @param integer $maxWidth |
|
| 1032 | + * @param integer $maxHeight |
|
| 1033 | + * @return bool |
|
| 1034 | + */ |
|
| 1035 | + public function scaleDownToFit($maxWidth, $maxHeight) { |
|
| 1036 | + if (!$this->valid()) { |
|
| 1037 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1038 | + return false; |
|
| 1039 | + } |
|
| 1040 | + $widthOrig = imagesx($this->resource); |
|
| 1041 | + $heightOrig = imagesy($this->resource); |
|
| 1042 | 1042 | |
| 1043 | - if ($widthOrig > $maxWidth || $heightOrig > $maxHeight) { |
|
| 1044 | - return $this->fitIn($maxWidth, $maxHeight); |
|
| 1045 | - } |
|
| 1043 | + if ($widthOrig > $maxWidth || $heightOrig > $maxHeight) { |
|
| 1044 | + return $this->fitIn($maxWidth, $maxHeight); |
|
| 1045 | + } |
|
| 1046 | 1046 | |
| 1047 | - return false; |
|
| 1048 | - } |
|
| 1047 | + return false; |
|
| 1048 | + } |
|
| 1049 | 1049 | |
| 1050 | - /** |
|
| 1051 | - * Destroys the current image and resets the object |
|
| 1052 | - */ |
|
| 1053 | - public function destroy() { |
|
| 1054 | - if ($this->valid()) { |
|
| 1055 | - imagedestroy($this->resource); |
|
| 1056 | - } |
|
| 1057 | - $this->resource = null; |
|
| 1058 | - } |
|
| 1050 | + /** |
|
| 1051 | + * Destroys the current image and resets the object |
|
| 1052 | + */ |
|
| 1053 | + public function destroy() { |
|
| 1054 | + if ($this->valid()) { |
|
| 1055 | + imagedestroy($this->resource); |
|
| 1056 | + } |
|
| 1057 | + $this->resource = null; |
|
| 1058 | + } |
|
| 1059 | 1059 | |
| 1060 | - public function __destruct() { |
|
| 1061 | - $this->destroy(); |
|
| 1062 | - } |
|
| 1060 | + public function __destruct() { |
|
| 1061 | + $this->destroy(); |
|
| 1062 | + } |
|
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 1065 | 1065 | if (!function_exists('imagebmp')) { |
| 1066 | - /** |
|
| 1067 | - * Output a BMP image to either the browser or a file |
|
| 1068 | - * |
|
| 1069 | - * @link http://www.ugia.cn/wp-data/imagebmp.php |
|
| 1070 | - * @author legend <[email protected]> |
|
| 1071 | - * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm |
|
| 1072 | - * @author mgutt <[email protected]> |
|
| 1073 | - * @version 1.00 |
|
| 1074 | - * @param resource $im |
|
| 1075 | - * @param string $fileName [optional] <p>The path to save the file to.</p> |
|
| 1076 | - * @param int $bit [optional] <p>Bit depth, (default is 24).</p> |
|
| 1077 | - * @param int $compression [optional] |
|
| 1078 | - * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
|
| 1079 | - */ |
|
| 1080 | - function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) { |
|
| 1081 | - if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) { |
|
| 1082 | - $bit = 24; |
|
| 1083 | - } else if ($bit == 32) { |
|
| 1084 | - $bit = 24; |
|
| 1085 | - } |
|
| 1086 | - $bits = pow(2, $bit); |
|
| 1087 | - imagetruecolortopalette($im, true, $bits); |
|
| 1088 | - $width = imagesx($im); |
|
| 1089 | - $height = imagesy($im); |
|
| 1090 | - $colorsNum = imagecolorstotal($im); |
|
| 1091 | - $rgbQuad = ''; |
|
| 1092 | - if ($bit <= 8) { |
|
| 1093 | - for ($i = 0; $i < $colorsNum; $i++) { |
|
| 1094 | - $colors = imagecolorsforindex($im, $i); |
|
| 1095 | - $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1096 | - } |
|
| 1097 | - $bmpData = ''; |
|
| 1098 | - if ($compression == 0 || $bit < 8) { |
|
| 1099 | - $compression = 0; |
|
| 1100 | - $extra = ''; |
|
| 1101 | - $padding = 4 - ceil($width / (8 / $bit)) % 4; |
|
| 1102 | - if ($padding % 4 != 0) { |
|
| 1103 | - $extra = str_repeat("\0", $padding); |
|
| 1104 | - } |
|
| 1105 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1106 | - $i = 0; |
|
| 1107 | - while ($i < $width) { |
|
| 1108 | - $bin = 0; |
|
| 1109 | - $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0; |
|
| 1110 | - for ($k = 8 - $bit; $k >= $limit; $k -= $bit) { |
|
| 1111 | - $index = imagecolorat($im, $i, $j); |
|
| 1112 | - $bin |= $index << $k; |
|
| 1113 | - $i++; |
|
| 1114 | - } |
|
| 1115 | - $bmpData .= chr($bin); |
|
| 1116 | - } |
|
| 1117 | - $bmpData .= $extra; |
|
| 1118 | - } |
|
| 1119 | - } // RLE8 |
|
| 1120 | - else if ($compression == 1 && $bit == 8) { |
|
| 1121 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1122 | - $lastIndex = "\0"; |
|
| 1123 | - $sameNum = 0; |
|
| 1124 | - for ($i = 0; $i <= $width; $i++) { |
|
| 1125 | - $index = imagecolorat($im, $i, $j); |
|
| 1126 | - if ($index !== $lastIndex || $sameNum > 255) { |
|
| 1127 | - if ($sameNum != 0) { |
|
| 1128 | - $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1129 | - } |
|
| 1130 | - $lastIndex = $index; |
|
| 1131 | - $sameNum = 1; |
|
| 1132 | - } else { |
|
| 1133 | - $sameNum++; |
|
| 1134 | - } |
|
| 1135 | - } |
|
| 1136 | - $bmpData .= "\0\0"; |
|
| 1137 | - } |
|
| 1138 | - $bmpData .= "\0\1"; |
|
| 1139 | - } |
|
| 1140 | - $sizeQuad = strlen($rgbQuad); |
|
| 1141 | - $sizeData = strlen($bmpData); |
|
| 1142 | - } else { |
|
| 1143 | - $extra = ''; |
|
| 1144 | - $padding = 4 - ($width * ($bit / 8)) % 4; |
|
| 1145 | - if ($padding % 4 != 0) { |
|
| 1146 | - $extra = str_repeat("\0", $padding); |
|
| 1147 | - } |
|
| 1148 | - $bmpData = ''; |
|
| 1149 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1150 | - for ($i = 0; $i < $width; $i++) { |
|
| 1151 | - $index = imagecolorat($im, $i, $j); |
|
| 1152 | - $colors = imagecolorsforindex($im, $index); |
|
| 1153 | - if ($bit == 16) { |
|
| 1154 | - $bin = 0 << $bit; |
|
| 1155 | - $bin |= ($colors['red'] >> 3) << 10; |
|
| 1156 | - $bin |= ($colors['green'] >> 3) << 5; |
|
| 1157 | - $bin |= $colors['blue'] >> 3; |
|
| 1158 | - $bmpData .= pack("v", $bin); |
|
| 1159 | - } else { |
|
| 1160 | - $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']); |
|
| 1161 | - } |
|
| 1162 | - } |
|
| 1163 | - $bmpData .= $extra; |
|
| 1164 | - } |
|
| 1165 | - $sizeQuad = 0; |
|
| 1166 | - $sizeData = strlen($bmpData); |
|
| 1167 | - $colorsNum = 0; |
|
| 1168 | - } |
|
| 1169 | - $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1170 | - $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
|
| 1171 | - if ($fileName != '') { |
|
| 1172 | - $fp = fopen($fileName, 'wb'); |
|
| 1173 | - fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1174 | - fclose($fp); |
|
| 1175 | - return true; |
|
| 1176 | - } |
|
| 1177 | - echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1178 | - return true; |
|
| 1179 | - } |
|
| 1066 | + /** |
|
| 1067 | + * Output a BMP image to either the browser or a file |
|
| 1068 | + * |
|
| 1069 | + * @link http://www.ugia.cn/wp-data/imagebmp.php |
|
| 1070 | + * @author legend <[email protected]> |
|
| 1071 | + * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm |
|
| 1072 | + * @author mgutt <[email protected]> |
|
| 1073 | + * @version 1.00 |
|
| 1074 | + * @param resource $im |
|
| 1075 | + * @param string $fileName [optional] <p>The path to save the file to.</p> |
|
| 1076 | + * @param int $bit [optional] <p>Bit depth, (default is 24).</p> |
|
| 1077 | + * @param int $compression [optional] |
|
| 1078 | + * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
|
| 1079 | + */ |
|
| 1080 | + function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) { |
|
| 1081 | + if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) { |
|
| 1082 | + $bit = 24; |
|
| 1083 | + } else if ($bit == 32) { |
|
| 1084 | + $bit = 24; |
|
| 1085 | + } |
|
| 1086 | + $bits = pow(2, $bit); |
|
| 1087 | + imagetruecolortopalette($im, true, $bits); |
|
| 1088 | + $width = imagesx($im); |
|
| 1089 | + $height = imagesy($im); |
|
| 1090 | + $colorsNum = imagecolorstotal($im); |
|
| 1091 | + $rgbQuad = ''; |
|
| 1092 | + if ($bit <= 8) { |
|
| 1093 | + for ($i = 0; $i < $colorsNum; $i++) { |
|
| 1094 | + $colors = imagecolorsforindex($im, $i); |
|
| 1095 | + $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1096 | + } |
|
| 1097 | + $bmpData = ''; |
|
| 1098 | + if ($compression == 0 || $bit < 8) { |
|
| 1099 | + $compression = 0; |
|
| 1100 | + $extra = ''; |
|
| 1101 | + $padding = 4 - ceil($width / (8 / $bit)) % 4; |
|
| 1102 | + if ($padding % 4 != 0) { |
|
| 1103 | + $extra = str_repeat("\0", $padding); |
|
| 1104 | + } |
|
| 1105 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1106 | + $i = 0; |
|
| 1107 | + while ($i < $width) { |
|
| 1108 | + $bin = 0; |
|
| 1109 | + $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0; |
|
| 1110 | + for ($k = 8 - $bit; $k >= $limit; $k -= $bit) { |
|
| 1111 | + $index = imagecolorat($im, $i, $j); |
|
| 1112 | + $bin |= $index << $k; |
|
| 1113 | + $i++; |
|
| 1114 | + } |
|
| 1115 | + $bmpData .= chr($bin); |
|
| 1116 | + } |
|
| 1117 | + $bmpData .= $extra; |
|
| 1118 | + } |
|
| 1119 | + } // RLE8 |
|
| 1120 | + else if ($compression == 1 && $bit == 8) { |
|
| 1121 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1122 | + $lastIndex = "\0"; |
|
| 1123 | + $sameNum = 0; |
|
| 1124 | + for ($i = 0; $i <= $width; $i++) { |
|
| 1125 | + $index = imagecolorat($im, $i, $j); |
|
| 1126 | + if ($index !== $lastIndex || $sameNum > 255) { |
|
| 1127 | + if ($sameNum != 0) { |
|
| 1128 | + $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1129 | + } |
|
| 1130 | + $lastIndex = $index; |
|
| 1131 | + $sameNum = 1; |
|
| 1132 | + } else { |
|
| 1133 | + $sameNum++; |
|
| 1134 | + } |
|
| 1135 | + } |
|
| 1136 | + $bmpData .= "\0\0"; |
|
| 1137 | + } |
|
| 1138 | + $bmpData .= "\0\1"; |
|
| 1139 | + } |
|
| 1140 | + $sizeQuad = strlen($rgbQuad); |
|
| 1141 | + $sizeData = strlen($bmpData); |
|
| 1142 | + } else { |
|
| 1143 | + $extra = ''; |
|
| 1144 | + $padding = 4 - ($width * ($bit / 8)) % 4; |
|
| 1145 | + if ($padding % 4 != 0) { |
|
| 1146 | + $extra = str_repeat("\0", $padding); |
|
| 1147 | + } |
|
| 1148 | + $bmpData = ''; |
|
| 1149 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1150 | + for ($i = 0; $i < $width; $i++) { |
|
| 1151 | + $index = imagecolorat($im, $i, $j); |
|
| 1152 | + $colors = imagecolorsforindex($im, $index); |
|
| 1153 | + if ($bit == 16) { |
|
| 1154 | + $bin = 0 << $bit; |
|
| 1155 | + $bin |= ($colors['red'] >> 3) << 10; |
|
| 1156 | + $bin |= ($colors['green'] >> 3) << 5; |
|
| 1157 | + $bin |= $colors['blue'] >> 3; |
|
| 1158 | + $bmpData .= pack("v", $bin); |
|
| 1159 | + } else { |
|
| 1160 | + $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']); |
|
| 1161 | + } |
|
| 1162 | + } |
|
| 1163 | + $bmpData .= $extra; |
|
| 1164 | + } |
|
| 1165 | + $sizeQuad = 0; |
|
| 1166 | + $sizeData = strlen($bmpData); |
|
| 1167 | + $colorsNum = 0; |
|
| 1168 | + } |
|
| 1169 | + $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1170 | + $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
|
| 1171 | + if ($fileName != '') { |
|
| 1172 | + $fp = fopen($fileName, 'wb'); |
|
| 1173 | + fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1174 | + fclose($fp); |
|
| 1175 | + return true; |
|
| 1176 | + } |
|
| 1177 | + echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1178 | + return true; |
|
| 1179 | + } |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | if (!function_exists('exif_imagetype')) { |
| 1183 | - /** |
|
| 1184 | - * Workaround if exif_imagetype does not exist |
|
| 1185 | - * |
|
| 1186 | - * @link http://www.php.net/manual/en/function.exif-imagetype.php#80383 |
|
| 1187 | - * @param string $fileName |
|
| 1188 | - * @return string|boolean |
|
| 1189 | - */ |
|
| 1190 | - function exif_imagetype($fileName) { |
|
| 1191 | - if (($info = getimagesize($fileName)) !== false) { |
|
| 1192 | - return $info[2]; |
|
| 1193 | - } |
|
| 1194 | - return false; |
|
| 1195 | - } |
|
| 1183 | + /** |
|
| 1184 | + * Workaround if exif_imagetype does not exist |
|
| 1185 | + * |
|
| 1186 | + * @link http://www.php.net/manual/en/function.exif-imagetype.php#80383 |
|
| 1187 | + * @param string $fileName |
|
| 1188 | + * @return string|boolean |
|
| 1189 | + */ |
|
| 1190 | + function exif_imagetype($fileName) { |
|
| 1191 | + if (($info = getimagesize($fileName)) !== false) { |
|
| 1192 | + return $info[2]; |
|
| 1193 | + } |
|
| 1194 | + return false; |
|
| 1195 | + } |
|
| 1196 | 1196 | } |
@@ -27,73 +27,73 @@ |
||
| 27 | 27 | namespace OC\Preview; |
| 28 | 28 | |
| 29 | 29 | class TXT extends Provider { |
| 30 | - /** |
|
| 31 | - * {@inheritDoc} |
|
| 32 | - */ |
|
| 33 | - public function getMimeType() { |
|
| 34 | - return '/text\/plain/'; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * {@inheritDoc} |
|
| 39 | - */ |
|
| 40 | - public function isAvailable(\OCP\Files\FileInfo $file) { |
|
| 41 | - return $file->getSize() > 0; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * {@inheritDoc} |
|
| 46 | - */ |
|
| 47 | - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { |
|
| 48 | - $content = $fileview->fopen($path, 'r'); |
|
| 49 | - |
|
| 50 | - if ($content === false) { |
|
| 51 | - return false; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - $content = stream_get_contents($content,3000); |
|
| 55 | - |
|
| 56 | - //don't create previews of empty text files |
|
| 57 | - if(trim($content) === '') { |
|
| 58 | - return false; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $lines = preg_split("/\r\n|\n|\r/", $content); |
|
| 62 | - |
|
| 63 | - $fontSize = ($maxX) ? (int) ((5 / 32) * $maxX) : 5; //5px |
|
| 64 | - $lineSize = ceil($fontSize * 1.25); |
|
| 65 | - |
|
| 66 | - $image = imagecreate($maxX, $maxY); |
|
| 67 | - imagecolorallocate($image, 255, 255, 255); |
|
| 68 | - $textColor = imagecolorallocate($image, 0, 0, 0); |
|
| 69 | - |
|
| 70 | - $fontFile = __DIR__; |
|
| 71 | - $fontFile .= '/../../../core'; |
|
| 72 | - $fontFile .= '/fonts/OpenSans-Regular.ttf'; |
|
| 73 | - |
|
| 74 | - $canUseTTF = function_exists('imagettftext'); |
|
| 75 | - |
|
| 76 | - foreach($lines as $index => $line) { |
|
| 77 | - $index = $index + 1; |
|
| 78 | - |
|
| 79 | - $x = (int) 1; |
|
| 80 | - $y = (int) ($index * $lineSize); |
|
| 81 | - |
|
| 82 | - if ($canUseTTF === true) { |
|
| 83 | - imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line); |
|
| 84 | - } else { |
|
| 85 | - $y -= $fontSize; |
|
| 86 | - imagestring($image, 1, $x, $y, $line, $textColor); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - if(($index * $lineSize) >= $maxY) { |
|
| 90 | - break; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $imageObject = new \OC_Image(); |
|
| 95 | - $imageObject->setResource($image); |
|
| 96 | - |
|
| 97 | - return $imageObject->valid() ? $imageObject : false; |
|
| 98 | - } |
|
| 30 | + /** |
|
| 31 | + * {@inheritDoc} |
|
| 32 | + */ |
|
| 33 | + public function getMimeType() { |
|
| 34 | + return '/text\/plain/'; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * {@inheritDoc} |
|
| 39 | + */ |
|
| 40 | + public function isAvailable(\OCP\Files\FileInfo $file) { |
|
| 41 | + return $file->getSize() > 0; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * {@inheritDoc} |
|
| 46 | + */ |
|
| 47 | + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { |
|
| 48 | + $content = $fileview->fopen($path, 'r'); |
|
| 49 | + |
|
| 50 | + if ($content === false) { |
|
| 51 | + return false; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + $content = stream_get_contents($content,3000); |
|
| 55 | + |
|
| 56 | + //don't create previews of empty text files |
|
| 57 | + if(trim($content) === '') { |
|
| 58 | + return false; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $lines = preg_split("/\r\n|\n|\r/", $content); |
|
| 62 | + |
|
| 63 | + $fontSize = ($maxX) ? (int) ((5 / 32) * $maxX) : 5; //5px |
|
| 64 | + $lineSize = ceil($fontSize * 1.25); |
|
| 65 | + |
|
| 66 | + $image = imagecreate($maxX, $maxY); |
|
| 67 | + imagecolorallocate($image, 255, 255, 255); |
|
| 68 | + $textColor = imagecolorallocate($image, 0, 0, 0); |
|
| 69 | + |
|
| 70 | + $fontFile = __DIR__; |
|
| 71 | + $fontFile .= '/../../../core'; |
|
| 72 | + $fontFile .= '/fonts/OpenSans-Regular.ttf'; |
|
| 73 | + |
|
| 74 | + $canUseTTF = function_exists('imagettftext'); |
|
| 75 | + |
|
| 76 | + foreach($lines as $index => $line) { |
|
| 77 | + $index = $index + 1; |
|
| 78 | + |
|
| 79 | + $x = (int) 1; |
|
| 80 | + $y = (int) ($index * $lineSize); |
|
| 81 | + |
|
| 82 | + if ($canUseTTF === true) { |
|
| 83 | + imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line); |
|
| 84 | + } else { |
|
| 85 | + $y -= $fontSize; |
|
| 86 | + imagestring($image, 1, $x, $y, $line, $textColor); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + if(($index * $lineSize) >= $maxY) { |
|
| 90 | + break; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $imageObject = new \OC_Image(); |
|
| 95 | + $imageObject->setResource($image); |
|
| 96 | + |
|
| 97 | + return $imageObject->valid() ? $imageObject : false; |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -39,387 +39,387 @@ |
||
| 39 | 39 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 40 | 40 | |
| 41 | 41 | class PreviewManager implements IPreview { |
| 42 | - /** @var IConfig */ |
|
| 43 | - protected $config; |
|
| 44 | - |
|
| 45 | - /** @var IRootFolder */ |
|
| 46 | - protected $rootFolder; |
|
| 47 | - |
|
| 48 | - /** @var IAppData */ |
|
| 49 | - protected $appData; |
|
| 50 | - |
|
| 51 | - /** @var EventDispatcherInterface */ |
|
| 52 | - protected $eventDispatcher; |
|
| 53 | - |
|
| 54 | - /** @var Generator */ |
|
| 55 | - private $generator; |
|
| 56 | - |
|
| 57 | - /** @var bool */ |
|
| 58 | - protected $providerListDirty = false; |
|
| 59 | - |
|
| 60 | - /** @var bool */ |
|
| 61 | - protected $registeredCoreProviders = false; |
|
| 62 | - |
|
| 63 | - /** @var array */ |
|
| 64 | - protected $providers = []; |
|
| 65 | - |
|
| 66 | - /** @var array mime type => support status */ |
|
| 67 | - protected $mimeTypeSupportMap = []; |
|
| 68 | - |
|
| 69 | - /** @var array */ |
|
| 70 | - protected $defaultProviders; |
|
| 71 | - |
|
| 72 | - /** @var string */ |
|
| 73 | - protected $userId; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * PreviewManager constructor. |
|
| 77 | - * |
|
| 78 | - * @param IConfig $config |
|
| 79 | - * @param IRootFolder $rootFolder |
|
| 80 | - * @param IAppData $appData |
|
| 81 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 82 | - * @param string $userId |
|
| 83 | - */ |
|
| 84 | - public function __construct(IConfig $config, |
|
| 85 | - IRootFolder $rootFolder, |
|
| 86 | - IAppData $appData, |
|
| 87 | - EventDispatcherInterface $eventDispatcher, |
|
| 88 | - $userId) { |
|
| 89 | - $this->config = $config; |
|
| 90 | - $this->rootFolder = $rootFolder; |
|
| 91 | - $this->appData = $appData; |
|
| 92 | - $this->eventDispatcher = $eventDispatcher; |
|
| 93 | - $this->userId = $userId; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * In order to improve lazy loading a closure can be registered which will be |
|
| 98 | - * called in case preview providers are actually requested |
|
| 99 | - * |
|
| 100 | - * $callable has to return an instance of \OCP\Preview\IProvider |
|
| 101 | - * |
|
| 102 | - * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
|
| 103 | - * @param \Closure $callable |
|
| 104 | - * @return void |
|
| 105 | - */ |
|
| 106 | - public function registerProvider($mimeTypeRegex, \Closure $callable) { |
|
| 107 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 108 | - return; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - if (!isset($this->providers[$mimeTypeRegex])) { |
|
| 112 | - $this->providers[$mimeTypeRegex] = []; |
|
| 113 | - } |
|
| 114 | - $this->providers[$mimeTypeRegex][] = $callable; |
|
| 115 | - $this->providerListDirty = true; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get all providers |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function getProviders() { |
|
| 123 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 124 | - return []; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $this->registerCoreProviders(); |
|
| 128 | - if ($this->providerListDirty) { |
|
| 129 | - $keys = array_map('strlen', array_keys($this->providers)); |
|
| 130 | - array_multisort($keys, SORT_DESC, $this->providers); |
|
| 131 | - $this->providerListDirty = false; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $this->providers; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Does the manager have any providers |
|
| 139 | - * @return bool |
|
| 140 | - */ |
|
| 141 | - public function hasProviders() { |
|
| 142 | - $this->registerCoreProviders(); |
|
| 143 | - return !empty($this->providers); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * return a preview of a file |
|
| 148 | - * |
|
| 149 | - * @param string $file The path to the file where you want a thumbnail from |
|
| 150 | - * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 151 | - * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 152 | - * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly |
|
| 153 | - * @return \OCP\IImage |
|
| 154 | - * @deprecated 11 Use getPreview |
|
| 155 | - */ |
|
| 156 | - public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) { |
|
| 157 | - try { |
|
| 158 | - $userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent(); |
|
| 159 | - $node = $userRoot->get($file); |
|
| 160 | - if (!($file instanceof File)) { |
|
| 161 | - throw new NotFoundException(); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $preview = $this->getPreview($node, $maxX, $maxY); |
|
| 165 | - } catch (\Exception $e) { |
|
| 166 | - return new \OC_Image(); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $previewImage = new \OC_Image(); |
|
| 170 | - $previewImage->loadFromData($preview->getContent()); |
|
| 171 | - return $previewImage; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Returns a preview of a file |
|
| 176 | - * |
|
| 177 | - * The cache is searched first and if nothing usable was found then a preview is |
|
| 178 | - * generated by one of the providers |
|
| 179 | - * |
|
| 180 | - * @param File $file |
|
| 181 | - * @param int $width |
|
| 182 | - * @param int $height |
|
| 183 | - * @param bool $crop |
|
| 184 | - * @param string $mode |
|
| 185 | - * @param string $mimeType |
|
| 186 | - * @return ISimpleFile |
|
| 187 | - * @throws NotFoundException |
|
| 188 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 189 | - * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
|
| 190 | - */ |
|
| 191 | - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
| 192 | - if ($this->generator === null) { |
|
| 193 | - $this->generator = new Generator( |
|
| 194 | - $this->config, |
|
| 195 | - $this, |
|
| 196 | - $this->appData, |
|
| 197 | - new GeneratorHelper( |
|
| 198 | - $this->rootFolder |
|
| 199 | - ), |
|
| 200 | - $this->eventDispatcher |
|
| 201 | - ); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * returns true if the passed mime type is supported |
|
| 209 | - * |
|
| 210 | - * @param string $mimeType |
|
| 211 | - * @return boolean |
|
| 212 | - */ |
|
| 213 | - public function isMimeSupported($mimeType = '*') { |
|
| 214 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 215 | - return false; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
| 219 | - return $this->mimeTypeSupportMap[$mimeType]; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $this->registerCoreProviders(); |
|
| 223 | - $providerMimeTypes = array_keys($this->providers); |
|
| 224 | - foreach ($providerMimeTypes as $supportedMimeType) { |
|
| 225 | - if (preg_match($supportedMimeType, $mimeType)) { |
|
| 226 | - $this->mimeTypeSupportMap[$mimeType] = true; |
|
| 227 | - return true; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - $this->mimeTypeSupportMap[$mimeType] = false; |
|
| 231 | - return false; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Check if a preview can be generated for a file |
|
| 236 | - * |
|
| 237 | - * @param \OCP\Files\FileInfo $file |
|
| 238 | - * @return bool |
|
| 239 | - */ |
|
| 240 | - public function isAvailable(\OCP\Files\FileInfo $file) { |
|
| 241 | - if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 242 | - return false; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - $this->registerCoreProviders(); |
|
| 246 | - if (!$this->isMimeSupported($file->getMimetype())) { |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $mount = $file->getMountPoint(); |
|
| 251 | - if ($mount and !$mount->getOption('previews', true)){ |
|
| 252 | - return false; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - foreach ($this->providers as $supportedMimeType => $providers) { |
|
| 256 | - if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
| 257 | - foreach ($providers as $closure) { |
|
| 258 | - $provider = $closure(); |
|
| 259 | - if (!($provider instanceof IProvider)) { |
|
| 260 | - continue; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** @var $provider IProvider */ |
|
| 264 | - if ($provider->isAvailable($file)) { |
|
| 265 | - return true; |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - return false; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * List of enabled default providers |
|
| 275 | - * |
|
| 276 | - * The following providers are enabled by default: |
|
| 277 | - * - OC\Preview\PNG |
|
| 278 | - * - OC\Preview\JPEG |
|
| 279 | - * - OC\Preview\GIF |
|
| 280 | - * - OC\Preview\BMP |
|
| 281 | - * - OC\Preview\XBitmap |
|
| 282 | - * - OC\Preview\MarkDown |
|
| 283 | - * - OC\Preview\MP3 |
|
| 284 | - * - OC\Preview\TXT |
|
| 285 | - * |
|
| 286 | - * The following providers are disabled by default due to performance or privacy concerns: |
|
| 287 | - * - OC\Preview\Font |
|
| 288 | - * - OC\Preview\Illustrator |
|
| 289 | - * - OC\Preview\Movie |
|
| 290 | - * - OC\Preview\MSOfficeDoc |
|
| 291 | - * - OC\Preview\MSOffice2003 |
|
| 292 | - * - OC\Preview\MSOffice2007 |
|
| 293 | - * - OC\Preview\OpenDocument |
|
| 294 | - * - OC\Preview\PDF |
|
| 295 | - * - OC\Preview\Photoshop |
|
| 296 | - * - OC\Preview\Postscript |
|
| 297 | - * - OC\Preview\StarOffice |
|
| 298 | - * - OC\Preview\SVG |
|
| 299 | - * - OC\Preview\TIFF |
|
| 300 | - * |
|
| 301 | - * @return array |
|
| 302 | - */ |
|
| 303 | - protected function getEnabledDefaultProvider() { |
|
| 304 | - if ($this->defaultProviders !== null) { |
|
| 305 | - return $this->defaultProviders; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - $imageProviders = [ |
|
| 309 | - 'OC\Preview\PNG', |
|
| 310 | - 'OC\Preview\JPEG', |
|
| 311 | - 'OC\Preview\GIF', |
|
| 312 | - 'OC\Preview\BMP', |
|
| 313 | - 'OC\Preview\XBitmap' |
|
| 314 | - ]; |
|
| 315 | - |
|
| 316 | - $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
| 317 | - 'OC\Preview\MarkDown', |
|
| 318 | - 'OC\Preview\MP3', |
|
| 319 | - 'OC\Preview\TXT', |
|
| 320 | - ], $imageProviders)); |
|
| 321 | - |
|
| 322 | - if (in_array('OC\Preview\Image', $this->defaultProviders)) { |
|
| 323 | - $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
| 324 | - } |
|
| 325 | - $this->defaultProviders = array_unique($this->defaultProviders); |
|
| 326 | - return $this->defaultProviders; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Register the default providers (if enabled) |
|
| 331 | - * |
|
| 332 | - * @param string $class |
|
| 333 | - * @param string $mimeType |
|
| 334 | - */ |
|
| 335 | - protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
| 336 | - if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
| 337 | - $this->registerProvider($mimeType, function () use ($class, $options) { |
|
| 338 | - return new $class($options); |
|
| 339 | - }); |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Register the default providers (if enabled) |
|
| 345 | - */ |
|
| 346 | - protected function registerCoreProviders() { |
|
| 347 | - if ($this->registeredCoreProviders) { |
|
| 348 | - return; |
|
| 349 | - } |
|
| 350 | - $this->registeredCoreProviders = true; |
|
| 351 | - |
|
| 352 | - $this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/'); |
|
| 353 | - $this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/'); |
|
| 354 | - $this->registerCoreProvider('OC\Preview\PNG', '/image\/png/'); |
|
| 355 | - $this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/'); |
|
| 356 | - $this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/'); |
|
| 357 | - $this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/'); |
|
| 358 | - $this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/'); |
|
| 359 | - $this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/'); |
|
| 360 | - |
|
| 361 | - // SVG, Office and Bitmap require imagick |
|
| 362 | - if (extension_loaded('imagick')) { |
|
| 363 | - $checkImagick = new \Imagick(); |
|
| 364 | - |
|
| 365 | - $imagickProviders = [ |
|
| 366 | - 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'], |
|
| 367 | - 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'], |
|
| 368 | - 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'], |
|
| 369 | - 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'], |
|
| 370 | - 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'], |
|
| 371 | - 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'], |
|
| 372 | - 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'], |
|
| 373 | - ]; |
|
| 374 | - |
|
| 375 | - foreach ($imagickProviders as $queryFormat => $provider) { |
|
| 376 | - $class = $provider['class']; |
|
| 377 | - if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
| 378 | - continue; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
| 382 | - $this->registerCoreProvider($class, $provider['mimetype']); |
|
| 383 | - } |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
| 387 | - if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
| 388 | - $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
| 389 | - |
|
| 390 | - if (!$officeFound) { |
|
| 391 | - //let's see if there is libreoffice or openoffice on this machine |
|
| 392 | - $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
| 393 | - $officeFound = !empty($whichLibreOffice); |
|
| 394 | - if (!$officeFound) { |
|
| 395 | - $whichOpenOffice = shell_exec('command -v openoffice'); |
|
| 396 | - $officeFound = !empty($whichOpenOffice); |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - if ($officeFound) { |
|
| 401 | - $this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/'); |
|
| 402 | - $this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/'); |
|
| 403 | - $this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/'); |
|
| 404 | - $this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/'); |
|
| 405 | - $this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/'); |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - // Video requires avconv or ffmpeg |
|
| 412 | - if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider())) { |
|
| 413 | - $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
| 414 | - $ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
| 415 | - |
|
| 416 | - if ($avconvBinary || $ffmpegBinary) { |
|
| 417 | - // FIXME // a bit hacky but didn't want to use subclasses |
|
| 418 | - \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
| 419 | - \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
| 420 | - |
|
| 421 | - $this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/'); |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - } |
|
| 42 | + /** @var IConfig */ |
|
| 43 | + protected $config; |
|
| 44 | + |
|
| 45 | + /** @var IRootFolder */ |
|
| 46 | + protected $rootFolder; |
|
| 47 | + |
|
| 48 | + /** @var IAppData */ |
|
| 49 | + protected $appData; |
|
| 50 | + |
|
| 51 | + /** @var EventDispatcherInterface */ |
|
| 52 | + protected $eventDispatcher; |
|
| 53 | + |
|
| 54 | + /** @var Generator */ |
|
| 55 | + private $generator; |
|
| 56 | + |
|
| 57 | + /** @var bool */ |
|
| 58 | + protected $providerListDirty = false; |
|
| 59 | + |
|
| 60 | + /** @var bool */ |
|
| 61 | + protected $registeredCoreProviders = false; |
|
| 62 | + |
|
| 63 | + /** @var array */ |
|
| 64 | + protected $providers = []; |
|
| 65 | + |
|
| 66 | + /** @var array mime type => support status */ |
|
| 67 | + protected $mimeTypeSupportMap = []; |
|
| 68 | + |
|
| 69 | + /** @var array */ |
|
| 70 | + protected $defaultProviders; |
|
| 71 | + |
|
| 72 | + /** @var string */ |
|
| 73 | + protected $userId; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * PreviewManager constructor. |
|
| 77 | + * |
|
| 78 | + * @param IConfig $config |
|
| 79 | + * @param IRootFolder $rootFolder |
|
| 80 | + * @param IAppData $appData |
|
| 81 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 82 | + * @param string $userId |
|
| 83 | + */ |
|
| 84 | + public function __construct(IConfig $config, |
|
| 85 | + IRootFolder $rootFolder, |
|
| 86 | + IAppData $appData, |
|
| 87 | + EventDispatcherInterface $eventDispatcher, |
|
| 88 | + $userId) { |
|
| 89 | + $this->config = $config; |
|
| 90 | + $this->rootFolder = $rootFolder; |
|
| 91 | + $this->appData = $appData; |
|
| 92 | + $this->eventDispatcher = $eventDispatcher; |
|
| 93 | + $this->userId = $userId; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * In order to improve lazy loading a closure can be registered which will be |
|
| 98 | + * called in case preview providers are actually requested |
|
| 99 | + * |
|
| 100 | + * $callable has to return an instance of \OCP\Preview\IProvider |
|
| 101 | + * |
|
| 102 | + * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider |
|
| 103 | + * @param \Closure $callable |
|
| 104 | + * @return void |
|
| 105 | + */ |
|
| 106 | + public function registerProvider($mimeTypeRegex, \Closure $callable) { |
|
| 107 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 108 | + return; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + if (!isset($this->providers[$mimeTypeRegex])) { |
|
| 112 | + $this->providers[$mimeTypeRegex] = []; |
|
| 113 | + } |
|
| 114 | + $this->providers[$mimeTypeRegex][] = $callable; |
|
| 115 | + $this->providerListDirty = true; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get all providers |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function getProviders() { |
|
| 123 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 124 | + return []; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $this->registerCoreProviders(); |
|
| 128 | + if ($this->providerListDirty) { |
|
| 129 | + $keys = array_map('strlen', array_keys($this->providers)); |
|
| 130 | + array_multisort($keys, SORT_DESC, $this->providers); |
|
| 131 | + $this->providerListDirty = false; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $this->providers; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Does the manager have any providers |
|
| 139 | + * @return bool |
|
| 140 | + */ |
|
| 141 | + public function hasProviders() { |
|
| 142 | + $this->registerCoreProviders(); |
|
| 143 | + return !empty($this->providers); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * return a preview of a file |
|
| 148 | + * |
|
| 149 | + * @param string $file The path to the file where you want a thumbnail from |
|
| 150 | + * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 151 | + * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image |
|
| 152 | + * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly |
|
| 153 | + * @return \OCP\IImage |
|
| 154 | + * @deprecated 11 Use getPreview |
|
| 155 | + */ |
|
| 156 | + public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) { |
|
| 157 | + try { |
|
| 158 | + $userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent(); |
|
| 159 | + $node = $userRoot->get($file); |
|
| 160 | + if (!($file instanceof File)) { |
|
| 161 | + throw new NotFoundException(); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $preview = $this->getPreview($node, $maxX, $maxY); |
|
| 165 | + } catch (\Exception $e) { |
|
| 166 | + return new \OC_Image(); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + $previewImage = new \OC_Image(); |
|
| 170 | + $previewImage->loadFromData($preview->getContent()); |
|
| 171 | + return $previewImage; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Returns a preview of a file |
|
| 176 | + * |
|
| 177 | + * The cache is searched first and if nothing usable was found then a preview is |
|
| 178 | + * generated by one of the providers |
|
| 179 | + * |
|
| 180 | + * @param File $file |
|
| 181 | + * @param int $width |
|
| 182 | + * @param int $height |
|
| 183 | + * @param bool $crop |
|
| 184 | + * @param string $mode |
|
| 185 | + * @param string $mimeType |
|
| 186 | + * @return ISimpleFile |
|
| 187 | + * @throws NotFoundException |
|
| 188 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 189 | + * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 |
|
| 190 | + */ |
|
| 191 | + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
| 192 | + if ($this->generator === null) { |
|
| 193 | + $this->generator = new Generator( |
|
| 194 | + $this->config, |
|
| 195 | + $this, |
|
| 196 | + $this->appData, |
|
| 197 | + new GeneratorHelper( |
|
| 198 | + $this->rootFolder |
|
| 199 | + ), |
|
| 200 | + $this->eventDispatcher |
|
| 201 | + ); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * returns true if the passed mime type is supported |
|
| 209 | + * |
|
| 210 | + * @param string $mimeType |
|
| 211 | + * @return boolean |
|
| 212 | + */ |
|
| 213 | + public function isMimeSupported($mimeType = '*') { |
|
| 214 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 215 | + return false; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
| 219 | + return $this->mimeTypeSupportMap[$mimeType]; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $this->registerCoreProviders(); |
|
| 223 | + $providerMimeTypes = array_keys($this->providers); |
|
| 224 | + foreach ($providerMimeTypes as $supportedMimeType) { |
|
| 225 | + if (preg_match($supportedMimeType, $mimeType)) { |
|
| 226 | + $this->mimeTypeSupportMap[$mimeType] = true; |
|
| 227 | + return true; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + $this->mimeTypeSupportMap[$mimeType] = false; |
|
| 231 | + return false; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Check if a preview can be generated for a file |
|
| 236 | + * |
|
| 237 | + * @param \OCP\Files\FileInfo $file |
|
| 238 | + * @return bool |
|
| 239 | + */ |
|
| 240 | + public function isAvailable(\OCP\Files\FileInfo $file) { |
|
| 241 | + if (!$this->config->getSystemValue('enable_previews', true)) { |
|
| 242 | + return false; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + $this->registerCoreProviders(); |
|
| 246 | + if (!$this->isMimeSupported($file->getMimetype())) { |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $mount = $file->getMountPoint(); |
|
| 251 | + if ($mount and !$mount->getOption('previews', true)){ |
|
| 252 | + return false; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + foreach ($this->providers as $supportedMimeType => $providers) { |
|
| 256 | + if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
| 257 | + foreach ($providers as $closure) { |
|
| 258 | + $provider = $closure(); |
|
| 259 | + if (!($provider instanceof IProvider)) { |
|
| 260 | + continue; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** @var $provider IProvider */ |
|
| 264 | + if ($provider->isAvailable($file)) { |
|
| 265 | + return true; |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + return false; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * List of enabled default providers |
|
| 275 | + * |
|
| 276 | + * The following providers are enabled by default: |
|
| 277 | + * - OC\Preview\PNG |
|
| 278 | + * - OC\Preview\JPEG |
|
| 279 | + * - OC\Preview\GIF |
|
| 280 | + * - OC\Preview\BMP |
|
| 281 | + * - OC\Preview\XBitmap |
|
| 282 | + * - OC\Preview\MarkDown |
|
| 283 | + * - OC\Preview\MP3 |
|
| 284 | + * - OC\Preview\TXT |
|
| 285 | + * |
|
| 286 | + * The following providers are disabled by default due to performance or privacy concerns: |
|
| 287 | + * - OC\Preview\Font |
|
| 288 | + * - OC\Preview\Illustrator |
|
| 289 | + * - OC\Preview\Movie |
|
| 290 | + * - OC\Preview\MSOfficeDoc |
|
| 291 | + * - OC\Preview\MSOffice2003 |
|
| 292 | + * - OC\Preview\MSOffice2007 |
|
| 293 | + * - OC\Preview\OpenDocument |
|
| 294 | + * - OC\Preview\PDF |
|
| 295 | + * - OC\Preview\Photoshop |
|
| 296 | + * - OC\Preview\Postscript |
|
| 297 | + * - OC\Preview\StarOffice |
|
| 298 | + * - OC\Preview\SVG |
|
| 299 | + * - OC\Preview\TIFF |
|
| 300 | + * |
|
| 301 | + * @return array |
|
| 302 | + */ |
|
| 303 | + protected function getEnabledDefaultProvider() { |
|
| 304 | + if ($this->defaultProviders !== null) { |
|
| 305 | + return $this->defaultProviders; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + $imageProviders = [ |
|
| 309 | + 'OC\Preview\PNG', |
|
| 310 | + 'OC\Preview\JPEG', |
|
| 311 | + 'OC\Preview\GIF', |
|
| 312 | + 'OC\Preview\BMP', |
|
| 313 | + 'OC\Preview\XBitmap' |
|
| 314 | + ]; |
|
| 315 | + |
|
| 316 | + $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
| 317 | + 'OC\Preview\MarkDown', |
|
| 318 | + 'OC\Preview\MP3', |
|
| 319 | + 'OC\Preview\TXT', |
|
| 320 | + ], $imageProviders)); |
|
| 321 | + |
|
| 322 | + if (in_array('OC\Preview\Image', $this->defaultProviders)) { |
|
| 323 | + $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
| 324 | + } |
|
| 325 | + $this->defaultProviders = array_unique($this->defaultProviders); |
|
| 326 | + return $this->defaultProviders; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Register the default providers (if enabled) |
|
| 331 | + * |
|
| 332 | + * @param string $class |
|
| 333 | + * @param string $mimeType |
|
| 334 | + */ |
|
| 335 | + protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
| 336 | + if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
| 337 | + $this->registerProvider($mimeType, function () use ($class, $options) { |
|
| 338 | + return new $class($options); |
|
| 339 | + }); |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Register the default providers (if enabled) |
|
| 345 | + */ |
|
| 346 | + protected function registerCoreProviders() { |
|
| 347 | + if ($this->registeredCoreProviders) { |
|
| 348 | + return; |
|
| 349 | + } |
|
| 350 | + $this->registeredCoreProviders = true; |
|
| 351 | + |
|
| 352 | + $this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/'); |
|
| 353 | + $this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/'); |
|
| 354 | + $this->registerCoreProvider('OC\Preview\PNG', '/image\/png/'); |
|
| 355 | + $this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/'); |
|
| 356 | + $this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/'); |
|
| 357 | + $this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/'); |
|
| 358 | + $this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/'); |
|
| 359 | + $this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/'); |
|
| 360 | + |
|
| 361 | + // SVG, Office and Bitmap require imagick |
|
| 362 | + if (extension_loaded('imagick')) { |
|
| 363 | + $checkImagick = new \Imagick(); |
|
| 364 | + |
|
| 365 | + $imagickProviders = [ |
|
| 366 | + 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'], |
|
| 367 | + 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'], |
|
| 368 | + 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'], |
|
| 369 | + 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'], |
|
| 370 | + 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'], |
|
| 371 | + 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'], |
|
| 372 | + 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'], |
|
| 373 | + ]; |
|
| 374 | + |
|
| 375 | + foreach ($imagickProviders as $queryFormat => $provider) { |
|
| 376 | + $class = $provider['class']; |
|
| 377 | + if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
| 378 | + continue; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + if (count($checkImagick->queryFormats($queryFormat)) === 1) { |
|
| 382 | + $this->registerCoreProvider($class, $provider['mimetype']); |
|
| 383 | + } |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + if (count($checkImagick->queryFormats('PDF')) === 1) { |
|
| 387 | + if (\OC_Helper::is_function_enabled('shell_exec')) { |
|
| 388 | + $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null)); |
|
| 389 | + |
|
| 390 | + if (!$officeFound) { |
|
| 391 | + //let's see if there is libreoffice or openoffice on this machine |
|
| 392 | + $whichLibreOffice = shell_exec('command -v libreoffice'); |
|
| 393 | + $officeFound = !empty($whichLibreOffice); |
|
| 394 | + if (!$officeFound) { |
|
| 395 | + $whichOpenOffice = shell_exec('command -v openoffice'); |
|
| 396 | + $officeFound = !empty($whichOpenOffice); |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + if ($officeFound) { |
|
| 401 | + $this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/'); |
|
| 402 | + $this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/'); |
|
| 403 | + $this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/'); |
|
| 404 | + $this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/'); |
|
| 405 | + $this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/'); |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + // Video requires avconv or ffmpeg |
|
| 412 | + if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider())) { |
|
| 413 | + $avconvBinary = \OC_Helper::findBinaryPath('avconv'); |
|
| 414 | + $ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg'); |
|
| 415 | + |
|
| 416 | + if ($avconvBinary || $ffmpegBinary) { |
|
| 417 | + // FIXME // a bit hacky but didn't want to use subclasses |
|
| 418 | + \OC\Preview\Movie::$avconvBinary = $avconvBinary; |
|
| 419 | + \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; |
|
| 420 | + |
|
| 421 | + $this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/'); |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | 425 | } |
@@ -46,385 +46,385 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | |
| 48 | 48 | class Avatar implements IAvatar { |
| 49 | - /** @var ISimpleFolder */ |
|
| 50 | - private $folder; |
|
| 51 | - /** @var IL10N */ |
|
| 52 | - private $l; |
|
| 53 | - /** @var User */ |
|
| 54 | - private $user; |
|
| 55 | - /** @var ILogger */ |
|
| 56 | - private $logger; |
|
| 57 | - /** @var IConfig */ |
|
| 58 | - private $config; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * constructor |
|
| 62 | - * |
|
| 63 | - * @param ISimpleFolder $folder The folder where the avatars are |
|
| 64 | - * @param IL10N $l |
|
| 65 | - * @param User $user |
|
| 66 | - * @param ILogger $logger |
|
| 67 | - * @param IConfig $config |
|
| 68 | - */ |
|
| 69 | - public function __construct(ISimpleFolder $folder, |
|
| 70 | - IL10N $l, |
|
| 71 | - $user, |
|
| 72 | - ILogger $logger, |
|
| 73 | - IConfig $config) { |
|
| 74 | - $this->folder = $folder; |
|
| 75 | - $this->l = $l; |
|
| 76 | - $this->user = $user; |
|
| 77 | - $this->logger = $logger; |
|
| 78 | - $this->config = $config; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @inheritdoc |
|
| 83 | - */ |
|
| 84 | - public function get ($size = 64) { |
|
| 85 | - try { |
|
| 86 | - $file = $this->getFile($size); |
|
| 87 | - } catch (NotFoundException $e) { |
|
| 88 | - return false; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $avatar = new OC_Image(); |
|
| 92 | - $avatar->loadFromData($file->getContent()); |
|
| 93 | - return $avatar; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Check if an avatar exists for the user |
|
| 98 | - * |
|
| 99 | - * @return bool |
|
| 100 | - */ |
|
| 101 | - public function exists() { |
|
| 102 | - |
|
| 103 | - return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * sets the users avatar |
|
| 108 | - * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
| 109 | - * @throws \Exception if the provided file is not a jpg or png image |
|
| 110 | - * @throws \Exception if the provided image is not valid |
|
| 111 | - * @throws NotSquareException if the image is not square |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - public function set ($data) { |
|
| 115 | - |
|
| 116 | - if($data instanceOf IImage) { |
|
| 117 | - $img = $data; |
|
| 118 | - $data = $img->data(); |
|
| 119 | - } else { |
|
| 120 | - $img = new OC_Image(); |
|
| 121 | - if (is_resource($data) && get_resource_type($data) === "gd") { |
|
| 122 | - $img->setResource($data); |
|
| 123 | - } elseif(is_resource($data)) { |
|
| 124 | - $img->loadFromFileHandle($data); |
|
| 125 | - } else { |
|
| 126 | - try { |
|
| 127 | - // detect if it is a path or maybe the images as string |
|
| 128 | - $result = @realpath($data); |
|
| 129 | - if ($result === false || $result === null) { |
|
| 130 | - $img->loadFromData($data); |
|
| 131 | - } else { |
|
| 132 | - $img->loadFromFile($data); |
|
| 133 | - } |
|
| 134 | - } catch (\Error $e) { |
|
| 135 | - $img->loadFromData($data); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - $type = substr($img->mimeType(), -3); |
|
| 140 | - if ($type === 'peg') { |
|
| 141 | - $type = 'jpg'; |
|
| 142 | - } |
|
| 143 | - if ($type !== 'jpg' && $type !== 'png') { |
|
| 144 | - throw new \Exception($this->l->t('Unknown filetype')); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - if (!$img->valid()) { |
|
| 148 | - throw new \Exception($this->l->t('Invalid image')); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if (!($img->height() === $img->width())) { |
|
| 152 | - throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $this->remove(); |
|
| 156 | - $file = $this->folder->newFile('avatar.'.$type); |
|
| 157 | - $file->putContent($data); |
|
| 158 | - |
|
| 159 | - try { |
|
| 160 | - $generated = $this->folder->getFile('generated'); |
|
| 161 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
| 162 | - $generated->delete(); |
|
| 163 | - } catch (NotFoundException $e) { |
|
| 164 | - // |
|
| 165 | - } |
|
| 166 | - $this->user->triggerChange('avatar', $file); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * remove the users avatar |
|
| 171 | - * @return void |
|
| 172 | - */ |
|
| 173 | - public function remove () { |
|
| 174 | - $avatars = $this->folder->getDirectoryListing(); |
|
| 175 | - |
|
| 176 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
| 177 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 178 | - |
|
| 179 | - foreach ($avatars as $avatar) { |
|
| 180 | - $avatar->delete(); |
|
| 181 | - } |
|
| 182 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 183 | - $this->user->triggerChange('avatar', ''); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * @inheritdoc |
|
| 188 | - */ |
|
| 189 | - public function getFile($size) { |
|
| 190 | - try { |
|
| 191 | - $ext = $this->getExtension(); |
|
| 192 | - } catch (NotFoundException $e) { |
|
| 193 | - $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
| 194 | - $avatar = $this->folder->newFile('avatar.png'); |
|
| 195 | - $avatar->putContent($data); |
|
| 196 | - $ext = 'png'; |
|
| 197 | - |
|
| 198 | - $this->folder->newFile('generated'); |
|
| 199 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - if ($size === -1) { |
|
| 203 | - $path = 'avatar.' . $ext; |
|
| 204 | - } else { |
|
| 205 | - $path = 'avatar.' . $size . '.' . $ext; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - try { |
|
| 209 | - $file = $this->folder->getFile($path); |
|
| 210 | - } catch (NotFoundException $e) { |
|
| 211 | - if ($size <= 0) { |
|
| 212 | - throw new NotFoundException; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - if ($this->folder->fileExists('generated')) { |
|
| 216 | - $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
| 217 | - |
|
| 218 | - } else { |
|
| 219 | - $avatar = new OC_Image(); |
|
| 220 | - /** @var ISimpleFile $file */ |
|
| 221 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
| 222 | - $avatar->loadFromData($file->getContent()); |
|
| 223 | - $avatar->resize($size); |
|
| 224 | - $data = $avatar->data(); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - try { |
|
| 228 | - $file = $this->folder->newFile($path); |
|
| 229 | - $file->putContent($data); |
|
| 230 | - } catch (NotPermittedException $e) { |
|
| 231 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 232 | - throw new NotFoundException(); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return $file; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Get the extension of the avatar. If there is no avatar throw Exception |
|
| 242 | - * |
|
| 243 | - * @return string |
|
| 244 | - * @throws NotFoundException |
|
| 245 | - */ |
|
| 246 | - private function getExtension() { |
|
| 247 | - if ($this->folder->fileExists('avatar.jpg')) { |
|
| 248 | - return 'jpg'; |
|
| 249 | - } elseif ($this->folder->fileExists('avatar.png')) { |
|
| 250 | - return 'png'; |
|
| 251 | - } |
|
| 252 | - throw new NotFoundException; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @param string $userDisplayName |
|
| 257 | - * @param int $size |
|
| 258 | - * @return string |
|
| 259 | - */ |
|
| 260 | - private function generateAvatar($userDisplayName, $size) { |
|
| 261 | - $text = strtoupper(substr($userDisplayName, 0, 1)); |
|
| 262 | - $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
| 263 | - |
|
| 264 | - $im = imagecreatetruecolor($size, $size); |
|
| 265 | - $background = imagecolorallocate($im, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]); |
|
| 266 | - $white = imagecolorallocate($im, 255, 255, 255); |
|
| 267 | - imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
| 268 | - |
|
| 269 | - $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 270 | - |
|
| 271 | - $fontSize = $size * 0.4; |
|
| 272 | - $box = imagettfbbox($fontSize, 0, $font, $text); |
|
| 273 | - |
|
| 274 | - $x = ($size - ($box[2] - $box[0])) / 2; |
|
| 275 | - $y = ($size - ($box[1] - $box[7])) / 2; |
|
| 276 | - $x += 1; |
|
| 277 | - $y -= $box[7]; |
|
| 278 | - imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
| 279 | - |
|
| 280 | - ob_start(); |
|
| 281 | - imagepng($im); |
|
| 282 | - $data = ob_get_contents(); |
|
| 283 | - ob_end_clean(); |
|
| 284 | - |
|
| 285 | - return $data; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @param int $r |
|
| 290 | - * @param int $g |
|
| 291 | - * @param int $b |
|
| 292 | - * @return double[] Array containing h s l in [0, 1] range |
|
| 293 | - */ |
|
| 294 | - private function rgbToHsl($r, $g, $b) { |
|
| 295 | - $r /= 255.0; |
|
| 296 | - $g /= 255.0; |
|
| 297 | - $b /= 255.0; |
|
| 298 | - |
|
| 299 | - $max = max($r, $g, $b); |
|
| 300 | - $min = min($r, $g, $b); |
|
| 301 | - |
|
| 302 | - |
|
| 303 | - $h = ($max + $min) / 2.0; |
|
| 304 | - $l = ($max + $min) / 2.0; |
|
| 305 | - |
|
| 306 | - if($max === $min) { |
|
| 307 | - $h = $s = 0; // Achromatic |
|
| 308 | - } else { |
|
| 309 | - $d = $max - $min; |
|
| 310 | - $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); |
|
| 311 | - switch($max) { |
|
| 312 | - case $r: |
|
| 313 | - $h = ($g - $b) / $d + ($g < $b ? 6 : 0); |
|
| 314 | - break; |
|
| 315 | - case $g: |
|
| 316 | - $h = ($b - $r) / $d + 2.0; |
|
| 317 | - break; |
|
| 318 | - case $b: |
|
| 319 | - $h = ($r - $g) / $d + 4.0; |
|
| 320 | - break; |
|
| 321 | - } |
|
| 322 | - $h /= 6.0; |
|
| 323 | - } |
|
| 324 | - return [$h, $s, $l]; |
|
| 325 | - |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * @param string $text |
|
| 330 | - * @return int[] Array containting r g b in the range [0, 255] |
|
| 331 | - */ |
|
| 332 | - private function avatarBackgroundColor($text) { |
|
| 333 | - $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
| 334 | - |
|
| 335 | - $hash = md5($hash); |
|
| 336 | - $hashChars = str_split($hash); |
|
| 337 | - |
|
| 338 | - |
|
| 339 | - // Init vars |
|
| 340 | - $result = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']; |
|
| 341 | - $rgb = [0, 0, 0]; |
|
| 342 | - $sat = 0.70; |
|
| 343 | - $lum = 0.68; |
|
| 344 | - $modulo = 16; |
|
| 345 | - |
|
| 346 | - |
|
| 347 | - // Splitting evenly the string |
|
| 348 | - foreach($hashChars as $i => $char) { |
|
| 349 | - $result[$i % $modulo] .= intval($char, 16); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - // Converting our data into a usable rgb format |
|
| 353 | - // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even |
|
| 354 | - for($count = 1; $count < $modulo; $count++) { |
|
| 355 | - $rgb[$count%3] += (int)$result[$count]; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - // Reduce values bigger than rgb requirements |
|
| 359 | - $rgb[0] %= 255; |
|
| 360 | - $rgb[1] %= 255; |
|
| 361 | - $rgb[2] %= 255; |
|
| 362 | - |
|
| 363 | - $hsl = $this->rgbToHsl($rgb[0], $rgb[1], $rgb[2]); |
|
| 364 | - |
|
| 365 | - // Classic formula to check the brightness for our eye |
|
| 366 | - // If too bright, lower the sat |
|
| 367 | - $bright = sqrt(0.299 * ($rgb[0] ** 2) + 0.587 * ($rgb[1] ** 2) + 0.114 * ($rgb[2] ** 2)); |
|
| 368 | - if ($bright >= 200) { |
|
| 369 | - $sat = 0.60; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - return $this->hslToRgb($hsl[0], $sat, $lum); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * @param double $h Hue in range [0, 1] |
|
| 377 | - * @param double $s Saturation in range [0, 1] |
|
| 378 | - * @param double $l Lightness in range [0, 1] |
|
| 379 | - * @return int[] Array containing r g b in the range [0, 255] |
|
| 380 | - */ |
|
| 381 | - private function hslToRgb($h, $s, $l){ |
|
| 382 | - $hue2rgb = function ($p, $q, $t){ |
|
| 383 | - if($t < 0) { |
|
| 384 | - $t += 1; |
|
| 385 | - } |
|
| 386 | - if($t > 1) { |
|
| 387 | - $t -= 1; |
|
| 388 | - } |
|
| 389 | - if($t < 1/6) { |
|
| 390 | - return $p + ($q - $p) * 6 * $t; |
|
| 391 | - } |
|
| 392 | - if($t < 1/2) { |
|
| 393 | - return $q; |
|
| 394 | - } |
|
| 395 | - if($t < 2/3) { |
|
| 396 | - return $p + ($q - $p) * (2/3 - $t) * 6; |
|
| 397 | - } |
|
| 398 | - return $p; |
|
| 399 | - }; |
|
| 400 | - |
|
| 401 | - if($s === 0){ |
|
| 402 | - $r = $l; |
|
| 403 | - $g = $l; |
|
| 404 | - $b = $l; // achromatic |
|
| 405 | - }else{ |
|
| 406 | - $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
|
| 407 | - $p = 2 * $l - $q; |
|
| 408 | - $r = $hue2rgb($p, $q, $h + 1/3); |
|
| 409 | - $g = $hue2rgb($p, $q, $h); |
|
| 410 | - $b = $hue2rgb($p, $q, $h - 1/3); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - return array(round($r * 255), round($g * 255), round($b * 255)); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - public function userChanged($feature, $oldValue, $newValue) { |
|
| 417 | - // We only change the avatar on display name changes |
|
| 418 | - if ($feature !== 'displayName') { |
|
| 419 | - return; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - // If the avatar is not generated (so an uploaded image) we skip this |
|
| 423 | - if (!$this->folder->fileExists('generated')) { |
|
| 424 | - return; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - $this->remove(); |
|
| 428 | - } |
|
| 49 | + /** @var ISimpleFolder */ |
|
| 50 | + private $folder; |
|
| 51 | + /** @var IL10N */ |
|
| 52 | + private $l; |
|
| 53 | + /** @var User */ |
|
| 54 | + private $user; |
|
| 55 | + /** @var ILogger */ |
|
| 56 | + private $logger; |
|
| 57 | + /** @var IConfig */ |
|
| 58 | + private $config; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * constructor |
|
| 62 | + * |
|
| 63 | + * @param ISimpleFolder $folder The folder where the avatars are |
|
| 64 | + * @param IL10N $l |
|
| 65 | + * @param User $user |
|
| 66 | + * @param ILogger $logger |
|
| 67 | + * @param IConfig $config |
|
| 68 | + */ |
|
| 69 | + public function __construct(ISimpleFolder $folder, |
|
| 70 | + IL10N $l, |
|
| 71 | + $user, |
|
| 72 | + ILogger $logger, |
|
| 73 | + IConfig $config) { |
|
| 74 | + $this->folder = $folder; |
|
| 75 | + $this->l = $l; |
|
| 76 | + $this->user = $user; |
|
| 77 | + $this->logger = $logger; |
|
| 78 | + $this->config = $config; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @inheritdoc |
|
| 83 | + */ |
|
| 84 | + public function get ($size = 64) { |
|
| 85 | + try { |
|
| 86 | + $file = $this->getFile($size); |
|
| 87 | + } catch (NotFoundException $e) { |
|
| 88 | + return false; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $avatar = new OC_Image(); |
|
| 92 | + $avatar->loadFromData($file->getContent()); |
|
| 93 | + return $avatar; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Check if an avatar exists for the user |
|
| 98 | + * |
|
| 99 | + * @return bool |
|
| 100 | + */ |
|
| 101 | + public function exists() { |
|
| 102 | + |
|
| 103 | + return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * sets the users avatar |
|
| 108 | + * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
| 109 | + * @throws \Exception if the provided file is not a jpg or png image |
|
| 110 | + * @throws \Exception if the provided image is not valid |
|
| 111 | + * @throws NotSquareException if the image is not square |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + public function set ($data) { |
|
| 115 | + |
|
| 116 | + if($data instanceOf IImage) { |
|
| 117 | + $img = $data; |
|
| 118 | + $data = $img->data(); |
|
| 119 | + } else { |
|
| 120 | + $img = new OC_Image(); |
|
| 121 | + if (is_resource($data) && get_resource_type($data) === "gd") { |
|
| 122 | + $img->setResource($data); |
|
| 123 | + } elseif(is_resource($data)) { |
|
| 124 | + $img->loadFromFileHandle($data); |
|
| 125 | + } else { |
|
| 126 | + try { |
|
| 127 | + // detect if it is a path or maybe the images as string |
|
| 128 | + $result = @realpath($data); |
|
| 129 | + if ($result === false || $result === null) { |
|
| 130 | + $img->loadFromData($data); |
|
| 131 | + } else { |
|
| 132 | + $img->loadFromFile($data); |
|
| 133 | + } |
|
| 134 | + } catch (\Error $e) { |
|
| 135 | + $img->loadFromData($data); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + $type = substr($img->mimeType(), -3); |
|
| 140 | + if ($type === 'peg') { |
|
| 141 | + $type = 'jpg'; |
|
| 142 | + } |
|
| 143 | + if ($type !== 'jpg' && $type !== 'png') { |
|
| 144 | + throw new \Exception($this->l->t('Unknown filetype')); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + if (!$img->valid()) { |
|
| 148 | + throw new \Exception($this->l->t('Invalid image')); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if (!($img->height() === $img->width())) { |
|
| 152 | + throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $this->remove(); |
|
| 156 | + $file = $this->folder->newFile('avatar.'.$type); |
|
| 157 | + $file->putContent($data); |
|
| 158 | + |
|
| 159 | + try { |
|
| 160 | + $generated = $this->folder->getFile('generated'); |
|
| 161 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
| 162 | + $generated->delete(); |
|
| 163 | + } catch (NotFoundException $e) { |
|
| 164 | + // |
|
| 165 | + } |
|
| 166 | + $this->user->triggerChange('avatar', $file); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * remove the users avatar |
|
| 171 | + * @return void |
|
| 172 | + */ |
|
| 173 | + public function remove () { |
|
| 174 | + $avatars = $this->folder->getDirectoryListing(); |
|
| 175 | + |
|
| 176 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
| 177 | + (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 178 | + |
|
| 179 | + foreach ($avatars as $avatar) { |
|
| 180 | + $avatar->delete(); |
|
| 181 | + } |
|
| 182 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 183 | + $this->user->triggerChange('avatar', ''); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * @inheritdoc |
|
| 188 | + */ |
|
| 189 | + public function getFile($size) { |
|
| 190 | + try { |
|
| 191 | + $ext = $this->getExtension(); |
|
| 192 | + } catch (NotFoundException $e) { |
|
| 193 | + $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
| 194 | + $avatar = $this->folder->newFile('avatar.png'); |
|
| 195 | + $avatar->putContent($data); |
|
| 196 | + $ext = 'png'; |
|
| 197 | + |
|
| 198 | + $this->folder->newFile('generated'); |
|
| 199 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + if ($size === -1) { |
|
| 203 | + $path = 'avatar.' . $ext; |
|
| 204 | + } else { |
|
| 205 | + $path = 'avatar.' . $size . '.' . $ext; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + try { |
|
| 209 | + $file = $this->folder->getFile($path); |
|
| 210 | + } catch (NotFoundException $e) { |
|
| 211 | + if ($size <= 0) { |
|
| 212 | + throw new NotFoundException; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + if ($this->folder->fileExists('generated')) { |
|
| 216 | + $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
| 217 | + |
|
| 218 | + } else { |
|
| 219 | + $avatar = new OC_Image(); |
|
| 220 | + /** @var ISimpleFile $file */ |
|
| 221 | + $file = $this->folder->getFile('avatar.' . $ext); |
|
| 222 | + $avatar->loadFromData($file->getContent()); |
|
| 223 | + $avatar->resize($size); |
|
| 224 | + $data = $avatar->data(); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + try { |
|
| 228 | + $file = $this->folder->newFile($path); |
|
| 229 | + $file->putContent($data); |
|
| 230 | + } catch (NotPermittedException $e) { |
|
| 231 | + $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 232 | + throw new NotFoundException(); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return $file; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Get the extension of the avatar. If there is no avatar throw Exception |
|
| 242 | + * |
|
| 243 | + * @return string |
|
| 244 | + * @throws NotFoundException |
|
| 245 | + */ |
|
| 246 | + private function getExtension() { |
|
| 247 | + if ($this->folder->fileExists('avatar.jpg')) { |
|
| 248 | + return 'jpg'; |
|
| 249 | + } elseif ($this->folder->fileExists('avatar.png')) { |
|
| 250 | + return 'png'; |
|
| 251 | + } |
|
| 252 | + throw new NotFoundException; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @param string $userDisplayName |
|
| 257 | + * @param int $size |
|
| 258 | + * @return string |
|
| 259 | + */ |
|
| 260 | + private function generateAvatar($userDisplayName, $size) { |
|
| 261 | + $text = strtoupper(substr($userDisplayName, 0, 1)); |
|
| 262 | + $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
| 263 | + |
|
| 264 | + $im = imagecreatetruecolor($size, $size); |
|
| 265 | + $background = imagecolorallocate($im, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]); |
|
| 266 | + $white = imagecolorallocate($im, 255, 255, 255); |
|
| 267 | + imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
| 268 | + |
|
| 269 | + $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 270 | + |
|
| 271 | + $fontSize = $size * 0.4; |
|
| 272 | + $box = imagettfbbox($fontSize, 0, $font, $text); |
|
| 273 | + |
|
| 274 | + $x = ($size - ($box[2] - $box[0])) / 2; |
|
| 275 | + $y = ($size - ($box[1] - $box[7])) / 2; |
|
| 276 | + $x += 1; |
|
| 277 | + $y -= $box[7]; |
|
| 278 | + imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
| 279 | + |
|
| 280 | + ob_start(); |
|
| 281 | + imagepng($im); |
|
| 282 | + $data = ob_get_contents(); |
|
| 283 | + ob_end_clean(); |
|
| 284 | + |
|
| 285 | + return $data; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @param int $r |
|
| 290 | + * @param int $g |
|
| 291 | + * @param int $b |
|
| 292 | + * @return double[] Array containing h s l in [0, 1] range |
|
| 293 | + */ |
|
| 294 | + private function rgbToHsl($r, $g, $b) { |
|
| 295 | + $r /= 255.0; |
|
| 296 | + $g /= 255.0; |
|
| 297 | + $b /= 255.0; |
|
| 298 | + |
|
| 299 | + $max = max($r, $g, $b); |
|
| 300 | + $min = min($r, $g, $b); |
|
| 301 | + |
|
| 302 | + |
|
| 303 | + $h = ($max + $min) / 2.0; |
|
| 304 | + $l = ($max + $min) / 2.0; |
|
| 305 | + |
|
| 306 | + if($max === $min) { |
|
| 307 | + $h = $s = 0; // Achromatic |
|
| 308 | + } else { |
|
| 309 | + $d = $max - $min; |
|
| 310 | + $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); |
|
| 311 | + switch($max) { |
|
| 312 | + case $r: |
|
| 313 | + $h = ($g - $b) / $d + ($g < $b ? 6 : 0); |
|
| 314 | + break; |
|
| 315 | + case $g: |
|
| 316 | + $h = ($b - $r) / $d + 2.0; |
|
| 317 | + break; |
|
| 318 | + case $b: |
|
| 319 | + $h = ($r - $g) / $d + 4.0; |
|
| 320 | + break; |
|
| 321 | + } |
|
| 322 | + $h /= 6.0; |
|
| 323 | + } |
|
| 324 | + return [$h, $s, $l]; |
|
| 325 | + |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * @param string $text |
|
| 330 | + * @return int[] Array containting r g b in the range [0, 255] |
|
| 331 | + */ |
|
| 332 | + private function avatarBackgroundColor($text) { |
|
| 333 | + $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
| 334 | + |
|
| 335 | + $hash = md5($hash); |
|
| 336 | + $hashChars = str_split($hash); |
|
| 337 | + |
|
| 338 | + |
|
| 339 | + // Init vars |
|
| 340 | + $result = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']; |
|
| 341 | + $rgb = [0, 0, 0]; |
|
| 342 | + $sat = 0.70; |
|
| 343 | + $lum = 0.68; |
|
| 344 | + $modulo = 16; |
|
| 345 | + |
|
| 346 | + |
|
| 347 | + // Splitting evenly the string |
|
| 348 | + foreach($hashChars as $i => $char) { |
|
| 349 | + $result[$i % $modulo] .= intval($char, 16); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + // Converting our data into a usable rgb format |
|
| 353 | + // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even |
|
| 354 | + for($count = 1; $count < $modulo; $count++) { |
|
| 355 | + $rgb[$count%3] += (int)$result[$count]; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + // Reduce values bigger than rgb requirements |
|
| 359 | + $rgb[0] %= 255; |
|
| 360 | + $rgb[1] %= 255; |
|
| 361 | + $rgb[2] %= 255; |
|
| 362 | + |
|
| 363 | + $hsl = $this->rgbToHsl($rgb[0], $rgb[1], $rgb[2]); |
|
| 364 | + |
|
| 365 | + // Classic formula to check the brightness for our eye |
|
| 366 | + // If too bright, lower the sat |
|
| 367 | + $bright = sqrt(0.299 * ($rgb[0] ** 2) + 0.587 * ($rgb[1] ** 2) + 0.114 * ($rgb[2] ** 2)); |
|
| 368 | + if ($bright >= 200) { |
|
| 369 | + $sat = 0.60; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + return $this->hslToRgb($hsl[0], $sat, $lum); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * @param double $h Hue in range [0, 1] |
|
| 377 | + * @param double $s Saturation in range [0, 1] |
|
| 378 | + * @param double $l Lightness in range [0, 1] |
|
| 379 | + * @return int[] Array containing r g b in the range [0, 255] |
|
| 380 | + */ |
|
| 381 | + private function hslToRgb($h, $s, $l){ |
|
| 382 | + $hue2rgb = function ($p, $q, $t){ |
|
| 383 | + if($t < 0) { |
|
| 384 | + $t += 1; |
|
| 385 | + } |
|
| 386 | + if($t > 1) { |
|
| 387 | + $t -= 1; |
|
| 388 | + } |
|
| 389 | + if($t < 1/6) { |
|
| 390 | + return $p + ($q - $p) * 6 * $t; |
|
| 391 | + } |
|
| 392 | + if($t < 1/2) { |
|
| 393 | + return $q; |
|
| 394 | + } |
|
| 395 | + if($t < 2/3) { |
|
| 396 | + return $p + ($q - $p) * (2/3 - $t) * 6; |
|
| 397 | + } |
|
| 398 | + return $p; |
|
| 399 | + }; |
|
| 400 | + |
|
| 401 | + if($s === 0){ |
|
| 402 | + $r = $l; |
|
| 403 | + $g = $l; |
|
| 404 | + $b = $l; // achromatic |
|
| 405 | + }else{ |
|
| 406 | + $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
|
| 407 | + $p = 2 * $l - $q; |
|
| 408 | + $r = $hue2rgb($p, $q, $h + 1/3); |
|
| 409 | + $g = $hue2rgb($p, $q, $h); |
|
| 410 | + $b = $hue2rgb($p, $q, $h - 1/3); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + return array(round($r * 255), round($g * 255), round($b * 255)); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + public function userChanged($feature, $oldValue, $newValue) { |
|
| 417 | + // We only change the avatar on display name changes |
|
| 418 | + if ($feature !== 'displayName') { |
|
| 419 | + return; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + // If the avatar is not generated (so an uploaded image) we skip this |
|
| 423 | + if (!$this->folder->fileExists('generated')) { |
|
| 424 | + return; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + $this->remove(); |
|
| 428 | + } |
|
| 429 | 429 | |
| 430 | 430 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | /** |
| 82 | 82 | * @inheritdoc |
| 83 | 83 | */ |
| 84 | - public function get ($size = 64) { |
|
| 84 | + public function get($size = 64) { |
|
| 85 | 85 | try { |
| 86 | 86 | $file = $this->getFile($size); |
| 87 | 87 | } catch (NotFoundException $e) { |
@@ -111,16 +111,16 @@ discard block |
||
| 111 | 111 | * @throws NotSquareException if the image is not square |
| 112 | 112 | * @return void |
| 113 | 113 | */ |
| 114 | - public function set ($data) { |
|
| 114 | + public function set($data) { |
|
| 115 | 115 | |
| 116 | - if($data instanceOf IImage) { |
|
| 116 | + if ($data instanceOf IImage) { |
|
| 117 | 117 | $img = $data; |
| 118 | 118 | $data = $img->data(); |
| 119 | 119 | } else { |
| 120 | 120 | $img = new OC_Image(); |
| 121 | 121 | if (is_resource($data) && get_resource_type($data) === "gd") { |
| 122 | 122 | $img->setResource($data); |
| 123 | - } elseif(is_resource($data)) { |
|
| 123 | + } elseif (is_resource($data)) { |
|
| 124 | 124 | $img->loadFromFileHandle($data); |
| 125 | 125 | } else { |
| 126 | 126 | try { |
@@ -170,11 +170,11 @@ discard block |
||
| 170 | 170 | * remove the users avatar |
| 171 | 171 | * @return void |
| 172 | 172 | */ |
| 173 | - public function remove () { |
|
| 173 | + public function remove() { |
|
| 174 | 174 | $avatars = $this->folder->getDirectoryListing(); |
| 175 | 175 | |
| 176 | 176 | $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
| 177 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 177 | + (int) $this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 178 | 178 | |
| 179 | 179 | foreach ($avatars as $avatar) { |
| 180 | 180 | $avatar->delete(); |
@@ -200,9 +200,9 @@ discard block |
||
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | if ($size === -1) { |
| 203 | - $path = 'avatar.' . $ext; |
|
| 203 | + $path = 'avatar.'.$ext; |
|
| 204 | 204 | } else { |
| 205 | - $path = 'avatar.' . $size . '.' . $ext; |
|
| 205 | + $path = 'avatar.'.$size.'.'.$ext; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | try { |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | } else { |
| 219 | 219 | $avatar = new OC_Image(); |
| 220 | 220 | /** @var ISimpleFile $file */ |
| 221 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
| 221 | + $file = $this->folder->getFile('avatar.'.$ext); |
|
| 222 | 222 | $avatar->loadFromData($file->getContent()); |
| 223 | 223 | $avatar->resize($size); |
| 224 | 224 | $data = $avatar->data(); |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | $file = $this->folder->newFile($path); |
| 229 | 229 | $file->putContent($data); |
| 230 | 230 | } catch (NotPermittedException $e) { |
| 231 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 231 | + $this->logger->error('Failed to save avatar for '.$this->user->getUID()); |
|
| 232 | 232 | throw new NotFoundException(); |
| 233 | 233 | } |
| 234 | 234 | |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | $white = imagecolorallocate($im, 255, 255, 255); |
| 267 | 267 | imagefilledrectangle($im, 0, 0, $size, $size, $background); |
| 268 | 268 | |
| 269 | - $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 269 | + $font = __DIR__.'/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 270 | 270 | |
| 271 | 271 | $fontSize = $size * 0.4; |
| 272 | 272 | $box = imagettfbbox($fontSize, 0, $font, $text); |
@@ -303,12 +303,12 @@ discard block |
||
| 303 | 303 | $h = ($max + $min) / 2.0; |
| 304 | 304 | $l = ($max + $min) / 2.0; |
| 305 | 305 | |
| 306 | - if($max === $min) { |
|
| 306 | + if ($max === $min) { |
|
| 307 | 307 | $h = $s = 0; // Achromatic |
| 308 | 308 | } else { |
| 309 | 309 | $d = $max - $min; |
| 310 | 310 | $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); |
| 311 | - switch($max) { |
|
| 311 | + switch ($max) { |
|
| 312 | 312 | case $r: |
| 313 | 313 | $h = ($g - $b) / $d + ($g < $b ? 6 : 0); |
| 314 | 314 | break; |
@@ -345,14 +345,14 @@ discard block |
||
| 345 | 345 | |
| 346 | 346 | |
| 347 | 347 | // Splitting evenly the string |
| 348 | - foreach($hashChars as $i => $char) { |
|
| 348 | + foreach ($hashChars as $i => $char) { |
|
| 349 | 349 | $result[$i % $modulo] .= intval($char, 16); |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | // Converting our data into a usable rgb format |
| 353 | 353 | // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even |
| 354 | - for($count = 1; $count < $modulo; $count++) { |
|
| 355 | - $rgb[$count%3] += (int)$result[$count]; |
|
| 354 | + for ($count = 1; $count < $modulo; $count++) { |
|
| 355 | + $rgb[$count % 3] += (int) $result[$count]; |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | // Reduce values bigger than rgb requirements |
@@ -378,36 +378,36 @@ discard block |
||
| 378 | 378 | * @param double $l Lightness in range [0, 1] |
| 379 | 379 | * @return int[] Array containing r g b in the range [0, 255] |
| 380 | 380 | */ |
| 381 | - private function hslToRgb($h, $s, $l){ |
|
| 382 | - $hue2rgb = function ($p, $q, $t){ |
|
| 383 | - if($t < 0) { |
|
| 381 | + private function hslToRgb($h, $s, $l) { |
|
| 382 | + $hue2rgb = function($p, $q, $t) { |
|
| 383 | + if ($t < 0) { |
|
| 384 | 384 | $t += 1; |
| 385 | 385 | } |
| 386 | - if($t > 1) { |
|
| 386 | + if ($t > 1) { |
|
| 387 | 387 | $t -= 1; |
| 388 | 388 | } |
| 389 | - if($t < 1/6) { |
|
| 389 | + if ($t < 1 / 6) { |
|
| 390 | 390 | return $p + ($q - $p) * 6 * $t; |
| 391 | 391 | } |
| 392 | - if($t < 1/2) { |
|
| 392 | + if ($t < 1 / 2) { |
|
| 393 | 393 | return $q; |
| 394 | 394 | } |
| 395 | - if($t < 2/3) { |
|
| 396 | - return $p + ($q - $p) * (2/3 - $t) * 6; |
|
| 395 | + if ($t < 2 / 3) { |
|
| 396 | + return $p + ($q - $p) * (2 / 3 - $t) * 6; |
|
| 397 | 397 | } |
| 398 | 398 | return $p; |
| 399 | 399 | }; |
| 400 | 400 | |
| 401 | - if($s === 0){ |
|
| 401 | + if ($s === 0) { |
|
| 402 | 402 | $r = $l; |
| 403 | 403 | $g = $l; |
| 404 | 404 | $b = $l; // achromatic |
| 405 | - }else{ |
|
| 405 | + } else { |
|
| 406 | 406 | $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
| 407 | 407 | $p = 2 * $l - $q; |
| 408 | - $r = $hue2rgb($p, $q, $h + 1/3); |
|
| 408 | + $r = $hue2rgb($p, $q, $h + 1 / 3); |
|
| 409 | 409 | $g = $hue2rgb($p, $q, $h); |
| 410 | - $b = $hue2rgb($p, $q, $h - 1/3); |
|
| 410 | + $b = $hue2rgb($p, $q, $h - 1 / 3); |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | return array(round($r * 255), round($g * 255), round($b * 255)); |
@@ -50,294 +50,294 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | class AvatarController extends Controller { |
| 52 | 52 | |
| 53 | - /** @var IAvatarManager */ |
|
| 54 | - protected $avatarManager; |
|
| 55 | - |
|
| 56 | - /** @var ICache */ |
|
| 57 | - protected $cache; |
|
| 58 | - |
|
| 59 | - /** @var IL10N */ |
|
| 60 | - protected $l; |
|
| 61 | - |
|
| 62 | - /** @var IUserManager */ |
|
| 63 | - protected $userManager; |
|
| 64 | - |
|
| 65 | - /** @var IUserSession */ |
|
| 66 | - protected $userSession; |
|
| 67 | - |
|
| 68 | - /** @var IRootFolder */ |
|
| 69 | - protected $rootFolder; |
|
| 70 | - |
|
| 71 | - /** @var ILogger */ |
|
| 72 | - protected $logger; |
|
| 73 | - |
|
| 74 | - /** @var string */ |
|
| 75 | - protected $userId; |
|
| 76 | - |
|
| 77 | - /** @var TimeFactory */ |
|
| 78 | - protected $timeFactory; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param string $appName |
|
| 82 | - * @param IRequest $request |
|
| 83 | - * @param IAvatarManager $avatarManager |
|
| 84 | - * @param ICache $cache |
|
| 85 | - * @param IL10N $l10n |
|
| 86 | - * @param IUserManager $userManager |
|
| 87 | - * @param IRootFolder $rootFolder |
|
| 88 | - * @param ILogger $logger |
|
| 89 | - * @param string $userId |
|
| 90 | - * @param TimeFactory $timeFactory |
|
| 91 | - */ |
|
| 92 | - public function __construct($appName, |
|
| 93 | - IRequest $request, |
|
| 94 | - IAvatarManager $avatarManager, |
|
| 95 | - ICache $cache, |
|
| 96 | - IL10N $l10n, |
|
| 97 | - IUserManager $userManager, |
|
| 98 | - IRootFolder $rootFolder, |
|
| 99 | - ILogger $logger, |
|
| 100 | - $userId, |
|
| 101 | - TimeFactory $timeFactory) { |
|
| 102 | - parent::__construct($appName, $request); |
|
| 103 | - |
|
| 104 | - $this->avatarManager = $avatarManager; |
|
| 105 | - $this->cache = $cache; |
|
| 106 | - $this->l = $l10n; |
|
| 107 | - $this->userManager = $userManager; |
|
| 108 | - $this->rootFolder = $rootFolder; |
|
| 109 | - $this->logger = $logger; |
|
| 110 | - $this->userId = $userId; |
|
| 111 | - $this->timeFactory = $timeFactory; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @NoAdminRequired |
|
| 119 | - * @NoCSRFRequired |
|
| 120 | - * @NoSameSiteCookieRequired |
|
| 121 | - * @PublicPage |
|
| 122 | - * |
|
| 123 | - * @param string $userId |
|
| 124 | - * @param int $size |
|
| 125 | - * @return JSONResponse|FileDisplayResponse |
|
| 126 | - */ |
|
| 127 | - public function getAvatar($userId, $size) { |
|
| 128 | - if ($size > 2048) { |
|
| 129 | - $size = 2048; |
|
| 130 | - } elseif ($size <= 0) { |
|
| 131 | - $size = 64; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - try { |
|
| 135 | - $avatar = $this->avatarManager->getAvatar($userId)->getFile($size); |
|
| 136 | - $resp = new FileDisplayResponse($avatar, |
|
| 137 | - Http::STATUS_OK, |
|
| 138 | - ['Content-Type' => $avatar->getMimeType()]); |
|
| 139 | - } catch (\Exception $e) { |
|
| 140 | - $resp = new Http\Response(); |
|
| 141 | - $resp->setStatus(Http::STATUS_NOT_FOUND); |
|
| 142 | - return $resp; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // Let cache this! |
|
| 146 | - $resp->addHeader('Pragma', 'public'); |
|
| 147 | - // Cache for 30 minutes |
|
| 148 | - $resp->cacheFor(1800); |
|
| 149 | - |
|
| 150 | - $expires = new \DateTime(); |
|
| 151 | - $expires->setTimestamp($this->timeFactory->getTime()); |
|
| 152 | - $expires->add(new \DateInterval('PT30M')); |
|
| 153 | - $resp->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
| 154 | - |
|
| 155 | - return $resp; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @NoAdminRequired |
|
| 160 | - * |
|
| 161 | - * @param string $path |
|
| 162 | - * @return JSONResponse |
|
| 163 | - */ |
|
| 164 | - public function postAvatar($path) { |
|
| 165 | - $files = $this->request->getUploadedFile('files'); |
|
| 166 | - |
|
| 167 | - if (isset($path)) { |
|
| 168 | - $path = stripslashes($path); |
|
| 169 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 170 | - /** @var File $node */ |
|
| 171 | - $node = $userFolder->get($path); |
|
| 172 | - if (!($node instanceof File)) { |
|
| 173 | - return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); |
|
| 174 | - } |
|
| 175 | - if ($node->getSize() > 20*1024*1024) { |
|
| 176 | - return new JSONResponse( |
|
| 177 | - ['data' => ['message' => $this->l->t('File is too big')]], |
|
| 178 | - Http::STATUS_BAD_REQUEST |
|
| 179 | - ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { |
|
| 183 | - return new JSONResponse( |
|
| 184 | - ['data' => ['message' => $this->l->t('The selected file is not an image.')]], |
|
| 185 | - Http::STATUS_BAD_REQUEST |
|
| 186 | - ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - try { |
|
| 190 | - $content = $node->getContent(); |
|
| 191 | - } catch (\OCP\Files\NotPermittedException $e) { |
|
| 192 | - return new JSONResponse( |
|
| 193 | - ['data' => ['message' => $this->l->t('The selected file cannot be read.')]], |
|
| 194 | - Http::STATUS_BAD_REQUEST |
|
| 195 | - ); |
|
| 196 | - } |
|
| 197 | - } elseif (!is_null($files)) { |
|
| 198 | - if ( |
|
| 199 | - $files['error'][0] === 0 && |
|
| 200 | - is_uploaded_file($files['tmp_name'][0]) && |
|
| 201 | - !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0]) |
|
| 202 | - ) { |
|
| 203 | - if ($files['size'][0] > 20*1024*1024) { |
|
| 204 | - return new JSONResponse( |
|
| 205 | - ['data' => ['message' => $this->l->t('File is too big')]], |
|
| 206 | - Http::STATUS_BAD_REQUEST |
|
| 207 | - ); |
|
| 208 | - } |
|
| 209 | - $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); |
|
| 210 | - $content = $this->cache->get('avatar_upload'); |
|
| 211 | - unlink($files['tmp_name'][0]); |
|
| 212 | - } else { |
|
| 213 | - return new JSONResponse( |
|
| 214 | - ['data' => ['message' => $this->l->t('Invalid file provided')]], |
|
| 215 | - Http::STATUS_BAD_REQUEST |
|
| 216 | - ); |
|
| 217 | - } |
|
| 218 | - } else { |
|
| 219 | - //Add imgfile |
|
| 220 | - return new JSONResponse( |
|
| 221 | - ['data' => ['message' => $this->l->t('No image or file provided')]], |
|
| 222 | - Http::STATUS_BAD_REQUEST |
|
| 223 | - ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - try { |
|
| 227 | - $image = new \OC_Image(); |
|
| 228 | - $image->loadFromData($content); |
|
| 229 | - $image->readExif($content); |
|
| 230 | - $image->fixOrientation(); |
|
| 231 | - |
|
| 232 | - if ($image->valid()) { |
|
| 233 | - $mimeType = $image->mimeType(); |
|
| 234 | - if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
|
| 235 | - return new JSONResponse( |
|
| 236 | - ['data' => ['message' => $this->l->t('Unknown filetype')]], |
|
| 237 | - Http::STATUS_OK |
|
| 238 | - ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $this->cache->set('tmpAvatar', $image->data(), 7200); |
|
| 242 | - return new JSONResponse( |
|
| 243 | - ['data' => 'notsquare'], |
|
| 244 | - Http::STATUS_OK |
|
| 245 | - ); |
|
| 246 | - } else { |
|
| 247 | - return new JSONResponse( |
|
| 248 | - ['data' => ['message' => $this->l->t('Invalid image')]], |
|
| 249 | - Http::STATUS_OK |
|
| 250 | - ); |
|
| 251 | - } |
|
| 252 | - } catch (\Exception $e) { |
|
| 253 | - $this->logger->logException($e, ['app' => 'core']); |
|
| 254 | - return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * @NoAdminRequired |
|
| 53 | + /** @var IAvatarManager */ |
|
| 54 | + protected $avatarManager; |
|
| 55 | + |
|
| 56 | + /** @var ICache */ |
|
| 57 | + protected $cache; |
|
| 58 | + |
|
| 59 | + /** @var IL10N */ |
|
| 60 | + protected $l; |
|
| 61 | + |
|
| 62 | + /** @var IUserManager */ |
|
| 63 | + protected $userManager; |
|
| 64 | + |
|
| 65 | + /** @var IUserSession */ |
|
| 66 | + protected $userSession; |
|
| 67 | + |
|
| 68 | + /** @var IRootFolder */ |
|
| 69 | + protected $rootFolder; |
|
| 70 | + |
|
| 71 | + /** @var ILogger */ |
|
| 72 | + protected $logger; |
|
| 73 | + |
|
| 74 | + /** @var string */ |
|
| 75 | + protected $userId; |
|
| 76 | + |
|
| 77 | + /** @var TimeFactory */ |
|
| 78 | + protected $timeFactory; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param string $appName |
|
| 82 | + * @param IRequest $request |
|
| 83 | + * @param IAvatarManager $avatarManager |
|
| 84 | + * @param ICache $cache |
|
| 85 | + * @param IL10N $l10n |
|
| 86 | + * @param IUserManager $userManager |
|
| 87 | + * @param IRootFolder $rootFolder |
|
| 88 | + * @param ILogger $logger |
|
| 89 | + * @param string $userId |
|
| 90 | + * @param TimeFactory $timeFactory |
|
| 91 | + */ |
|
| 92 | + public function __construct($appName, |
|
| 93 | + IRequest $request, |
|
| 94 | + IAvatarManager $avatarManager, |
|
| 95 | + ICache $cache, |
|
| 96 | + IL10N $l10n, |
|
| 97 | + IUserManager $userManager, |
|
| 98 | + IRootFolder $rootFolder, |
|
| 99 | + ILogger $logger, |
|
| 100 | + $userId, |
|
| 101 | + TimeFactory $timeFactory) { |
|
| 102 | + parent::__construct($appName, $request); |
|
| 103 | + |
|
| 104 | + $this->avatarManager = $avatarManager; |
|
| 105 | + $this->cache = $cache; |
|
| 106 | + $this->l = $l10n; |
|
| 107 | + $this->userManager = $userManager; |
|
| 108 | + $this->rootFolder = $rootFolder; |
|
| 109 | + $this->logger = $logger; |
|
| 110 | + $this->userId = $userId; |
|
| 111 | + $this->timeFactory = $timeFactory; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @NoAdminRequired |
|
| 119 | + * @NoCSRFRequired |
|
| 120 | + * @NoSameSiteCookieRequired |
|
| 121 | + * @PublicPage |
|
| 260 | 122 | * |
| 261 | - * @return JSONResponse |
|
| 262 | - */ |
|
| 263 | - public function deleteAvatar() { |
|
| 264 | - try { |
|
| 265 | - $avatar = $this->avatarManager->getAvatar($this->userId); |
|
| 266 | - $avatar->remove(); |
|
| 267 | - return new JSONResponse(); |
|
| 268 | - } catch (\Exception $e) { |
|
| 269 | - $this->logger->logException($e, ['app' => 'core']); |
|
| 270 | - return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @NoAdminRequired |
|
| 276 | - * |
|
| 277 | - * @return JSONResponse|DataDisplayResponse |
|
| 278 | - */ |
|
| 279 | - public function getTmpAvatar() { |
|
| 280 | - $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
| 281 | - if (is_null($tmpAvatar)) { |
|
| 282 | - return new JSONResponse(['data' => [ |
|
| 283 | - 'message' => $this->l->t("No temporary profile picture available, try again") |
|
| 284 | - ]], |
|
| 285 | - Http::STATUS_NOT_FOUND); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $image = new \OC_Image(); |
|
| 289 | - $image->loadFromData($tmpAvatar); |
|
| 290 | - |
|
| 291 | - $resp = new DataDisplayResponse($image->data(), |
|
| 292 | - Http::STATUS_OK, |
|
| 293 | - ['Content-Type' => $image->mimeType()]); |
|
| 294 | - |
|
| 295 | - $resp->setETag((string)crc32($image->data())); |
|
| 296 | - $resp->cacheFor(0); |
|
| 297 | - $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
|
| 298 | - return $resp; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * @NoAdminRequired |
|
| 303 | - * |
|
| 304 | - * @param array $crop |
|
| 305 | - * @return JSONResponse |
|
| 306 | - */ |
|
| 307 | - public function postCroppedAvatar($crop) { |
|
| 308 | - if (is_null($crop)) { |
|
| 309 | - return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]], |
|
| 310 | - Http::STATUS_BAD_REQUEST); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { |
|
| 314 | - return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], |
|
| 315 | - Http::STATUS_BAD_REQUEST); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
| 319 | - if (is_null($tmpAvatar)) { |
|
| 320 | - return new JSONResponse(['data' => [ |
|
| 321 | - 'message' => $this->l->t("No temporary profile picture available, try again") |
|
| 322 | - ]], |
|
| 323 | - Http::STATUS_BAD_REQUEST); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $image = new \OC_Image(); |
|
| 327 | - $image->loadFromData($tmpAvatar); |
|
| 328 | - $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h'])); |
|
| 329 | - try { |
|
| 330 | - $avatar = $this->avatarManager->getAvatar($this->userId); |
|
| 331 | - $avatar->set($image); |
|
| 332 | - // Clean up |
|
| 333 | - $this->cache->remove('tmpAvatar'); |
|
| 334 | - return new JSONResponse(['status' => 'success']); |
|
| 335 | - } catch (\OC\NotSquareException $e) { |
|
| 336 | - return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]], |
|
| 337 | - Http::STATUS_BAD_REQUEST); |
|
| 338 | - } catch (\Exception $e) { |
|
| 339 | - $this->logger->logException($e, ['app' => 'core']); |
|
| 340 | - return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
| 341 | - } |
|
| 342 | - } |
|
| 123 | + * @param string $userId |
|
| 124 | + * @param int $size |
|
| 125 | + * @return JSONResponse|FileDisplayResponse |
|
| 126 | + */ |
|
| 127 | + public function getAvatar($userId, $size) { |
|
| 128 | + if ($size > 2048) { |
|
| 129 | + $size = 2048; |
|
| 130 | + } elseif ($size <= 0) { |
|
| 131 | + $size = 64; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + try { |
|
| 135 | + $avatar = $this->avatarManager->getAvatar($userId)->getFile($size); |
|
| 136 | + $resp = new FileDisplayResponse($avatar, |
|
| 137 | + Http::STATUS_OK, |
|
| 138 | + ['Content-Type' => $avatar->getMimeType()]); |
|
| 139 | + } catch (\Exception $e) { |
|
| 140 | + $resp = new Http\Response(); |
|
| 141 | + $resp->setStatus(Http::STATUS_NOT_FOUND); |
|
| 142 | + return $resp; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // Let cache this! |
|
| 146 | + $resp->addHeader('Pragma', 'public'); |
|
| 147 | + // Cache for 30 minutes |
|
| 148 | + $resp->cacheFor(1800); |
|
| 149 | + |
|
| 150 | + $expires = new \DateTime(); |
|
| 151 | + $expires->setTimestamp($this->timeFactory->getTime()); |
|
| 152 | + $expires->add(new \DateInterval('PT30M')); |
|
| 153 | + $resp->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
| 154 | + |
|
| 155 | + return $resp; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @NoAdminRequired |
|
| 160 | + * |
|
| 161 | + * @param string $path |
|
| 162 | + * @return JSONResponse |
|
| 163 | + */ |
|
| 164 | + public function postAvatar($path) { |
|
| 165 | + $files = $this->request->getUploadedFile('files'); |
|
| 166 | + |
|
| 167 | + if (isset($path)) { |
|
| 168 | + $path = stripslashes($path); |
|
| 169 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 170 | + /** @var File $node */ |
|
| 171 | + $node = $userFolder->get($path); |
|
| 172 | + if (!($node instanceof File)) { |
|
| 173 | + return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); |
|
| 174 | + } |
|
| 175 | + if ($node->getSize() > 20*1024*1024) { |
|
| 176 | + return new JSONResponse( |
|
| 177 | + ['data' => ['message' => $this->l->t('File is too big')]], |
|
| 178 | + Http::STATUS_BAD_REQUEST |
|
| 179 | + ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { |
|
| 183 | + return new JSONResponse( |
|
| 184 | + ['data' => ['message' => $this->l->t('The selected file is not an image.')]], |
|
| 185 | + Http::STATUS_BAD_REQUEST |
|
| 186 | + ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + try { |
|
| 190 | + $content = $node->getContent(); |
|
| 191 | + } catch (\OCP\Files\NotPermittedException $e) { |
|
| 192 | + return new JSONResponse( |
|
| 193 | + ['data' => ['message' => $this->l->t('The selected file cannot be read.')]], |
|
| 194 | + Http::STATUS_BAD_REQUEST |
|
| 195 | + ); |
|
| 196 | + } |
|
| 197 | + } elseif (!is_null($files)) { |
|
| 198 | + if ( |
|
| 199 | + $files['error'][0] === 0 && |
|
| 200 | + is_uploaded_file($files['tmp_name'][0]) && |
|
| 201 | + !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0]) |
|
| 202 | + ) { |
|
| 203 | + if ($files['size'][0] > 20*1024*1024) { |
|
| 204 | + return new JSONResponse( |
|
| 205 | + ['data' => ['message' => $this->l->t('File is too big')]], |
|
| 206 | + Http::STATUS_BAD_REQUEST |
|
| 207 | + ); |
|
| 208 | + } |
|
| 209 | + $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); |
|
| 210 | + $content = $this->cache->get('avatar_upload'); |
|
| 211 | + unlink($files['tmp_name'][0]); |
|
| 212 | + } else { |
|
| 213 | + return new JSONResponse( |
|
| 214 | + ['data' => ['message' => $this->l->t('Invalid file provided')]], |
|
| 215 | + Http::STATUS_BAD_REQUEST |
|
| 216 | + ); |
|
| 217 | + } |
|
| 218 | + } else { |
|
| 219 | + //Add imgfile |
|
| 220 | + return new JSONResponse( |
|
| 221 | + ['data' => ['message' => $this->l->t('No image or file provided')]], |
|
| 222 | + Http::STATUS_BAD_REQUEST |
|
| 223 | + ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + try { |
|
| 227 | + $image = new \OC_Image(); |
|
| 228 | + $image->loadFromData($content); |
|
| 229 | + $image->readExif($content); |
|
| 230 | + $image->fixOrientation(); |
|
| 231 | + |
|
| 232 | + if ($image->valid()) { |
|
| 233 | + $mimeType = $image->mimeType(); |
|
| 234 | + if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
|
| 235 | + return new JSONResponse( |
|
| 236 | + ['data' => ['message' => $this->l->t('Unknown filetype')]], |
|
| 237 | + Http::STATUS_OK |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $this->cache->set('tmpAvatar', $image->data(), 7200); |
|
| 242 | + return new JSONResponse( |
|
| 243 | + ['data' => 'notsquare'], |
|
| 244 | + Http::STATUS_OK |
|
| 245 | + ); |
|
| 246 | + } else { |
|
| 247 | + return new JSONResponse( |
|
| 248 | + ['data' => ['message' => $this->l->t('Invalid image')]], |
|
| 249 | + Http::STATUS_OK |
|
| 250 | + ); |
|
| 251 | + } |
|
| 252 | + } catch (\Exception $e) { |
|
| 253 | + $this->logger->logException($e, ['app' => 'core']); |
|
| 254 | + return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * @NoAdminRequired |
|
| 260 | + * |
|
| 261 | + * @return JSONResponse |
|
| 262 | + */ |
|
| 263 | + public function deleteAvatar() { |
|
| 264 | + try { |
|
| 265 | + $avatar = $this->avatarManager->getAvatar($this->userId); |
|
| 266 | + $avatar->remove(); |
|
| 267 | + return new JSONResponse(); |
|
| 268 | + } catch (\Exception $e) { |
|
| 269 | + $this->logger->logException($e, ['app' => 'core']); |
|
| 270 | + return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @NoAdminRequired |
|
| 276 | + * |
|
| 277 | + * @return JSONResponse|DataDisplayResponse |
|
| 278 | + */ |
|
| 279 | + public function getTmpAvatar() { |
|
| 280 | + $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
| 281 | + if (is_null($tmpAvatar)) { |
|
| 282 | + return new JSONResponse(['data' => [ |
|
| 283 | + 'message' => $this->l->t("No temporary profile picture available, try again") |
|
| 284 | + ]], |
|
| 285 | + Http::STATUS_NOT_FOUND); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $image = new \OC_Image(); |
|
| 289 | + $image->loadFromData($tmpAvatar); |
|
| 290 | + |
|
| 291 | + $resp = new DataDisplayResponse($image->data(), |
|
| 292 | + Http::STATUS_OK, |
|
| 293 | + ['Content-Type' => $image->mimeType()]); |
|
| 294 | + |
|
| 295 | + $resp->setETag((string)crc32($image->data())); |
|
| 296 | + $resp->cacheFor(0); |
|
| 297 | + $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
|
| 298 | + return $resp; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * @NoAdminRequired |
|
| 303 | + * |
|
| 304 | + * @param array $crop |
|
| 305 | + * @return JSONResponse |
|
| 306 | + */ |
|
| 307 | + public function postCroppedAvatar($crop) { |
|
| 308 | + if (is_null($crop)) { |
|
| 309 | + return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]], |
|
| 310 | + Http::STATUS_BAD_REQUEST); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { |
|
| 314 | + return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], |
|
| 315 | + Http::STATUS_BAD_REQUEST); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
| 319 | + if (is_null($tmpAvatar)) { |
|
| 320 | + return new JSONResponse(['data' => [ |
|
| 321 | + 'message' => $this->l->t("No temporary profile picture available, try again") |
|
| 322 | + ]], |
|
| 323 | + Http::STATUS_BAD_REQUEST); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $image = new \OC_Image(); |
|
| 327 | + $image->loadFromData($tmpAvatar); |
|
| 328 | + $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h'])); |
|
| 329 | + try { |
|
| 330 | + $avatar = $this->avatarManager->getAvatar($this->userId); |
|
| 331 | + $avatar->set($image); |
|
| 332 | + // Clean up |
|
| 333 | + $this->cache->remove('tmpAvatar'); |
|
| 334 | + return new JSONResponse(['status' => 'success']); |
|
| 335 | + } catch (\OC\NotSquareException $e) { |
|
| 336 | + return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]], |
|
| 337 | + Http::STATUS_BAD_REQUEST); |
|
| 338 | + } catch (\Exception $e) { |
|
| 339 | + $this->logger->logException($e, ['app' => 'core']); |
|
| 340 | + return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | 343 | } |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | if (!($node instanceof File)) { |
| 173 | 173 | return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); |
| 174 | 174 | } |
| 175 | - if ($node->getSize() > 20*1024*1024) { |
|
| 175 | + if ($node->getSize() > 20 * 1024 * 1024) { |
|
| 176 | 176 | return new JSONResponse( |
| 177 | 177 | ['data' => ['message' => $this->l->t('File is too big')]], |
| 178 | 178 | Http::STATUS_BAD_REQUEST |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | is_uploaded_file($files['tmp_name'][0]) && |
| 201 | 201 | !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0]) |
| 202 | 202 | ) { |
| 203 | - if ($files['size'][0] > 20*1024*1024) { |
|
| 203 | + if ($files['size'][0] > 20 * 1024 * 1024) { |
|
| 204 | 204 | return new JSONResponse( |
| 205 | 205 | ['data' => ['message' => $this->l->t('File is too big')]], |
| 206 | 206 | Http::STATUS_BAD_REQUEST |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | Http::STATUS_OK, |
| 293 | 293 | ['Content-Type' => $image->mimeType()]); |
| 294 | 294 | |
| 295 | - $resp->setETag((string)crc32($image->data())); |
|
| 295 | + $resp->setETag((string) crc32($image->data())); |
|
| 296 | 296 | $resp->cacheFor(0); |
| 297 | 297 | $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
| 298 | 298 | return $resp; |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | |
| 326 | 326 | $image = new \OC_Image(); |
| 327 | 327 | $image->loadFromData($tmpAvatar); |
| 328 | - $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h'])); |
|
| 328 | + $image->crop($crop['x'], $crop['y'], (int) round($crop['w']), (int) round($crop['h'])); |
|
| 329 | 329 | try { |
| 330 | 330 | $avatar = $this->avatarManager->getAvatar($this->userId); |
| 331 | 331 | $avatar->set($image); |