本文整理汇总了PHP中image_edit_apply_changes函数的典型用法代码示例。如果您正苦于以下问题:PHP image_edit_apply_changes函数的具体用法?PHP image_edit_apply_changes怎么用?PHP image_edit_apply_changes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了image_edit_apply_changes函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: wp_save_image
function wp_save_image($post_id)
{
$return = new stdClass();
$success = $delete = $scaled = $nocrop = false;
$post = get_post($post_id);
@ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT));
$img = load_image_to_edit($post_id, $post->post_mime_type);
if (!is_resource($img)) {
$return->error = esc_js(__('Unable to create new image.'));
return $return;
}
$fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;
$fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;
$target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';
$scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do'];
if ($scale && $fwidth > 0 && $fheight > 0) {
$sX = imagesx($img);
$sY = imagesy($img);
// check if it has roughly the same w / h ratio
$diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2);
if (-0.1 < $diff && $diff < 0.1) {
// scale the full size image
$dst = wp_imagecreatetruecolor($fwidth, $fheight);
if (imagecopyresampled($dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY)) {
imagedestroy($img);
$img = $dst;
$scaled = true;
}
}
if (!$scaled) {
$return->error = esc_js(__('Error while saving the scaled image. Please reload the page and try again.'));
return $return;
}
} elseif (!empty($_REQUEST['history'])) {
$changes = json_decode(stripslashes($_REQUEST['history']));
if ($changes) {
$img = image_edit_apply_changes($img, $changes);
}
} else {
$return->error = esc_js(__('Nothing to save, the image has not changed.'));
return $return;
}
$meta = wp_get_attachment_metadata($post_id);
$backup_sizes = get_post_meta($post->ID, '_wp_attachment_backup_sizes', true);
if (!is_array($meta)) {
$return->error = esc_js(__('Image data does not exist. Please re-upload the image.'));
return $return;
}
if (!is_array($backup_sizes)) {
$backup_sizes = array();
}
// generate new filename
$path = get_attached_file($post_id);
$path_parts = pathinfo($path);
$filename = $path_parts['filename'];
$suffix = time() . rand(100, 999);
if (defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename']) {
if ('thumbnail' == $target) {
$new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";
} else {
$new_path = $path;
}
} else {
while (true) {
$filename = preg_replace('/-e([0-9]+)$/', '', $filename);
$filename .= "-e{$suffix}";
$new_filename = "{$filename}.{$path_parts['extension']}";
$new_path = "{$path_parts['dirname']}/{$new_filename}";
if (file_exists($new_path)) {
$suffix++;
} else {
break;
}
}
}
// save the full-size file, also needed to create sub-sizes
if (!wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id)) {
$return->error = esc_js(__('Unable to save the image.'));
return $return;
}
if ('nothumb' == $target || 'all' == $target || 'full' == $target || $scaled) {
$tag = false;
if (isset($backup_sizes['full-orig'])) {
if ((!defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE) && $backup_sizes['full-orig']['file'] != $path_parts['basename']) {
$tag = "full-{$suffix}";
}
} else {
$tag = 'full-orig';
}
if ($tag) {
$backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
}
$success = update_attached_file($post_id, $new_path);
$meta['file'] = _wp_relative_upload_path($new_path);
$meta['width'] = imagesx($img);
$meta['height'] = imagesy($img);
list($uwidth, $uheight) = wp_constrain_dimensions($meta['width'], $meta['height'], 128, 96);
$meta['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
if ($success && ('nothumb' == $target || 'all' == $target)) {
$sizes = get_intermediate_image_sizes();
//.........这里部分代码省略.........
示例2: wp_save_image
/**
* Saves image to post along with enqueued changes
* in $_REQUEST['history']
*
* @param int $post_id
* @return \stdClass
*/
function wp_save_image($post_id)
{
global $_wp_additional_image_sizes;
$return = new stdClass();
$success = $delete = $scaled = $nocrop = false;
$post = get_post($post_id);
$img = wp_get_image_editor(_load_image_to_edit_path($post_id, 'full'));
if (is_wp_error($img)) {
$return->error = esc_js(__('Unable to create new image.'));
return $return;
}
$fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;
$fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;
$target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';
$scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do'];
if ($scale && $fwidth > 0 && $fheight > 0) {
$size = $img->get_size();
$sX = $size['width'];
$sY = $size['height'];
// Check if it has roughly the same w / h ratio.
$diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2);
if (-0.1 < $diff && $diff < 0.1) {
// Scale the full size image.
if ($img->resize($fwidth, $fheight)) {
$scaled = true;
}
}
if (!$scaled) {
$return->error = esc_js(__('Error while saving the scaled image. Please reload the page and try again.'));
return $return;
}
} elseif (!empty($_REQUEST['history'])) {
$changes = json_decode(wp_unslash($_REQUEST['history']));
if ($changes) {
$img = image_edit_apply_changes($img, $changes);
}
} else {
$return->error = esc_js(__('Nothing to save, the image has not changed.'));
return $return;
}
$meta = wp_get_attachment_metadata($post_id);
$backup_sizes = get_post_meta($post->ID, '_wp_attachment_backup_sizes', true);
if (!is_array($meta)) {
$return->error = esc_js(__('Image data does not exist. Please re-upload the image.'));
return $return;
}
if (!is_array($backup_sizes)) {
$backup_sizes = array();
}
// Generate new filename.
$path = get_attached_file($post_id);
$path_parts = pathinfo($path);
$filename = $path_parts['filename'];
$suffix = time() . rand(100, 999);
if (defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename']) {
if ('thumbnail' == $target) {
$new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";
} else {
$new_path = $path;
}
} else {
while (true) {
$filename = preg_replace('/-e([0-9]+)$/', '', $filename);
$filename .= "-e{$suffix}";
$new_filename = "{$filename}.{$path_parts['extension']}";
$new_path = "{$path_parts['dirname']}/{$new_filename}";
if (file_exists($new_path)) {
$suffix++;
} else {
break;
}
}
}
// Save the full-size file, also needed to create sub-sizes.
if (!wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id)) {
$return->error = esc_js(__('Unable to save the image.'));
return $return;
}
if ('nothumb' == $target || 'all' == $target || 'full' == $target || $scaled) {
$tag = false;
if (isset($backup_sizes['full-orig'])) {
if ((!defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE) && $backup_sizes['full-orig']['file'] != $path_parts['basename']) {
$tag = "full-{$suffix}";
}
} else {
$tag = 'full-orig';
}
if ($tag) {
$backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
}
$success = $path === $new_path || update_attached_file($post_id, $new_path);
$meta['file'] = _wp_relative_upload_path($new_path);
$size = $img->get_size();
//.........这里部分代码省略.........
示例3: wp_save_image
function wp_save_image($post_id)
{
$msg = '';
$success = $delete = $full_resized = false;
$post = get_post($post_id);
@ini_set('memory_limit', '256M');
$img = load_image_to_edit($post);
if (!is_resource($img)) {
return 'error=' . __('Unable to create new image.');
}
$fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;
$fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;
$target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';
if (!empty($_REQUEST['history'])) {
$changes = json_decode(stripslashes($_REQUEST['history']));
if ($changes) {
$img = image_edit_apply_changes($img, $changes);
}
}
if ($fwidth > 0 && $fheight > 0) {
// scale the full size image
$dst = wp_imagecreatetruecolor($fwidth, $fheight);
if (imagecopyresampled($dst, $img, 0, 0, 0, 0, $fwidth, $fheight, imagesx($img), imagesy($img))) {
imagedestroy($img);
$img = $dst;
$full_resized = true;
}
}
if (!$changes && !$full_resized) {
return 'error=' . __('Nothing to save, the image is not changed.');
}
$meta = wp_get_attachment_metadata($post_id, false, false);
if (!is_array($meta)) {
$meta = array();
}
if (!isset($meta['sizes']) || !is_array($meta['sizes'])) {
$meta['sizes'] = array();
}
// generate new filename
$path = get_attached_file($post_id);
$path_parts = pathinfo52($path);
$filename = $path_parts['filename'];
$suffix = time() . rand(100, 999);
while (true) {
$filename = preg_replace('/-e([0-9]+)$/', '', $filename);
$filename .= "-e{$suffix}";
$new_filename = "{$filename}.{$path_parts['extension']}";
$new_path = "{$path_parts['dirname']}/{$new_filename}";
if (file_exists($new_path)) {
$suffix++;
} else {
break;
}
}
// save the full-size file, also needed to create sub-sizes
if (!wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id)) {
return 'error=' . __('Unable to save the image.');
}
if ('full' == $target || 'all' == $target || $full_resized) {
$meta['sizes']["backup-{$suffix}-full"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
$success = update_attached_file($post_id, $new_path);
$meta['file'] = get_attached_file($post_id, true);
// get the path unfiltered
$meta['width'] = imagesx($img);
$meta['height'] = imagesy($img);
list($uwidth, $uheight) = wp_shrink_dimensions($meta['width'], $meta['height']);
$meta['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
if ($success && $target == 'all') {
$sizes = apply_filters('intermediate_image_sizes', array('large', 'medium', 'thumbnail'));
}
$msg .= "full={$meta['width']}x{$meta['height']}!";
} elseif (array_key_exists($target, $meta['sizes'])) {
$sizes = array($target);
$success = $delete = true;
}
if (isset($sizes)) {
foreach ($sizes as $size) {
if (isset($meta['sizes'][$size])) {
$meta['sizes']["backup-{$suffix}-{$size}"] = $meta['sizes'][$size];
}
$resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop"));
if ($resized) {
$meta['sizes'][$size] = $resized;
} else {
unset($meta['sizes'][$size]);
}
}
}
if ($success) {
wp_update_attachment_metadata($post_id, $meta);
if ($target == 'thumbnail' || $target == 'all' || $target == 'full' && !array_key_exists('thumbnail', $meta['sizes'])) {
if ($thumb_url = get_attachment_icon_src($post_id)) {
$msg .= "thumbnail={$thumb_url[0]}";
}
}
} else {
$delete = true;
}
if ($delete) {
$delpath = apply_filters('wp_delete_file', $new_path);
//.........这里部分代码省略.........