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
|
<?php
namespace Bitweaver\Liberty;
use Bitweaver\KernelTools;
use Imagick;
use ImagickPixel;
use ImagickException;
/**
* $Header$
*
* Image processor - extension: php-imagick
* @package liberty
* @subpackage plugins_processor
* @author spider <spider@steelsun.com>
*/
/**
* liberty_imagick_can_thumbnail_image
*
* @param string $pMimeType
* @access public
* @return string|bool 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...)
$ret = $gBitSystem->isFeatureActive( 'liberty_thumbnail_pdf' )
? preg_match( '/(^image|pdf$|postscript$)/i', $pMimeType )
: preg_match( '/^image/i', $pMimeType );
}
return $ret;
}
// ============================================
// ======== Version 2.* of php-imagick ========
// ============================================
function liberty_imagick_resize_image( &$pFileHash ) {
global $gBitSystem;
$pFileHash['error'] = null;
$ret = null;
if( !empty( $pFileHash['source_file'] ) && file_exists( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] ) && (filesize( $pFileHash['source_file'] ) > 0) ) {
try {
$im = new Imagick();
$im->readImage( $pFileHash['source_file'] );
if( !$im->valid()) {
$destFile = \Bitweaver\Liberty\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;
}
*/
global $gBitSystem;
$pFileHash['type'] = $gBitSystem->verifyMimeType( $pFileHash['source_file'] );
// 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'] ) ) || ( !empty( $pFileHash['max_width'] ) && $pFileHash['max_width'] == MAX_THUMBNAIL_DIMENSION ) || ( !empty( $pFileHash['max_height'] ) && $pFileHash['max_height'] == MAX_THUMBNAIL_DIMENSION ) ) {
$pFileHash['max_width'] = $iwidth;
$pFileHash['max_height'] = $iheight;
} elseif( $iheight && ( $iwidth / $iheight ) < 1 && !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'] = round(( $iwidth / $iheight ) * $pFileHash['max_height'] );
} elseif( !empty( $pFileHash['max_width'] ) ) {
$pFileHash['max_height'] = round(( $iheight / $iwidth ) * $pFileHash['max_width'] );
} elseif( !empty( $pFileHash['max_height'] ) ) {
$pFileHash['max_width'] = round(( $iwidth / $iheight ) * $pFileHash['max_height'] );
}
if( !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) && (( $pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight ) || $mimeExt != $targetType )) {
$destFile = !empty( $pFileHash['dest_file'] )
? $pFileHash['dest_file']
: STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt;
if( !empty( $pFileHash['dest_base_name'] ) ) {
$pFileHash['name'] = $pFileHash['dest_base_name'].$destExt;
}
// create thumb and write
$im->thumbnailImage( (int)$pFileHash['max_width'], (int)$pFileHash['max_height'], true );
$im->writeImage( $destFile );
$pFileHash['size'] = filesize( $destFile );
} else {
$destFile = \Bitweaver\Liberty\liberty_process_generic( $pFileHash, false );
}
}
// destroy object
$im->clear();
$ret = $destFile;
} catch( ImagickException $e ) {
\Bitweaver\bit_error_log( $e->getMessage().' '.$pFileHash['source_file'] );
$ret = null;
}
} else {
$pFileHash['error'] = "No source file to resize";
}
return $ret;
}
function liberty_imagick_rotate_image( &$pFileHash ) {
$ret = false;
if( !empty( $pFileHash['source_file'] ) && is_file( $pFileHash['source_file'] )) {
try {
$im = new Imagick();
$im->readImage( $pFileHash['source_file'] );
if( !$im->valid()) {
$destFile = \Bitweaver\Liberty\liberty_process_generic( $pFileHash, false );
} elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] )) {
$pFileHash['error'] = KernelTools::tra( 'Invalid rotation amount' );
} else {
$im->rotateImage( new ImagickPixel('none'), $pFileHash['degrees'] );
$im->writeImage( $pFileHash['source_file'] );
}
$im->clear();
} catch( ImagickException $e ) {
\Bitweaver\bit_error_log( $e->getMessage().' '.$pFileHash['source_file'] );
$pFileHash['error'] = $e->getMessage();
}
} else {
$pFileHash['error'] = "No source file to resize";
}
return empty( $pFileHash['error'] );
}
|