web/wp-admin/includes/image-edit.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    37 	<table id="imgedit-panel-<?php echo $post_id; ?>"><tbody>
    37 	<table id="imgedit-panel-<?php echo $post_id; ?>"><tbody>
    38 	<tr><td>
    38 	<tr><td>
    39 	<div class="imgedit-menu">
    39 	<div class="imgedit-menu">
    40 		<div onclick="imageEdit.crop(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-crop disabled" title="<?php esc_attr_e( 'Crop' ); ?>"></div><?php
    40 		<div onclick="imageEdit.crop(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-crop disabled" title="<?php esc_attr_e( 'Crop' ); ?>"></div><?php
    41 
    41 
    42 	// On some setups GD library does not provide imagerotate() - Ticket #11536   
    42 	// On some setups GD library does not provide imagerotate() - Ticket #11536
    43 	if ( function_exists('imagerotate') ) { ?>
    43 	if ( function_exists('imagerotate') ) { ?>
    44 		<div class="imgedit-rleft"  onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)" title="<?php esc_attr_e( 'Rotate counter-clockwise' ); ?>"></div>
    44 		<div class="imgedit-rleft"  onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)" title="<?php esc_attr_e( 'Rotate counter-clockwise' ); ?>"></div>
    45 		<div class="imgedit-rright" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)" title="<?php esc_attr_e( 'Rotate clockwise' ); ?>"></div>
    45 		<div class="imgedit-rright" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)" title="<?php esc_attr_e( 'Rotate clockwise' ); ?>"></div>
    46 <?php } else {
    46 <?php } else {
    47 		$note_gdlib = esc_attr__('Image rotation is not supported by your web host (function imagerotate() is missing)');
    47 		$note_gdlib = esc_attr__('Image rotation is not supported by your web host (function imagerotate() is missing)');
    65 	<input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
    65 	<input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
    66 	<input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo $meta['width']; ?>" />
    66 	<input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo $meta['width']; ?>" />
    67 	<input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo $meta['height']; ?>" />
    67 	<input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo $meta['height']; ?>" />
    68 
    68 
    69 	<div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
    69 	<div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
    70 	<img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')" src="<?php echo admin_url('admin-ajax.php'); ?>?action=imgedit-preview&amp;_ajax_nonce=<?php echo $nonce; ?>&amp;postid=<?php echo $post_id; ?>&amp;rand=<?php echo rand(1, 99999); ?>" />
    70 	<img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')" src="<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>?action=imgedit-preview&amp;_ajax_nonce=<?php echo $nonce; ?>&amp;postid=<?php echo $post_id; ?>&amp;rand=<?php echo rand(1, 99999); ?>" />
    71 	</div>
    71 	</div>
    72 
    72 
    73 	<div class="imgedit-submit">
    73 	<div class="imgedit-submit">
    74 		<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button" value="<?php esc_attr_e( 'Cancel' ); ?>" />
    74 		<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button" value="<?php esc_attr_e( 'Cancel' ); ?>" />
    75 		<input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" disabled="disabled" class="button-primary imgedit-submit-btn" value="<?php esc_attr_e( 'Save' ); ?>" />
    75 		<input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" disabled="disabled" class="button-primary imgedit-submit-btn" value="<?php esc_attr_e( 'Save' ); ?>" />
    94 <?php if ( $can_restore ) { ?>
    94 <?php if ( $can_restore ) { ?>
    95 
    95 
    96 	<div class="imgedit-group-top">
    96 	<div class="imgedit-group-top">
    97 		<a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><strong><?php _e('Restore Original Image'); ?></strong></a>
    97 		<a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><strong><?php _e('Restore Original Image'); ?></strong></a>
    98 		<div class="imgedit-help">
    98 		<div class="imgedit-help">
    99 		<p><?php _e('Discard any changes and restore the original image.'); 
    99 		<p><?php _e('Discard any changes and restore the original image.');
   100 
   100 
   101 		if ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE )
   101 		if ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE )
   102 			_e(' Previously edited copies of the image will not be deleted.');
   102 			echo ' '.__('Previously edited copies of the image will not be deleted.');
   103 
   103 
   104 		?></p>
   104 		?></p>
   105 		<div class="imgedit-submit">
   105 		<div class="imgedit-submit">
   106 		<input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'restore')" class="button-primary" value="<?php esc_attr_e( 'Restore image' ); ?>" <?php echo $can_restore; ?> />
   106 		<input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'restore')" class="button-primary" value="<?php esc_attr_e( 'Restore image' ); ?>" <?php echo $can_restore; ?> />
   107 		</div>
   107 		</div>
   116 	<div class="imgedit-group-top">
   116 	<div class="imgedit-group-top">
   117 		<strong><?php _e('Image Crop'); ?></strong>
   117 		<strong><?php _e('Image Crop'); ?></strong>
   118 		<a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><?php _e('(help)'); ?></a>
   118 		<a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><?php _e('(help)'); ?></a>
   119 		<div class="imgedit-help">
   119 		<div class="imgedit-help">
   120 		<p><?php _e('The image can be cropped by clicking on it and dragging to select the desired part. While dragging the dimensions of the selection are displayed below.'); ?></p>
   120 		<p><?php _e('The image can be cropped by clicking on it and dragging to select the desired part. While dragging the dimensions of the selection are displayed below.'); ?></p>
   121 		<strong><?php _e('Keyboard shortcuts'); ?></strong>
   121 		<strong><?php _e('Keyboard Shortcuts'); ?></strong>
   122 		<ul>
   122 		<ul>
   123 		<li><?php _e('Arrow: move by 10px'); ?></li>
   123 		<li><?php _e('Arrow: move by 10px'); ?></li>
   124 		<li><?php _e('Shift + arrow: move by 1px'); ?></li>
   124 		<li><?php _e('Shift + arrow: move by 1px'); ?></li>
   125 		<li><?php _e('Ctrl + arrow: resize by 10px'); ?></li>
   125 		<li><?php _e('Ctrl + arrow: resize by 10px'); ?></li>
   126 		<li><?php _e('Ctrl + Shift + arrow: resize by 1px'); ?></li>
   126 		<li><?php _e('Ctrl + Shift + arrow: resize by 1px'); ?></li>
   190 
   190 
   191 	</td></tr>
   191 	</td></tr>
   192 	</tbody></table>
   192 	</tbody></table>
   193 	<div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
   193 	<div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
   194 	<script type="text/javascript">imageEdit.init(<?php echo $post_id; ?>);</script>
   194 	<script type="text/javascript">imageEdit.init(<?php echo $post_id; ?>);</script>
   195 	<div class="hidden" id="imgedit-leaving-<?php echo $post_id; ?>"><?php _e("There are unsaved changes that will be lost.  'OK' to continue, 'Cancel' to return to the Image Editor."); ?></div>
   195 	<div class="hidden" id="imgedit-leaving-<?php echo $post_id; ?>"><?php _e("There are unsaved changes that will be lost. 'OK' to continue, 'Cancel' to return to the Image Editor."); ?></div>
   196 	</div>
   196 	</div>
   197 <?php
   197 <?php
   198 }
       
   199 
       
   200 function load_image_to_edit($post_id, $mime_type, $size = 'full') {
       
   201 	$filepath = get_attached_file($post_id);
       
   202 
       
   203 	if ( $filepath && file_exists($filepath) ) {
       
   204 		if ( 'full' != $size && ( $data = image_get_intermediate_size($post_id, $size) ) )
       
   205 			$filepath = path_join( dirname($filepath), $data['file'] );
       
   206 	} elseif ( WP_Http_Fopen::test() ) {
       
   207 		$filepath = wp_get_attachment_url($post_id);
       
   208 	}
       
   209 
       
   210 	$filepath = apply_filters('load_image_to_edit_path', $filepath, $post_id, $size);
       
   211 	if ( empty($filepath) )
       
   212 		return false;
       
   213 
       
   214 	switch ( $mime_type ) {
       
   215 		case 'image/jpeg':
       
   216 			$image = imagecreatefromjpeg($filepath);
       
   217 			break;
       
   218 		case 'image/png':
       
   219 			$image = imagecreatefrompng($filepath);
       
   220 			break;
       
   221 		case 'image/gif':
       
   222 			$image = imagecreatefromgif($filepath);
       
   223 			break;
       
   224 		default:
       
   225 			$image = false;
       
   226 			break;
       
   227 	}
       
   228 	if ( is_resource($image) ) {
       
   229 		$image = apply_filters('load_image_to_edit', $image, $post_id, $size);
       
   230 		if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
       
   231 			imagealphablending($image, false);
       
   232 			imagesavealpha($image, true);
       
   233 		}
       
   234 	}
       
   235 	return $image;
       
   236 }
   198 }
   237 
   199 
   238 function wp_stream_image($image, $mime_type, $post_id) {
   200 function wp_stream_image($image, $mime_type, $post_id) {
   239 	$image = apply_filters('image_save_pre', $image, $post_id);
   201 	$image = apply_filters('image_save_pre', $image, $post_id);
   240 
   202 
   284 			$img = $rotated;
   246 			$img = $rotated;
   285 		}
   247 		}
   286 	}
   248 	}
   287 	return $img;
   249 	return $img;
   288 }
   250 }
   289 
       
   290 
   251 
   291 function _flip_image_resource($img, $horz, $vert) {
   252 function _flip_image_resource($img, $horz, $vert) {
   292 	$w = imagesx($img);
   253 	$w = imagesx($img);
   293 	$h = imagesy($img);
   254 	$h = imagesy($img);
   294 	$dst = wp_imagecreatetruecolor($w, $h);
   255 	$dst = wp_imagecreatetruecolor($w, $h);
   388 	return $img;
   349 	return $img;
   389 }
   350 }
   390 
   351 
   391 function stream_preview_image($post_id) {
   352 function stream_preview_image($post_id) {
   392 	$post = get_post($post_id);
   353 	$post = get_post($post_id);
   393 	@ini_set('memory_limit', '256M');
   354 	@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
   394 	$img = load_image_to_edit( $post_id, $post->post_mime_type, array(400, 400) );
   355 	$img = load_image_to_edit( $post_id, $post->post_mime_type, array(400, 400) );
   395 
   356 
   396 	if ( !is_resource($img) )
   357 	if ( !is_resource($img) )
   397 		return false;
   358 		return false;
   398 
   359 
   419 function wp_restore_image($post_id) {
   380 function wp_restore_image($post_id) {
   420 	$meta = wp_get_attachment_metadata($post_id);
   381 	$meta = wp_get_attachment_metadata($post_id);
   421 	$file = get_attached_file($post_id);
   382 	$file = get_attached_file($post_id);
   422 	$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
   383 	$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
   423 	$restored = false;
   384 	$restored = false;
   424 	$msg = '';
   385 	$msg = new stdClass;
   425 
   386 
   426 	if ( !is_array($backup_sizes) ) {
   387 	if ( !is_array($backup_sizes) ) {
   427 		$msg->error = __('Cannot load image metadata.');
   388 		$msg->error = __('Cannot load image metadata.');
   428 		return $msg;
   389 		return $msg;
   429 	}
   390 	}
   430 
   391 
   431 	$parts = pathinfo($file);
   392 	$parts = pathinfo($file);
   432 	$suffix = time() . rand(100, 999);
   393 	$suffix = time() . rand(100, 999);
   433 	$default_sizes = apply_filters( 'intermediate_image_sizes', array('large', 'medium', 'thumbnail') );
   394 	$default_sizes = get_intermediate_image_sizes();
   434 
   395 
   435 	if ( isset($backup_sizes['full-orig']) && is_array($backup_sizes['full-orig']) ) {
   396 	if ( isset($backup_sizes['full-orig']) && is_array($backup_sizes['full-orig']) ) {
   436 		$data = $backup_sizes['full-orig'];
   397 		$data = $backup_sizes['full-orig'];
   437 
   398 
   438 		if ( $parts['basename'] != $data['file'] ) {
   399 		if ( $parts['basename'] != $data['file'] ) {
   451 		$restored = update_attached_file($post_id, $restored_file);
   412 		$restored = update_attached_file($post_id, $restored_file);
   452 
   413 
   453 		$meta['file'] = _wp_relative_upload_path( $restored_file );
   414 		$meta['file'] = _wp_relative_upload_path( $restored_file );
   454 		$meta['width'] = $data['width'];
   415 		$meta['width'] = $data['width'];
   455 		$meta['height'] = $data['height'];
   416 		$meta['height'] = $data['height'];
   456 		list ( $uwidth, $uheight ) = wp_shrink_dimensions($meta['width'], $meta['height']);
   417 		list ( $uwidth, $uheight ) = wp_constrain_dimensions($meta['width'], $meta['height'], 128, 96);
   457 		$meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
   418 		$meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
   458 	}
   419 	}
   459 
   420 
   460 	foreach ( $default_sizes as $default_size ) {
   421 	foreach ( $default_sizes as $default_size ) {
   461 		if ( isset($backup_sizes["$default_size-orig"]) ) {
   422 		if ( isset($backup_sizes["$default_size-orig"]) ) {
   490 
   451 
   491 	return $msg;
   452 	return $msg;
   492 }
   453 }
   493 
   454 
   494 function wp_save_image($post_id) {
   455 function wp_save_image($post_id) {
   495 	$return = '';
   456 	$return = new stdClass;
   496 	$success = $delete = $scaled = $nocrop = false;
   457 	$success = $delete = $scaled = $nocrop = false;
   497 	$post = get_post($post_id);
   458 	$post = get_post($post_id);
   498 	@ini_set('memory_limit', '256M');
   459 	@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
   499 	$img = load_image_to_edit($post_id, $post->post_mime_type);
   460 	$img = load_image_to_edit($post_id, $post->post_mime_type);
   500 
   461 
   501 	if ( !is_resource($img) ) {
   462 	if ( !is_resource($img) ) {
   502 		$return->error = esc_js( __('Unable to create new image.') );
   463 		$return->error = esc_js( __('Unable to create new image.') );
   503 		return $return;
   464 		return $return;
   548 	if ( !is_array($backup_sizes) )
   509 	if ( !is_array($backup_sizes) )
   549 		$backup_sizes = array();
   510 		$backup_sizes = array();
   550 
   511 
   551 	// generate new filename
   512 	// generate new filename
   552 	$path = get_attached_file($post_id);
   513 	$path = get_attached_file($post_id);
   553 	$path_parts = pathinfo52( $path );
   514 	$path_parts = pathinfo( $path );
   554 	$filename = $path_parts['filename'];
   515 	$filename = $path_parts['filename'];
   555 	$suffix = time() . rand(100, 999);
   516 	$suffix = time() . rand(100, 999);
   556 
   517 
   557 	if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE &&
   518 	if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE &&
   558 		isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) {
   519 		isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) {
   596 
   557 
   597 		$meta['file'] = _wp_relative_upload_path($new_path);
   558 		$meta['file'] = _wp_relative_upload_path($new_path);
   598 		$meta['width'] = imagesx($img);
   559 		$meta['width'] = imagesx($img);
   599 		$meta['height'] = imagesy($img);
   560 		$meta['height'] = imagesy($img);
   600 
   561 
   601 		list ( $uwidth, $uheight ) = wp_shrink_dimensions($meta['width'], $meta['height']);
   562 		list ( $uwidth, $uheight ) = wp_constrain_dimensions($meta['width'], $meta['height'], 128, 96);
   602 		$meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
   563 		$meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
   603 
   564 
   604 		if ( $success && ('nothumb' == $target || 'all' == $target) ) {
   565 		if ( $success && ('nothumb' == $target || 'all' == $target) ) {
   605 			$sizes = apply_filters( 'intermediate_image_sizes', array('large', 'medium', 'thumbnail') );
   566 			$sizes = get_intermediate_image_sizes();
   606 			if ( 'nothumb' == $target )
   567 			if ( 'nothumb' == $target )
   607 				$sizes = array_diff( $sizes, array('thumbnail') );
   568 				$sizes = array_diff( $sizes, array('thumbnail') );
   608 		}
   569 		}
   609 
   570 
   610 		$return->fw = $meta['width'];
   571 		$return->fw = $meta['width'];
   662 	imagedestroy($img);
   623 	imagedestroy($img);
   663 
   624 
   664 	$return->msg = esc_js( __('Image saved') );
   625 	$return->msg = esc_js( __('Image saved') );
   665 	return $return;
   626 	return $return;
   666 }
   627 }
   667