本文整理汇总了PHP中wp_save_image_file函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_save_image_file函数的具体用法?PHP wp_save_image_file怎么用?PHP wp_save_image_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_save_image_file函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_wp_save_image_file
/**
* Test save image file and mime_types
* @ticket 6821
*/
public function test_wp_save_image_file()
{
include_once ABSPATH . 'wp-admin/includes/image-edit.php';
// Mime types
$mime_types = array('image/jpeg', 'image/gif', 'image/png');
// Test each image editor engine
$classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
foreach ($classes as $class) {
// If the image editor isn't available, skip it
if (!call_user_func(array($class, 'test'))) {
continue;
}
$filter = create_function('', "return '{$class}';");
add_filter('image_editor_class', $filter);
// Call wp_save_image_file
$img = WP_Image_Editor::get_instance(DIR_TESTDATA . '/images/canola.jpg');
// Save a file as each mime type, assert it works
foreach ($mime_types as $mime_type) {
$file = wp_tempnam();
$ret = wp_save_image_file($file, $img, $mime_type, 1);
$this->assertNotEmpty($ret);
$this->assertNotInstanceOf('WP_Error', $ret);
$this->assertEquals($mime_type, $this->get_mime_type($ret['path']));
// Clean up
@unlink($file);
@unlink($ret['path']);
}
// Clean up
unset($img);
}
}
示例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)
{
$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();
//.........这里部分代码省略.........
示例4: test_wp_save_image_file
/**
* Test save image file and mime_types
* @ticket 6821
*/
public function test_wp_save_image_file()
{
if (!extension_loaded('fileinfo')) {
$this->markTestSkipped('The fileinfo PHP extension is not loaded.');
}
include_once ABSPATH . 'wp-admin/includes/image-edit.php';
// Mime types
$mime_types = array('image/jpeg', 'image/gif', 'image/png');
// Test each image editor engine
$classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
foreach ($classes as $class) {
// If the image editor isn't available, skip it
if (!call_user_func(array($class, 'test'))) {
continue;
}
$img = new $class(DIR_TESTDATA . '/images/canola.jpg');
$loaded = $img->load();
// Save a file as each mime type, assert it works
foreach ($mime_types as $mime_type) {
if (!$img->supports_mime_type($mime_type)) {
continue;
}
$file = wp_tempnam();
$ret = wp_save_image_file($file, $img, $mime_type, 1);
$this->assertNotEmpty($ret);
$this->assertNotInstanceOf('WP_Error', $ret);
$this->assertEquals($mime_type, $this->get_mime_type($ret['path']));
// Clean up
unlink($file);
unlink($ret['path']);
}
// Clean up
unset($img);
}
}
示例5: test_wp_save_image_file_not_deprecated_with_wp_image_editor
/**
* Test that wp_save_image_file doesn't have a deprecated argument when passed a WP_Image_Editor
* @ticket 6821
*/
public function test_wp_save_image_file_not_deprecated_with_wp_image_editor()
{
// Call wp_save_image_file
include_once ABSPATH . 'wp-admin/includes/image-edit.php';
$file = wp_tempnam();
$img = WP_Image_Editor::get_instance(DIR_TESTDATA . '/images/canola.jpg');
wp_save_image_file($file, $img, 'image/jpeg', 1);
unset($img);
@unlink($file);
// Check if the arg was deprecated
$check = $this->was_deprecated('argument', 'wp_save_image_file');
$this->assertFalse($check);
}
示例6: test_wp_save_image_file_not_deprecated_with_wp_image_editor
/**
* Test that wp_save_image_file doesn't have a deprecated argument when passed a WP_Image_Editor
* @ticket 6821
*/
public function test_wp_save_image_file_not_deprecated_with_wp_image_editor() {
if ( !function_exists( 'imagejpeg' ) )
$this->markTestSkipped( 'jpeg support unavailable' );
// Call wp_save_image_file
include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
$file = wp_tempnam();
$img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
wp_save_image_file( $file, $img, 'image/jpeg', 1 );
unset( $img );
@unlink($file);
// Check if the arg was deprecated
$check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
$this->assertFalse( $check );
}
示例7: 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);
//.........这里部分代码省略.........