本文整理汇总了PHP中wp_load_image函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_load_image函数的具体用法?PHP wp_load_image怎么用?PHP wp_load_image使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_load_image函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: wp_crop_image
/**
* Crop an Image to a given size.
*
* @since 2.1.0
*
* @param string|int $src_file The source file or Attachment ID.
* @param int $src_x The start x position to crop from.
* @param int $src_y The start y position to crop from.
* @param int $src_w The width to crop.
* @param int $src_h The height to crop.
* @param int $dst_w The destination width.
* @param int $dst_h The destination height.
* @param int $src_abs Optional. If the source crop points are absolute.
* @param string $dst_file Optional. The destination file to write to.
* @return string New filepath on success, String error message on failure.
*/
function wp_crop_image($src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
if (is_numeric($src_file)) {
// Handle int as attachment ID
$src_file = get_attached_file($src_file);
}
$src = wp_load_image($src_file);
if (!is_resource($src)) {
return $src;
}
$dst = wp_imagecreatetruecolor($dst_w, $dst_h);
if ($src_abs) {
$src_w -= $src_x;
$src_h -= $src_y;
}
if (function_exists('imageantialias')) {
imageantialias($dst, true);
}
imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagedestroy($src);
// Free up memory
if (!$dst_file) {
$dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
}
$dst_file = preg_replace('/\\.[^\\.]+$/', '.jpg', $dst_file);
if (imagejpeg($dst, $dst_file, apply_filters('jpeg_quality', 90, 'wp_crop_image'))) {
return $dst_file;
} else {
return false;
}
}
示例2: image_resize
/**
* This function is almost equal to the image_resize (native function of wordpress)
*/
function image_resize($file, $max_w, $max_h, $crop = false, $far = false, $iar = false, $dest_path = null, $jpeg_quality = 90)
{
$image = wp_load_image($file);
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image);
}
$size = @getimagesize($file);
if (!$size) {
return new WP_Error('invalid_image', __('Could not read image size'), $file);
}
list($orig_w, $orig_h, $orig_type) = $size;
$dims = mf_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $far, $iar);
if (!$dims) {
$dims = array(0, 0, 0, 0, $orig_w, $orig_h, $orig_w, $orig_h);
}
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
$transparent = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
imagefilledrectangle($newimage, 0, 0, $dst_w, $dst_h, $transparent);
imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $orig_type && !imageistruecolor($image)) {
imagetruecolortopalette($newimage, false, imagecolorstotal($image));
}
// we don't need the original in memory anymore
imagedestroy($image);
$info = pathinfo($dest_path);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($dest_path, ".{$ext}");
$destfilename = "{$dir}/{$name}.{$ext}";
if (IMAGETYPE_GIF == $orig_type) {
if (!imagegif($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} elseif (IMAGETYPE_PNG == $orig_type) {
if (!imagepng($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} else {
// all other formats are converted to jpg
//Todo: add option for use progresive JPG
//imageinterlace($newimage, true); //Progressive JPG
if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
}
imagedestroy($newimage);
// Set correct file permissions
$stat = stat(dirname($destfilename));
$perms = $stat['mode'] & 0666;
//same permissions as parent folder, strip off the executable bits
@chmod($destfilename, $perms);
return $destfilename;
}
示例3: wp_crop_image
/**
* Crop an Image to a given size.
*
* @since 2.1.0
*
* @param string|int $src The source file or Attachment ID.
* @param int $src_x The start x position to crop from.
* @param int $src_y The start y position to crop from.
* @param int $src_w The width to crop.
* @param int $src_h The height to crop.
* @param int $dst_w The destination width.
* @param int $dst_h The destination height.
* @param int $src_abs Optional. If the source crop points are absolute.
* @param string $dst_file Optional. The destination file to write to.
* @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
*/
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
if (0 == $src_x && 0 == $src_y && $src_w == $dst_w && $src_h == $dst_h) {
return is_numeric($src) ? get_attached_file($src) : $src;
}
if (is_numeric($src)) {
// Handle int as attachment ID
$src_file = get_attached_file($src);
if (!file_exists($src_file)) {
// If the file doesn't exist, attempt a url fopen on the src link.
// This can occur with certain file replication plugins.
$post = get_post($src);
$image_type = $post->post_mime_type;
$src = load_image_to_edit($src, $post->post_mime_type, 'full');
} else {
$size = @getimagesize($src_file);
$image_type = $size ? $size['mime'] : '';
$src = wp_load_image($src_file);
}
} else {
$size = @getimagesize($src);
$image_type = $size ? $size['mime'] : '';
$src = wp_load_image($src);
}
if (!is_resource($src)) {
return new WP_Error('error_loading_image', $src, $src_file);
}
$dst = wp_imagecreatetruecolor($dst_w, $dst_h);
if ($src_abs) {
$src_w -= $src_x;
$src_h -= $src_y;
}
if (function_exists('imageantialias')) {
imageantialias($dst, true);
}
imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagedestroy($src);
// Free up memory
if (!$dst_file) {
$dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
}
if ('image/png' != $image_type) {
$dst_file = preg_replace('/\\.[^\\.]+$/', '.jpg', $dst_file);
}
// The directory containing the original file may no longer exist when
// using a replication plugin.
wp_mkdir_p(dirname($dst_file));
if ('image/png' == $image_type && imagepng($dst, $dst_file)) {
return $dst_file;
} elseif (imagejpeg($dst, $dst_file, apply_filters('jpeg_quality', 90, 'wp_crop_image'))) {
return $dst_file;
} else {
return false;
}
}
示例4: wp_crop_image
function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
if ( ctype_digit( $src_file ) ) // Handle int as attachment ID
$src_file = get_attached_file( $src_file );
$src = wp_load_image( $src_file );
if ( !is_resource( $src ))
return $src;
$dst = imagecreatetruecolor( $dst_w, $dst_h );
if ( $src_abs ) {
$src_w -= $src_x;
$src_h -= $src_y;
}
if (function_exists('imageantialias'))
imageantialias( $dst, true );
imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
if ( !$dst_file )
$dst_file = str_replace( basename( $src_file ), 'cropped-'.basename( $src_file ), $src_file );
$dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
if ( imagejpeg( $dst, $dst_file ) )
return $dst_file;
else
return false;
}
示例5: simulate_image_make_intermediate_size
/**
* Method for testing if an image WOULD be resized
* Simulates the validation that occurs prior to actually beginning the resize
* process that would negate the need for or prevent the image from being resized.
*
* We use image_make_intermediate_size() to create our images
* This method recreates that. See media.php
*
* @since 0.1.5
* @uses wp_load_image() WP function
* @uses image_resize_dimensions() WP function
*/
public static function simulate_image_make_intermediate_size($file, $width, $height, $crop = false)
{
// Begin image_make_intermediate_size($file, $width, $height, $crop=false)
if ($width || $height) {
// Begin image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 )
$image = wp_load_image($file);
if (!is_resource($image)) {
return FALSE;
}
//return new WP_Error( 'error_loading_image', $image, $file );
$size = @getimagesize($file);
if (!$size) {
return FALSE;
}
//return new WP_Error('invalid_image', __('Could not read image size'), $file);
list($orig_w, $orig_h, $orig_type) = $size;
$dims = image_resize_dimensions($orig_w, $orig_h, $width, $height, $crop);
// $max_w, $max_h
if (!$dims) {
return FALSE;
}
//return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') );
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
}
imagedestroy($image);
// Free up memory
// Return value of image_make_intermediate_size()
return array('file' => basename($file), 'width' => $dst_w, 'height' => $dst_h);
}
示例6: test_load_directory
/**
* Try loading a directory
*
* @ticket 17814
* @expectedDeprecated wp_load_image
*/
public function test_load_directory()
{
// First, test with deprecated wp_load_image function
$editor1 = wp_load_image(DIR_TESTDATA);
$this->assertNotInternalType('resource', $editor1);
$editor2 = wp_get_image_editor(DIR_TESTDATA);
$this->assertNotInternalType('resource', $editor2);
// Then, test with editors.
$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;
}
$editor = new $class(DIR_TESTDATA);
$loaded = $editor->load();
$this->assertInstanceOf('WP_Error', $loaded);
$this->assertEquals('error_loading_image', $loaded->get_error_code());
}
}
示例7: ifp_wp_crop_image
/**
* Crop an Image to a given size.
* Modified version of the function wp_crop_image located in
* wp-admin/includes/image.php
*
* Mixes in some picture detection and treatment from image_resize in
* wp-includes/media.php
*
* @param string|int $src_file The source file or Attachment ID.
* @param int $src_x The start x position to crop from.
* @param int $src_y The start y position to crop from.
* @param int $src_w The width to crop.
* @param int $src_h The height to crop.
* @param int $dst_w The destination width.
* @param int $dst_h The destination height.
* @param int $src_abs Optional. If the source crop points are absolute.
* @param string $dst_file Optional. The destination file to write to.
* @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
*/
function ifp_wp_crop_image($src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
if (is_numeric($src_file)) {
// Handle int as attachment ID
$src_file = get_attached_file($src_file);
}
$src = wp_load_image($src_file);
if (!is_resource($src)) {
return new WP_Error('error_loading_image', $src, $src_file);
}
/* Part from image_resize */
$size = @getimagesize($src_file);
if (!$size) {
return new WP_Error('invalid_image', __('Could not read image size'), $file);
}
list($orig_w, $orig_h, $orig_type) = $size;
/* End part from image_resize */
$dst = wp_imagecreatetruecolor($dst_w, $dst_h);
if ($src_abs) {
$src_w -= $src_x;
$src_h -= $src_y;
}
imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
/* Part from image_resize */
// convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor($src)) {
imagetruecolortopalette($dst, false, imagecolorstotal($src));
}
/* End part from image_resize */
imagedestroy($src);
// Free up memory
if (!$dst_file) {
$dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
}
$info_src = pathinfo($src_file);
$info_dst = pathinfo($dst_file);
$dir = $info_dst['dirname'];
$ext = $info_src['extension'];
// Keep the source extension
$name = wp_basename($dst_file, ".{$info_dst['extension']}");
$dst_file = "{$dir}/{$name}.{$ext}";
if (IMAGETYPE_GIF == $orig_type) {
$success = imagegif($dst, $dst_file);
} else {
if (IMAGETYPE_PNG == $orig_type) {
$success = imagepng($dst, $dst_file);
} else {
$dst_file = "{$dir}/{$name}.jpg";
$success = imagejpeg($dst, $dst_file, apply_filters('jpeg_quality', 90, 'wp_crop_image'));
}
}
imagedestroy($dst);
if ($success) {
/* Part from image_resize */
// Set correct file permissions
$stat = stat(dirname($dst_file));
$perms = $stat['mode'] & 0666;
//same permissions as parent folder, strip off the executable bits
@chmod($dst_file, $perms);
/* End part from image_resize */
return $dst_file;
} else {
return false;
}
}
示例8: wpcf_image_resize
/**
* Copy from wp-includes/media.php
* Scale down an image to fit a particular size and save a new copy of the image.
*
* The PNG transparency will be preserved using the function, as well as the
* image type. If the file going in is PNG, then the resized image is going to
* be PNG. The only supported image types are PNG, GIF, and JPEG.
*
* Some functionality requires API to exist, so some PHP version may lose out
* support. This is not the fault of WordPress (where functionality is
* downgraded, not actual defects), but of your PHP version.
*
* @since 2.5.0
*
* @param string $file Image file path.
* @param int $max_w Maximum width to resize to.
* @param int $max_h Maximum height to resize to.
* @param bool $crop Optional. Whether to crop image or resize.
* @param string $suffix Optional. File suffix.
* @param string $dest_path Optional. New image file path.
* @param int $jpeg_quality Optional, default is 90. Image quality percentage.
* @return mixed WP_Error on failure. String with new destination path.
*/
function wpcf_image_resize($file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90)
{
$image = wp_load_image($file);
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image, $file);
}
$size = @getimagesize($file);
if (!$size) {
return new WP_Error('invalid_image', __('Could not read image size', 'wpcf'), $file);
}
list($orig_w, $orig_h, $orig_type) = $size;
$dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
if (!$dims) {
return new WP_Error('error_getting_dimensions', __('Could not calculate resized image dimensions', 'wpcf'));
}
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = wp_imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor($image)) {
imagetruecolortopalette($newimage, false, imagecolorstotal($image));
}
// we don't need the original in memory anymore
imagedestroy($image);
// $suffix will be appended to the destination filename, just before the extension
if (!$suffix) {
$suffix = "{$dst_w}x{$dst_h}";
}
$info = pathinfo($file);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = wpcf_basename($file, ".{$ext}");
// use fix here for windows
if (!is_null($dest_path) and $_dest_path = realpath($dest_path)) {
$dir = $_dest_path;
}
$destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
if (IMAGETYPE_GIF == $orig_type) {
if (!imagegif($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid', 'wpcf'));
}
} elseif (IMAGETYPE_PNG == $orig_type) {
if (!imagepng($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid', 'wpcf'));
}
} else {
// all other formats are converted to jpg
if ('jpg' != $ext && 'jpeg' != $ext) {
$destfilename = "{$dir}/{$name}-{$suffix}.jpg";
}
if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) {
return new WP_Error('resize_path_invalid', __('Resize path invalid', 'wpcf'));
}
}
imagedestroy($newimage);
// Set correct file permissions
$stat = stat(dirname($destfilename));
$perms = $stat['mode'] & 0666;
//same permissions as parent folder, strip off the executable bits
@chmod($destfilename, $perms);
return $destfilename;
}
示例9: pte_resize_images
function pte_resize_images()
{
$logger = PteLogger::singleton();
global $pte_sizes;
// Require JSON output
pte_require_json();
$id = pte_check_id($_GET['id']);
$w = pte_check_int($_GET['w']);
$h = pte_check_int($_GET['h']);
$x = pte_check_int($_GET['x']);
$y = pte_check_int($_GET['y']);
if ($id === false || $w === false || $h === false || $x === false || $y === false) {
return pte_json_error("ResizeImages initialization failed: '{$id}-{$w}-{$h}-{$x}-{$y}'");
}
// Get the sizes to process
$pte_sizes = $_GET['pte-sizes'];
$sizes = pte_get_all_alternate_size_information($id);
// The following information is common to all sizes
// *** common-info
$dst_x = 0;
$dst_y = 0;
$original_file = get_attached_file($id);
$original_image = wp_load_image($original_file);
$original_size = @getimagesize($original_file);
$uploads = wp_upload_dir();
$PTE_TMP_DIR = $uploads['basedir'] . DIRECTORY_SEPARATOR . "ptetmp" . DIRECTORY_SEPARATOR;
$PTE_TMP_URL = $uploads['baseurl'] . "/ptetmp/";
if (!$original_size) {
return pte_json_error("Could not read image size");
}
$logger->debug("BASE FILE DIMENSIONS/INFO: " . print_r($original_size, true));
list($orig_w, $orig_h, $orig_type) = $original_size;
// *** End common-info
foreach ($sizes as $size => $data) {
// Get all the data needed to run image_create
//
// $dst_w, $dst_h
extract(pte_get_width_height($data, $w, $h));
//
// Set the directory
$basename = pte_generate_filename($original_file, $dst_w, $dst_h);
// Set the file and URL's - defines set in pte_ajax
$tmpfile = "{$PTE_TMP_DIR}{$id}" . DIRECTORY_SEPARATOR . "{$basename}";
$tmpurl = "{$PTE_TMP_URL}{$id}/{$basename}";
// === CREATE IMAGE ===================
$image = pte_create_image($original_image, $orig_type, $dst_x, $dst_y, $dst_w, $dst_h, $x, $y, $w, $h);
if (!isset($image)) {
$logger->error("Error creating image: {$size}");
continue;
}
if (!pte_write_image($image, $orig_type, $tmpfile)) {
$logger->error("Error writing image: {$size} to '{$tmpfile}'");
continue;
}
// === END CREATE IMAGE ===============
// URL: wp_upload_dir => base_url/subdir + /basename of $tmpfile
// This is for the output
$thumbnails[$size]['url'] = $tmpurl;
$thumbnails[$size]['file'] = basename($tmpfile);
}
// we don't need the original in memory anymore
imagedestroy($original_image);
// Did you process anything?
if (count($thumbnails) < 1) {
return pte_json_error("No images processed");
}
return pte_json_encode(array('thumbnails' => $thumbnails, 'pte-nonce' => wp_create_nonce("pte-{$id}"), 'pte-delete-nonce' => wp_create_nonce("pte-delete-{$id}")));
}
示例10: catch_upload
/**
* Catch the file upload and parse it
*
* @since 0.1
*
* @param int $post_id
* @return string The filepath
*/
public static function catch_upload($post_id)
{
if (!wp_attachment_is_image($post_id)) {
return;
}
// Get default options for Large Size Media and options
$max_width = (int) get_option('large_size_w', 1024);
$max_height = (int) get_option('large_size_h', 1024);
$crop = (bool) get_option('media_features_crop', false);
$resizes = (bool) get_option('media_features_resize', false);
// If option is not enabled return
if (!$resizes) {
return;
}
// Let's see if these sizes are reasonable
if ($max_width < 0 || $max_height < 0) {
return;
}
// Get file and image GD resource
$file = get_attached_file($post_id);
$image = wp_load_image($file);
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image, $file);
}
// Get real dimensions from file image
list($orig_width, $orig_height, $orig_type) = @getimagesize($file);
// if the image needs to be cropped, resize it
if ($max_width >= $orig_width && $max_height >= $orig_height) {
return;
}
//var_dump( $file, $orig_width, $orig_height, $orig_type, $max_width, $max_height );
$dims = image_resize_dimensions($orig_width, $orig_height, $max_width, $max_height, $crop);
if (!$dims) {
return new WP_Error('error_getting_dimensions', __('Could not calculate resized image dimensions', 'media-features'));
}
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = wp_imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor($image)) {
imagetruecolortopalette($newimage, false, imagecolorstotal($image));
}
// we don't need the original in memory anymore
imagedestroy($image);
extract(pathinfo($file));
$name = wp_basename($file, ".{$extension}");
//var_dump( $name, $dirname, $extension );
$dirname = realpath($dirname);
$dest_file = "{$dirname}/{$name}.{$extension}";
// If the file is not writable should not proceed
if (!is_writeable($dest_file)) {
return;
}
// Save to file based on original type
$did_save = false;
switch ($orig_type) {
case IMAGETYPE_GIF:
imagegif($newimage, $dest_file);
break;
case IMAGETYPE_PNG:
imagepng($newimage, $dest_file);
break;
case IMAGETYPE_JPEG:
case IMAGETYPE_JPEG2000:
imagejpeg($newimage, $dest_file, apply_filters('jpeg_quality', 90, 'image_resize'));
break;
default:
$dest_file = "{$dirname}/{$name}.{$extension}";
}
imagedestroy($newimage);
// Set correct file permissions
$stat = stat(dirname($dest_file));
$perms = $stat['mode'] & 0666;
//same permissions as parent folder, strip off the executable bits
@chmod($dest_file, $perms);
return $dest_file;
}
示例11: ocmx_custom_resize
function ocmx_custom_resize($file, $max_w = 0, $max_h = 0, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 100)
{
$image = wp_load_image($file);
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image, $file);
}
$size = @getimagesize($file);
if (!$size) {
return new WP_Error('invalid_image', __('Could not read image size'), $file);
}
list($orig_w, $orig_h, $orig_type) = $size;
if ($max_h == 0) {
$max_h = $orig_h;
}
if ($max_w == 0) {
$max_w = $orig_w;
}
if ($orig_w > $max_w || $orig_h > $max_h) {
$dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
}
if (!$dims) {
$info = pathinfo($file);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($file, ".{$ext}");
if (!is_null($dest_path) and $_dest_path = realpath($dest_path)) {
$dir = $_dest_path;
}
if (strpos($dest_path, "ocmx") !== false) {
copy($file, "{$dir}/{$name}.{$ext}");
}
} else {
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = wp_imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor($image)) {
imagetruecolortopalette($newimage, false, imagecolorstotal($image));
}
// we don't need the original in memory anymore
imagedestroy($image);
$info = pathinfo($file);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($file, ".{$ext}");
if (!is_null($dest_path) and $_dest_path = realpath($dest_path)) {
$dir = $_dest_path;
}
$destfilename = "{$dir}/{$name}.{$ext}";
if (IMAGETYPE_GIF == $orig_type) {
if (!imagegif($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} elseif (IMAGETYPE_PNG == $orig_type) {
if (!imagepng($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} else {
// all other formats are converted to jpg
$destfilename = "{$dir}/{$name}.jpg";
if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
}
imagedestroy($newimage);
// Set correct file permissions
$stat = stat(dirname($destfilename));
$perms = $stat['mode'] & 0666;
//same permissions as parent folder, strip off the executable bits
@chmod($destfilename, $perms);
}
return $destfilename;
}
示例12: boilerstrap_sharpen_resized_files
/**
* Sharpen resized JPG's so they don't get blurry
*/
function boilerstrap_sharpen_resized_files($resized_file)
{
$image = wp_load_image($resized_file);
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image, $file);
}
$size = @getimagesize($resized_file);
if (!$size) {
return new WP_Error('invalid_image', __('Could not read image size'), $file);
}
list($orig_w, $orig_h, $orig_type) = $size;
switch ($orig_type) {
case IMAGETYPE_JPEG:
$matrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1));
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($image, $matrix, $divisor, $offset);
imagejpeg($image, $resized_file, apply_filters('jpeg_quality', 90, 'edit_image'));
break;
case IMAGETYPE_PNG:
return $resized_file;
case IMAGETYPE_GIF:
return $resized_file;
}
return $resized_file;
}
示例13: resize_image
public static function resize_image($file, $max_w = 0, $max_h = 0, $crop = true, $suffix = null, $dest_path = null, $jpeg_quality = 90)
{
it_classes_load('it-file-utility.php');
if (is_numeric($file)) {
$file_info = ITFileUtility::get_file_attachment($file);
if (false === $file_info) {
return new WP_Error('error_loading_image_attachment', "Could not find requested file attachment ({$file})");
}
$file = $file_info['file'];
} else {
$file_attachment_id = '';
}
if (preg_match('/\\.ico$/', $file)) {
return array('file' => $file, 'url' => ITFileUtility::get_url_from_file($file), 'name' => basename($file));
}
if (version_compare($GLOBALS['wp_version'], '3.4.9', '>')) {
// Compat code taken from pre-release 3.5.0 code.
if (!file_exists($file)) {
return new WP_Error('error_loading_image', sprintf(__('File “%s” doesn’t exist?'), $file));
}
if (!function_exists('imagecreatefromstring')) {
return new WP_Error('error_loading_image', __('The GD image library is not installed.'));
}
// Set artificially high because GD uses uncompressed images in memory
@ini_set('memory_limit', apply_filters('image_memory_limit', WP_MAX_MEMORY_LIMIT));
$image = imagecreatefromstring(file_get_contents($file));
if (!is_resource($image)) {
return new WP_Error('error_loading_image', sprintf(__('File “%s” is not an image.'), $file));
}
} else {
require_once ABSPATH . 'wp-admin/includes/image.php';
$image = wp_load_image($file);
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image);
}
}
list($orig_w, $orig_h, $orig_type) = getimagesize($file);
$dims = ITImageUtility::_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
if (!$dims) {
return new WP_Error('error_resizing_image', "Could not resize image");
}
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
if ($orig_w == $dst_w && $orig_h == $dst_h) {
return array('file' => $file, 'url' => ITFileUtility::get_url_from_file($file), 'name' => basename($file));
}
if (!$suffix) {
$suffix = "resized-image-{$dst_w}x{$dst_h}";
}
$info = pathinfo($file);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($file, ".{$ext}");
if (!is_null($dest_path) && ($_dest_path = realpath($dest_path))) {
$dir = $_dest_path;
}
$destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
if (file_exists($destfilename)) {
if (filemtime($file) > filemtime($destfilename)) {
unlink($destfilename);
} else {
return array('file' => $destfilename, 'url' => ITFileUtility::get_url_from_file($destfilename), 'name' => basename($destfilename));
}
}
// ImageMagick cannot resize animated PNG files yet, so this only works for
// animated GIF files.
$animated = false;
if (ITImageUtility::is_animated_gif($file)) {
$coalescefilename = "{$dir}/{$name}-coalesced-file.{$ext}";
if (!file_exists($coalescefilename)) {
system("convert {$file} -coalesce {$coalescefilename}");
}
if (file_exists($coalescefilename)) {
system("convert -crop {$src_w}x{$src_h}+{$src_x}+{$src_y}! {$coalescefilename} {$destfilename}");
if (file_exists($destfilename)) {
system("mogrify -resize {$dst_w}x{$dst_h} {$destfilename}");
system("convert -layers optimize {$destfilename}");
$animated = true;
}
}
}
if (!$animated) {
$newimage = imagecreatetruecolor($dst_w, $dst_h);
// preserve PNG transparency
if (IMAGETYPE_PNG == $orig_type && function_exists('imagealphablending') && function_exists('imagesavealpha')) {
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
}
imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// we don't need the original in memory anymore
if ($orig_type == IMAGETYPE_GIF) {
if (!imagegif($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} elseif ($orig_type == IMAGETYPE_PNG) {
if (!imagepng($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} else {
// all other formats are converted to jpg
$destfilename = "{$dir}/{$name}-{$suffix}.jpg";
//.........这里部分代码省略.........
示例14: _dp_resize_logo
function _dp_resize_logo()
{
$dp_resize_message = array('error' => true, 'message' => '');
//値をチェック
$ratio = intval($_REQUEST['dp_resize_ratio']);
if (!($ratio > 0 && $ratio <= 100)) {
$ratio = 100;
}
$orignal_to_display_ratio = (double) $_REQUEST['dp_logo_to_resize_ratio'];
$width = round($_REQUEST['dp_logo_resize_width'] / $orignal_to_display_ratio);
$height = round($_REQUEST['dp_logo_resize_height'] / $orignal_to_display_ratio);
$top = round($_REQUEST['dp_logo_resize_top'] / $orignal_to_display_ratio);
$left = round($_REQUEST['dp_logo_resize_left'] / $orignal_to_display_ratio);
$new_width = round($width * $ratio / 100);
$new_height = round($height * $ratio / 100);
//画像を読み込んでみる
$info = dp_logo_info();
if (!$info) {
$dp_resize_message['message'] = __('Logo file not exists.', 'tcd-w');
return $dp_resize_message;
}
// 保存ファイル名を決める
$path = preg_replace("/logo\\.(png|gif|jpe?g)\$/i", "logo-resized.\$1", $info['path']);
// 3.5以前と以降で処理を分岐
try {
// 3.5以降はwp_get_image_editorが存在する
if (function_exists('wp_get_image_editor')) {
// 新APIを利用
$orig_image = wp_get_image_editor($info['path']);
// 読み込み失敗ならエラー
if (is_wp_error($orig_image)) {
throw new Exception(__('Logo file not exists.', 'tcd-w'));
}
// リサイズしてダメだったらエラー
$size = $orig_image->get_size();
$resize_result = $orig_image->resize(round($size['width'] * $ratio / 100), round($size['height'] * $ratio / 100));
if (is_wp_error($resize_result)) {
// WordPressが返すエラーメッセージを利用
// 適宜変更してください。
throw new Exception($resize_result->get_error_message());
}
// 切り抜きしてダメだったらエラー
$crop_result = $orig_image->crop(round($left * $ratio / 100), round($top * $ratio / 100), $new_width, $new_height);
if (is_wp_error($crop_result)) {
// WordPressが返すエラーメッセージを利用
// 適宜変更してください。
throw new Exception($crop_result->get_error_message());
}
// 保存してダメだったらエラー
// 基本は上書きOK.
$save_result = $orig_image->save($path);
if (is_wp_error($save_result)) {
// WordPressが返すエラーメッセージを利用
// 適宜変更してください。
throw new Exception($save_result->get_error_message());
}
} else {
// それ以前は昔の方法で行う
$orig_image = wp_load_image($info['path']);
// 画像を読み込めなかったらエラー
if (!is_resource($orig_image)) {
throw new Exception(__('Logo file not exists.', 'tcd-w'));
}
$newimage = wp_imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($newimage, $orig_image, 0, 0, $left, $top, $new_width, $new_height, $width, $height);
if (IMAGETYPE_PNG == $info['mime'] && function_exists('imageistruecolor')) {
@imagetruecolortopalette($newimage, false, imagecolorstotal($orig_image));
}
imagedestroy($orig_image);
//ファイルを保存する前に削除
$dest_path = dp_logo_exists(true);
if ($dest_path && !@unlink($dest_path)) {
throw new Exception('Cannot delete existing resized logo.', 'tcd-w');
}
//名前を決めて保存
$result = null;
if (IMAGETYPE_GIF == $info['mime']) {
$result = imagegif($newimage, $path);
} elseif (IMAGETYPE_PNG == $info['mime']) {
$result = imagepng($newimage, $path);
} else {
$result = imagejpeg($newimage, $path, 100);
}
imagedestroy($newimage);
if (!$result) {
throw new Exception(sprintf(__('Failed to save resized logo. Please check permission of <code>%s</code>', 'tcd-w'), dp_logo_basedir()));
}
}
} catch (Exception $e) {
// 上記処理中で一回でも例外が発生したら、エラーを返す
$dp_resize_message['message'] = $e->getMessage();
return $dp_resize_message;
}
// ここまで来たということはエラーなし
$dp_resize_message['error'] = false;
$dp_resize_message['message'] = __('Logo image is successfully resized.', 'tcd-w');
return $dp_resize_message;
}
示例15: image_upsize
function image_upsize($file, $max_w, $max_h, $color = null, $suffix = null, $dest_path = null, $jpeg_quality = 90)
{
$image = wp_load_image($file);
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image, $file);
}
$size = @getimagesize($file);
if (!$size) {
return new WP_Error('invalid_image', __('Could not read image size'), $file);
}
list($orig_w, $orig_h, $orig_type) = $size;
$dst_x = (int) ($max_w / 2) - $orig_w / 2;
$dst_y = (int) ($max_h / 2) - $orig_h / 2;
$src_x = 0;
$src_y = 0;
$dst_w = $max_w;
$dst_h = $max_h;
$src_w = $orig_w;
$src_h = $orig_h;
$newimage = wp_imagecreatetruecolor($dst_w, $dst_h);
if (!empty($color)) {
$r = base_convert(substr($color, 0, 2), 16, 10);
$g = base_convert(substr($color, 2, 2), 16, 10);
$b = base_convert(substr($color, 4, 2), 16, 10);
$background = imagecolorallocate($newimage, $r, $g, $b);
imagefill($newimage, 0, 0, $background);
}
imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $src_w, $src_h);
// Convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor($image)) {
imagetruecolortopalette($newimage, false, imagecolorstotal($image));
}
// We don't need the original in memory anymore.
imagedestroy($image);
// $suffix will be appended to the destination filename, just before the extension.
if (!$suffix) {
$suffix = "{$dst_w}x{$dst_h}";
}
$info = pathinfo($file);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($file, ".{$ext}");
if (!is_null($dest_path) && ($_dest_path = realpath($dest_path))) {
$dir = $_dest_path;
}
$destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
if (IMAGETYPE_GIF == $orig_type) {
if (!imagegif($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} elseif (IMAGETYPE_PNG == $orig_type) {
if (!imagepng($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} else {
// All other formats are converted to jpg.
$destfilename = "{$dir}/{$name}-{$suffix}.jpg";
if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
}
imagedestroy($newimage);
// Set correct file permissions.
$stat = stat(dirname($destfilename));
$perms = $stat['mode'] & 0666;
// Same permissions as parent folder, strip off the executable bits.
@chmod($destfilename, $perms);
// Delete old image.
unlink($file);
return $destfilename;
}