• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP wp_save_image_file函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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);
     }
 }
开发者ID:rmccue,项目名称:wordpress-unit-tests,代码行数:35,代码来源:functions.php


示例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();
//.........这里部分代码省略.........
开发者ID:cybKIRA,项目名称:roverlink-updated,代码行数:101,代码来源:image-edit.php


示例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();
//.........这里部分代码省略.........
开发者ID:nuevomediagroup,项目名称:WordPress,代码行数:101,代码来源:image-edit.php


示例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);
     }
 }
开发者ID:CompositeUK,项目名称:clone.WordPress-Develop,代码行数:39,代码来源:functions.php


示例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);
 }
开发者ID:rmccue,项目名称:wordpress-unit-tests,代码行数:17,代码来源:deprecated.php


示例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 );
	}
开发者ID:staylor,项目名称:develop.svn.wordpress.org,代码行数:20,代码来源:deprecated.php


示例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);
//.........这里部分代码省略.........
开发者ID:bluedanbob,项目名称:wordpress,代码行数:101,代码来源:image-edit.php



注:本文中的wp_save_image_file函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP wp_save_nav_menu_items函数代码示例发布时间:2022-05-23
下一篇:
PHP wp_save_image函数代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap