summaryrefslogtreecommitdiff
path: root/plugins/processor.imagick.php
blob: 3743bbe759b46df55f0768320e26bf634b3da393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
 * $Header$
 *
 * Image processor - extension: php-imagick
 * @package  liberty
 * @subpackage plugins_processor
 * @author   spider <spider@steelsun.com>
 */

/**
 * liberty_imagick_resize_image 
 * 
 * @param array $pFileHash 
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_imagick_resize_image( &$pFileHash ) {
	if( $func = liberty_imagick_get_function( 'resize_image' )) {
		return $func( $pFileHash );
	}
}

/**
 * liberty_imagick_rotate_image 
 * 
 * @param array $pFileHash 
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_imagick_rotate_image( &$pFileHash ) {
	if( $func = liberty_imagick_get_function( 'rotate_image' )) {
		return $func( $pFileHash );
	}
}

/**
 * liberty_imagick_can_thumbnail_image 
 * 
 * @param array $pMimeType 
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_imagick_can_thumbnail_image( $pMimeType ) {
	global $gBitSystem;
	$ret = FALSE;
	if( !empty( $pMimeType ) ) {
		// allow images, pdf, and postscript thumbnailing (eps, ai, etc...)
		if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_pdf' )) {
			$ret = preg_match( '/(^image|pdf$|postscript$)/i', $pMimeType );
		} else {
			$ret = preg_match( '/^image/i', $pMimeType );
		}
	}
	return $ret;
}

/**
 * liberty_imagick_get_function will automagically pick the correct function based on the version of imagick extension installed 
 * 
 * @return valid function.
 */
function liberty_imagick_get_function( $pFunction ) {
	$ret = FALSE;
	if( extension_loaded( 'imagick' )) {
		if( function_exists( 'imagick_readimage' )) {
			$version = 0;
		} elseif( class_exists( 'Imagick' )) {
			$version = 2;
		}
	}

	if( isset( $version ) && !empty( $pFunction )) {
		$func = 'liberty_imagick'.$version.'_'.$pFunction;
		if( function_exists( $func )) {
			$ret = $func;
		}
	}
	return $ret;
}


// =============================================
// ======== Version 0.9* of php-imagick ========
// =============================================
function liberty_imagick0_resize_image( &$pFileHash ) {
	global $gBitSystem;
	$pFileHash['error'] = NULL;
	$ret = NULL;
	if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
		$iImg = imagick_readimage( $pFileHash['source_file'] );
		if( !$iImg ) {
			// $pFileHash['error'] = $pFileHash['name'].' '.tra ( "is not a known image file" );
			$destFile = liberty_process_generic( $pFileHash, FALSE );
		} elseif( imagick_iserror( $iImg ) ) {
			// $pFileHash['error'] = imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
			$destFile = liberty_process_generic( $pFileHash, FALSE );
		} else {
			imagick_set_image_quality( $iImg, $gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 ));
			$iwidth = imagick_getwidth( $iImg );
			$iheight = imagick_getheight( $iImg );
			if( (($iwidth / $iheight) > 0) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) ) {
				// we have a portrait image, flip everything
				$temp = $pFileHash['max_width'];
				$pFileHash['max_height'] = $pFileHash['max_width'];
				$pFileHash['max_width'] = $temp;
			}

			$itype = imagick_getmimetype( $iImg );

			// override $mimeExt if we have a custom setting for it
			if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_format' )) {
				$mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
			} else {
				list( $type, $mimeExt ) = explode( '/', strtolower( $itype ));
			}

			if( $mimeExt = preg_replace( "!^(x-)?(jpeg|png|gif)$!", "$2", $mimeExt )) {
				$targetType = $mimeExt;
				$destExt = '.'.$mimeExt;
			}
			if( !$mimeExt || $mimeExt == 'jpeg' ) {
				$targetType = 'jpeg';
				$destExt = '.jpg';
			}

			if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && ( ($pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) {
				// We have to resize. *ALL* resizes are converted to jpeg or png
				if( !empty( $pFileHash['dest_file'] ) ) {
					$destFile = $pFileHash['dest_file'];
				} else {
					$destFile = STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt;
				}
				$pFileHash['name'] = $pFileHash['dest_base_name'].$destExt;
				// print "			if ( !imagick_resize( $iImg, $pFileHash[max_width], $pFileHash[max_height], IMAGICK_FILTER_LANCZOS, 0.5, $pFileHash[max_width] x $pFileHash[max_height] > ) ) {";

				// Alternate Filter settings can seen here http://www.dylanbeattie.net/magick/filters/result.html

				if ( !imagick_resize( $iImg, $pFileHash['max_width'], $pFileHash['max_height'], IMAGICK_FILTER_CATROM, 1.00, '>' ) ) {
					$pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
				}

				if( function_exists( 'imagick_set_attribute' ) ) {
					// this exists in the PECL package, but not php-imagick
					$imagick_set_attribute( $iImg, array( "quality"=>1 ));
				}

				if( !imagick_writeimage( $iImg, $destFile ) ) {
					$pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
				}
				$pFileHash['size'] = filesize( $destFile );
			} else {
				// print "GENERIC";
				$destFile = liberty_process_generic( $pFileHash, FALSE );
			}
		}
		$ret = $destFile;
	} else {
		$pFileHash['error'] = "No source file to resize";
	}

	return $ret;
}

