66 * @author Bart Visscher <[email protected] > 77 * @author Björn Schießle <[email protected] > 88 * @author Byron Marohn <[email protected] > 9+ * @author Côme Chilliet <[email protected] > 910 * @author Christopher Schäpers <[email protected] > 1011 * @author Christoph Wurst <[email protected] > 1112 * @author Georg Ehrke <[email protected] > 4546 * Class for basic image manipulation
4647 */
4748class OC_Image implements \OCP \IImage {
48- /** @var false|resource */
49+ /** @var false|resource|\GdImage */
4950 protected $ resource = false ; // tmp resource.
5051 /** @var int */
5152 protected $ imageType = IMAGETYPE_PNG ; // Default to png if file type isn't evident.
@@ -67,7 +68,7 @@ class OC_Image implements \OCP\IImage {
6768 /**
6869 * Constructor.
6970 *
70- * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by
71+ * @param resource|string|\GdImage $imageRef The path to a local file, a base64 encoded string or a resource created by
7172 * an imagecreate* function.
7273 * @param \OCP\ILogger $logger
7374 * @param \OCP\IConfig $config
@@ -97,7 +98,7 @@ public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\
9798 *
9899 * @return bool
99100 */
100- public function valid () { // apparently you can't name a method 'empty'...
101+ public function valid () {
101102 if (is_resource ($ this ->resource )) {
102103 return true ;
103104 }
@@ -123,7 +124,13 @@ public function mimeType() {
123124 * @return int
124125 */
125126 public function width () {
126- return $ this ->valid () ? imagesx ($ this ->resource ) : -1 ;
127+ if ($ this ->valid ()) {
128+ $ width = imagesx ($ this ->resource );
129+ if ($ width !== false ) {
130+ return $ width ;
131+ }
132+ }
133+ return -1 ;
127134 }
128135
129136 /**
@@ -132,7 +139,13 @@ public function width() {
132139 * @return int
133140 */
134141 public function height () {
135- return $ this ->valid () ? imagesy ($ this ->resource ) : -1 ;
142+ if ($ this ->valid ()) {
143+ $ height = imagesy ($ this ->resource );
144+ if ($ height !== false ) {
145+ return $ height ;
146+ }
147+ }
148+ return -1 ;
136149 }
137150
138151 /**
@@ -326,7 +339,7 @@ public function setResource($resource) {
326339 }
327340
328341 /**
329- * @return resource|\GdImage Returns the image resource in any.
342+ * @return false| resource|\GdImage Returns the image resource if any
330343 */
331344 public function resource () {
332345 return $ this ->resource ;
@@ -468,6 +481,10 @@ public function readExif($data) {
468481 * @return bool
469482 */
470483 public function fixOrientation () {
484+ if (!$ this ->valid ()) {
485+ $ this ->logger ->error (__METHOD__ . '(): No image loaded ' , ['app ' => 'core ' ]);
486+ return false ;
487+ }
471488 $ o = $ this ->getOrientation ();
472489 $ this ->logger ->debug ('OC_Image->fixOrientation() Orientation: ' . $ o , ['app ' => 'core ' ]);
473490 $ rotate = 0 ;
@@ -875,6 +892,10 @@ private function imagecreatefrombmp($fileName) {
875892 * @return bool
876893 */
877894 public function resize ($ maxSize ) {
895+ if (!$ this ->valid ()) {
896+ $ this ->logger ->error (__METHOD__ . '(): No image loaded ' , ['app ' => 'core ' ]);
897+ return false ;
898+ }
878899 $ result = $ this ->resizeNew ($ maxSize );
879900 imagedestroy ($ this ->resource );
880901 $ this ->resource = $ result ;
@@ -911,6 +932,10 @@ private function resizeNew($maxSize) {
911932 * @return bool
912933 */
913934 public function preciseResize (int $ width , int $ height ): bool {
935+ if (!$ this ->valid ()) {
936+ $ this ->logger ->error (__METHOD__ . '(): No image loaded ' , ['app ' => 'core ' ]);
937+ return false ;
938+ }
914939 $ result = $ this ->preciseResizeNew ($ width , $ height );
915940 imagedestroy ($ this ->resource );
916941 $ this ->resource = $ result ;
@@ -990,9 +1015,8 @@ public function centerCrop($size = 0) {
9901015 $ targetHeight = $ height ;
9911016 }
9921017 $ process = imagecreatetruecolor ($ targetWidth , $ targetHeight );
993- if ($ process == false ) {
1018+ if ($ process === false ) {
9941019 $ this ->logger ->error ('OC_Image->centerCrop, Error creating true color image ' , ['app ' => 'core ' ]);
995- imagedestroy ($ process );
9961020 return false ;
9971021 }
9981022
@@ -1004,9 +1028,8 @@ public function centerCrop($size = 0) {
10041028 }
10051029
10061030 imagecopyresampled ($ process , $ this ->resource , 0 , 0 , $ x , $ y , $ targetWidth , $ targetHeight , $ width , $ height );
1007- if ($ process == false ) {
1031+ if ($ process === false ) {
10081032 $ this ->logger ->error ('OC_Image->centerCrop, Error re-sampling process image ' . $ width . 'x ' . $ height , ['app ' => 'core ' ]);
1009- imagedestroy ($ process );
10101033 return false ;
10111034 }
10121035 imagedestroy ($ this ->resource );
@@ -1024,6 +1047,10 @@ public function centerCrop($size = 0) {
10241047 * @return bool for success or failure
10251048 */
10261049 public function crop (int $ x , int $ y , int $ w , int $ h ): bool {
1050+ if (!$ this ->valid ()) {
1051+ $ this ->logger ->error (__METHOD__ . '(): No image loaded ' , ['app ' => 'core ' ]);
1052+ return false ;
1053+ }
10271054 $ result = $ this ->cropNew ($ x , $ y , $ w , $ h );
10281055 imagedestroy ($ this ->resource );
10291056 $ this ->resource = $ result ;
@@ -1037,17 +1064,16 @@ public function crop(int $x, int $y, int $w, int $h): bool {
10371064 * @param int $y Vertical position
10381065 * @param int $w Width
10391066 * @param int $h Height
1040- * @return resource | bool
1067+ * @return resource|\GdImage|false
10411068 */
10421069 public function cropNew (int $ x , int $ y , int $ w , int $ h ) {
10431070 if (!$ this ->valid ()) {
10441071 $ this ->logger ->error (__METHOD__ . '(): No image loaded ' , ['app ' => 'core ' ]);
10451072 return false ;
10461073 }
10471074 $ process = imagecreatetruecolor ($ w , $ h );
1048- if ($ process == false ) {
1075+ if ($ process === false ) {
10491076 $ this ->logger ->error (__METHOD__ . '(): Error creating true color image ' , ['app ' => 'core ' ]);
1050- imagedestroy ($ process );
10511077 return false ;
10521078 }
10531079
@@ -1059,9 +1085,8 @@ public function cropNew(int $x, int $y, int $w, int $h) {
10591085 }
10601086
10611087 imagecopyresampled ($ process , $ this ->resource , 0 , 0 , $ x , $ y , $ w , $ h , $ w , $ h );
1062- if ($ process == false ) {
1088+ if ($ process === false ) {
10631089 $ this ->logger ->error (__METHOD__ . '(): Error re-sampling process image ' . $ w . 'x ' . $ h , ['app ' => 'core ' ]);
1064- imagedestroy ($ process );
10651090 return false ;
10661091 }
10671092 return $ process ;
@@ -1168,7 +1193,7 @@ public function destroy() {
11681193 if ($ this ->valid ()) {
11691194 imagedestroy ($ this ->resource );
11701195 }
1171- $ this ->resource = null ;
1196+ $ this ->resource = false ;
11721197 }
11731198
11741199 public function __destruct () {
0 commit comments