Changeset 1827158
- Timestamp:
- 02/22/2018 05:37:01 PM (8 years ago)
- File:
-
- 1 edited
-
hiweb-image-orient/trunk/inc/functions.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
hiweb-image-orient/trunk/inc/functions.php
r1474257 r1827158 7 7 */ 8 8 9 10 function _hw_io_hook_wp_handle_upload_prefilter($file) { 11 if (isset($file['type']) && $file['type'] == 'image/jpeg') { 12 $exif_data = exif_read_data($file['tmp_name']); 13 foreach ($exif_data as $key => $data) { 14 if (strtolower($key) == 'orientation') { 15 _hw_io_apply_orientation($file['tmp_name'], $data, $exif_data); 9 function _hw_io_hook_wp_handle_upload_prefilter( $file ){ 10 if( isset( $file['type'] ) && $file['type'] == 'image/jpeg' ){ 11 $exif_data = exif_read_data( $file['tmp_name'] ); 12 foreach( $exif_data as $key => $data ){ 13 if( strtolower( $key ) == 'orientation' ){ 14 _hw_io_apply_orientation( $file['tmp_name'], $data, $exif_data ); 16 15 } 17 16 } … … 20 19 } 21 20 22 23 function _hw_io_apply_orientation($filePath, $orientation = 0, $exifData = array()) { 24 $orientToDegree = array( 21 function _hw_io_apply_orientation( $filePath, $orientation = 0, $exifData = [] ){ 22 $orientToDegree = [ 25 23 3 => 180, 26 24 6 => 90, 27 25 8 => 270 28 );26 ]; 29 27 /// 30 if (!isset($orientToDegree[$orientation])) return false;28 if( !isset( $orientToDegree[ $orientation ] ) ) return false; 31 29 /// 32 $img = imagecreatefromjpeg($filePath); 33 $img = _hw_io_rotateImage($img, $orientToDegree[$orientation]); 34 return imagejpeg($img, $filePath); 35 } 36 37 38 function _hw_io_rotateImage($img, $rotation) { 39 $width = imagesx($img); 40 $height = imagesy($img); 41 switch ($rotation) { 30 $img = imagecreatefromjpeg( $filePath ); 31 $img = $orientation == 3 ? _hw_io_rotateImage2( $img, $orientToDegree[ $orientation ] ) : _hw_io_rotateImage( $img, $orientToDegree[ $orientation ] ); 32 return imagejpeg( $img, $filePath ); 33 } 34 35 function _hw_io_rotateImage( $img, $rotation ){ 36 $width = imagesx( $img ); 37 $height = imagesy( $img ); 38 switch( $rotation ){ 42 39 case 90: 43 $newimg = @imagecreatetruecolor( $height, $width);40 $newimg = @imagecreatetruecolor( $height, $width ); 44 41 break; 45 42 case 180: 46 $newimg = @imagecreatetruecolor( $width, $height);43 $newimg = @imagecreatetruecolor( $width, $height ); 47 44 break; 48 45 case 270: 49 $newimg = @imagecreatetruecolor( $height, $width);46 $newimg = @imagecreatetruecolor( $height, $width ); 50 47 break; 51 48 case 0: … … 56 53 break; 57 54 } 58 if ($newimg){59 for ($i = 0; $i < $width; $i++){60 for ($j = 0; $j < $height; $j++){61 $reference = imagecolorat( $img, $i, $j);62 switch ($rotation){55 if( $newimg ){ 56 for( $i = 0; $i < $width; $i ++ ){ 57 for( $j = 0; $j < $height; $j ++ ){ 58 $reference = imagecolorat( $img, $i, $j ); 59 switch( $rotation ){ 63 60 case 90: 64 if (!@imagesetpixel($newimg, ($height - 1) - $j, $i, $reference)){61 if( !@imagesetpixel( $newimg, ( $height - 1 ) - $j, $i, $reference ) ){ 65 62 return false; 66 63 } 67 64 break; 68 65 case 180: 69 if (!@imagesetpixel($newimg, $i, ($height - 1) - $j, $reference)){66 if( !@imagesetpixel( $newimg, $i, ( $height - 1 ) - $j, $reference ) ){ 70 67 return false; 71 68 } 72 69 break; 73 70 case 270: 74 if (!@imagesetpixel($newimg, $j, $width - $i, $reference)){71 if( !@imagesetpixel( $newimg, $j, $width - $i, $reference ) ){ 75 72 return false; 76 73 } … … 84 81 } 85 82 86 87 function _hw_io_tool_template() { 83 /** 84 * Imagerotate replacement. ignore_transparent is work for png images 85 * Also, have some standard functions for 90, 180 and 270 degrees. 86 * Rotation is clockwise 87 * @param $srcImg - Image resource 88 * @param $angle - degree angle, etc. 90, 180, 270, 45... 89 * @param $bgcolor 90 * @param int $ignore_transparent 91 * @return resource 92 */ 93 function _hw_io_rotateImage2( $srcImg, $angle, $bgcolor = 0, $ignore_transparent = 0 ){ 94 function rotateX( $x, $y, $theta ){ 95 return $x * cos( $theta ) - $y * sin( $theta ); 96 } 97 98 function rotateY( $x, $y, $theta ){ 99 return $x * sin( $theta ) + $y * cos( $theta ); 100 } 101 102 $srcw = imagesx( $srcImg ); 103 $srch = imagesy( $srcImg ); 104 105 //Normalize angle 106 $angle %= 360; 107 //Set rotate to clockwise 108 $angle = - $angle; 109 110 if( $angle == 0 ){ 111 if( $ignore_transparent == 0 ){ 112 imagesavealpha( $srcImg, true ); 113 } 114 return $srcImg; 115 } 116 117 // Convert the angle to radians 118 $theta = deg2rad( $angle ); 119 120 //Standart case of rotate 121 if( ( abs( $angle ) == 90 ) || ( abs( $angle ) == 270 ) ){ 122 $width = $srch; 123 $height = $srcw; 124 if( ( $angle == 90 ) || ( $angle == - 270 ) ){ 125 $minX = 0; 126 $maxX = $width; 127 $minY = - $height + 1; 128 $maxY = 1; 129 } else if( ( $angle == - 90 ) || ( $angle == 270 ) ){ 130 $minX = - $width + 1; 131 $maxX = 1; 132 $minY = 0; 133 $maxY = $height; 134 } 135 } else if( abs( $angle ) === 180 ){ 136 $width = $srcw; 137 $height = $srch; 138 $minX = - $width + 1; 139 $maxX = 1; 140 $minY = - $height + 1; 141 $maxY = 1; 142 } else { 143 // Calculate the width of the destination image. 144 $temp = [ 145 rotateX( 0, 0, 0 - $theta ), 146 rotateX( $srcw, 0, 0 - $theta ), 147 rotateX( 0, $srch, 0 - $theta ), 148 rotateX( $srcw, $srch, 0 - $theta ) 149 ]; 150 $minX = floor( min( $temp ) ); 151 $maxX = ceil( max( $temp ) ); 152 $width = $maxX - $minX; 153 154 // Calculate the height of the destination image. 155 $temp = [ 156 rotateY( 0, 0, 0 - $theta ), 157 rotateY( $srcw, 0, 0 - $theta ), 158 rotateY( 0, $srch, 0 - $theta ), 159 rotateY( $srcw, $srch, 0 - $theta ) 160 ]; 161 $minY = floor( min( $temp ) ); 162 $maxY = ceil( max( $temp ) ); 163 $height = $maxY - $minY; 164 } 165 166 $destimg = imagecreatetruecolor( $width, $height ); 167 if( $ignore_transparent == 0 ){ 168 imagefill( $destimg, 0, 0, imagecolorallocatealpha( $destimg, 255, 255, 255, 127 ) ); 169 imagesavealpha( $destimg, true ); 170 } 171 172 // sets all pixels in the new image 173 for( $x = $minX; $x < $maxX; $x ++ ){ 174 for( $y = $minY; $y < $maxY; $y ++ ){ 175 // fetch corresponding pixel from the source image 176 $srcX = round( rotateX( $x, $y, $theta ) ); 177 $srcY = round( rotateY( $x, $y, $theta ) ); 178 if( $srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch ){ 179 $color = imagecolorat( $srcImg, $srcX, $srcY ); 180 } else { 181 $color = $bgcolor; 182 } 183 imagesetpixel( $destimg, $x - $minX, $y - $minY, $color ); 184 } 185 } 186 return $destimg; 187 } 188 189 function _hw_io_tool_template(){ 88 190 include HW_IO_DIR_TEMPLATE . '/_hw_io_tool_template.php'; 89 191 } 90 192 91 92 function _hw_io_ajax() { 93 $R = array(); 94 if ($_POST['do'] == 'reorient') { 95 $image = _hw_io_getOrientFromImagePost($_POST['id'], false); 96 $R[$image['post']->ID] = array('name' => $image['post']->post_title); 97 if ($image !== false && $image['orientation'] != 0 && $image['orientation'] != 1) { 98 $meta = get_post_meta($image['post']->ID,'_wp_attachment_metadata',true); 99 if(is_array($image['meta']['sizes'])) foreach ($image['meta']['sizes'] as $sizeName => $item){ 100 $file = dirname($image['path']).'/'.$item['file']; 101 $R[$image['post']->ID]['files'][$file] = _hw_io_apply_orientation($file, $image['meta']['image_meta']['orientation']); 193 function _hw_io_ajax(){ 194 $R = []; 195 if( $_POST['do'] == 'reorient' ){ 196 $image = _hw_io_getOrientFromImagePost( $_POST['id'], false ); 197 $R[ $image['post']->ID ] = [ 'name' => $image['post']->post_title ]; 198 if( $image !== false && $image['orientation'] != 0 && $image['orientation'] != 1 ){ 199 $meta = get_post_meta( $image['post']->ID, '_wp_attachment_metadata', true ); 200 if( is_array( $image['meta']['sizes'] ) ) foreach( $image['meta']['sizes'] as $sizeName => $item ){ 201 $file = dirname( $image['path'] ) . '/' . $item['file']; 202 $R[ $image['post']->ID ]['files'][ $file ] = _hw_io_apply_orientation( $file, $image['meta']['image_meta']['orientation'] ); 102 203 } 103 204 $meta['image_meta']['orientation'] = 0; 104 $R[ $image['post']->ID]['update_post_meta'] = update_post_meta($image['post']->ID,'_wp_attachment_metadata',$meta);105 } 106 } 107 echo json_encode( $R);205 $R[ $image['post']->ID ]['update_post_meta'] = update_post_meta( $image['post']->ID, '_wp_attachment_metadata', $meta ); 206 } 207 } 208 echo json_encode( $R ); 108 209 die; 109 210 } … … 114 215 * @return array|bool 115 216 */ 116 function _hw_io_getOrientFromImagePost( $postOrId, $returnOnlyOrientation = true){117 $post = get_post( $postOrId);118 if (!$post instanceof WP_Post) return false;119 if ($post->post_type != 'attachment') return false;120 $metaData = wp_get_attachment_metadata( $post->ID);121 if (!is_array($metaData) || !isset($metaData['image_meta']) || !isset($metaData['image_meta']['orientation'])) return false;217 function _hw_io_getOrientFromImagePost( $postOrId, $returnOnlyOrientation = true ){ 218 $post = get_post( $postOrId ); 219 if( !$post instanceof WP_Post ) return false; 220 if( $post->post_type != 'attachment' ) return false; 221 $metaData = wp_get_attachment_metadata( $post->ID ); 222 if( !is_array( $metaData ) || !isset( $metaData['image_meta'] ) || !isset( $metaData['image_meta']['orientation'] ) ) return false; 122 223 $upload_dir_arr = wp_upload_dir(); 123 return $returnOnlyOrientation ? $metaData['image_meta']['orientation'] : array(224 return $returnOnlyOrientation ? $metaData['image_meta']['orientation'] : [ 124 225 'path' => $upload_dir_arr['basedir'] . '/' . $metaData['file'], 125 226 'url' => $upload_dir_arr['baseurl'] . '/' . $metaData['file'], … … 127 228 'meta' => $metaData, 128 229 'orientation' => $metaData['image_meta']['orientation'] 129 );130 } 230 ]; 231 }
Note: See TracChangeset
for help on using the changeset viewer.