function liberty_imagick0_rotate_image( &$pFileHash ) {
	$ret = FALSE;
	if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) ) {
		$iImg = imagick_readimage( $pFileHash['source_file'] );
		if( !$iImg ) {
			$pFileHash['error'] = $pFileHash['name'].' '.tra ( "is not a known image file" );
		} elseif( imagick_iserror( $iImg ) ) {
			$pFileHash['error'] = imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
		} elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] ) ) {
			$pFileHash['error'] = tra( 'Invalid rotation amount' );
		} else {
			if ( !imagick_rotate( $iImg, $pFileHash['degrees'] ) ) {
				$pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
			}
			if( !imagick_writeimage( $iImg, $pFileHash['source_file'] ) ) {
				$pFileHash['error'] .= imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
			}
		}
	} else {
		$pFileHash['error'] = "No source file to resize";
	}

	return( empty( $pFileHash['error'] ) );
}



// ============================================
// ======== Version 2.* of php-imagick ========
// ============================================
function liberty_imagick2_resize_image( &$pFileHash ) {
	global $gBitSystem;
	$pFileHash['error'] = NULL;
	$ret = NULL;
	if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) {
		$im = new Imagick();
		$im->readImage( $pFileHash['source_file'] );
		if( !$im->valid()) {
			$destFile = liberty_process_generic( $pFileHash, FALSE );
		} else {
			$im->setCompressionQuality( $gBitSystem->getConfig( 'liberty_thumbnail_quality', 85 ));
			$iwidth = $im->getImageWidth();
			$iheight = $im->getImageHeight();
			/*
			 * the math on this was bad and the property assignments were bad - those are fixed. 
			 * however this is disabled since its being invoked by default, which prevents the max height
			 * from being enforced on portrait images which is counter intuitive. by default the bounding
			 * rectangle should be enforced. if someone wants this feature, then a flag for this needs to
			 * be created which will likely require some bigger change up stream, like in gThumbSizes and
			 * in liberty_generate_thumbnails()
			 * -wjames5
			if((( $iwidth / $iheight ) < 1 ) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] )) {
				// we have a portrait image, flip everything
				$temp = $pFileHash['max_height'];
				$pFileHash['max_height'] = $pFileHash['max_width'];
				$pFileHash['max_width'] = $temp;
			}
			 */

			// override $mimeExt if we have a custom setting for it
			if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_format' )) {
				$mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
			} else {
				list( $type, $mimeExt ) = explode( '/', strtolower( $pFileHash['type'] ));
			}

			if( preg_match( "!(png|gif)!", $mimeExt )) {
				$targetType = $mimeExt;
				$destExt = '.'.$mimeExt;
			} else {
				$targetType = 'jpeg';
				$destExt = '.jpg';
			}

			if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && (( $pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) {
				if( !empty( $pFileHash['dest_file'] ) ) {
					$destFile = $pFileHash['dest_file'];
				} else {
					$destFile = STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt;
				}
				$pFileHash['name'] = $pFileHash['dest_base_name'].$destExt;

				// create thumb and write
				$im->thumbnailImage( $pFileHash['max_width'],  $pFileHash['max_height'], TRUE );
				$im->writeImage( $destFile );

				$pFileHash['size'] = filesize( $destFile );
			} else {
				$destFile = liberty_process_generic( $pFileHash, FALSE );
			}
		}

		// destroy object
		$im->destroy();

		$ret = $destFile;
	} else {
		$pFileHash['error'] = "No source file to resize";
	}

	return $ret;
}

function liberty_imagick2_rotate_image( &$pFileHash ) {
	$ret = FALSE;
	if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) {
		$im = new Imagick();
		$im->readImage( $pFileHash['source_file'] );
		if( !$im->valid()) {
			$destFile = liberty_process_generic( $pFileHash, FALSE );
		} elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] )) {
			$pFileHash['error'] = tra( 'Invalid rotation amount' );
		} else {
			$im->rotateImage( new ImagickPixel(), $pFileHash['degrees'] );
			$im->writeImage( $pFileHash['source_file'] );
		}
	} else {
		$pFileHash['error'] = "No source file to resize";
	}

	return( empty( $pFileHash['error'] ));
}