本文整理汇总了PHP中image_get_intermediate_size函数的典型用法代码示例。如果您正苦于以下问题:PHP image_get_intermediate_size函数的具体用法?PHP image_get_intermediate_size怎么用?PHP image_get_intermediate_size使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了image_get_intermediate_size函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: image_downsize
function image_downsize($id, $size = 'medium')
{
if (!wp_attachment_is_image($id)) {
return false;
}
$img_url = wp_get_attachment_url($id);
$meta = wp_get_attachment_metadata($id);
$width = $height = 0;
// plugins can use this to provide resize services
if ($out = apply_filters('image_downsize', false, $id, $size)) {
return $out;
}
// try for a new style intermediate size
if ($intermediate = image_get_intermediate_size($id, $size)) {
$img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
$width = $intermediate['width'];
$height = $intermediate['height'];
} elseif ($size == 'thumbnail') {
// fall back to the old thumbnail
if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) {
$img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
$width = $info[0];
$height = $info[1];
}
}
if (!$width && !$height && isset($meta['width'], $meta['height'])) {
// any other type: use the real image and constrain it
list($width, $height) = image_constrain_size_for_editor($meta['width'], $meta['height'], $size);
}
if ($img_url) {
return array($img_url, $width, $height);
}
return false;
}
示例2: downsize
function downsize($out, $id, $size)
{
$img_url = wp_get_attachment_url($id);
$img_path = get_attached_file($id);
$meta = wp_get_attachment_metadata($id);
$width = $height = 0;
$is_intermediate = false;
$img_url_basename = wp_basename($img_url);
$img_path_basename = wp_basename($img_path);
// extract filter from size request
if (!is_string($size)) {
return $out;
}
$size_bits = explode(':', $size);
$filter = isset($size_bits[1]) ? $size_bits[1] : false;
$size = isset($size_bits[0]) ? $size_bits[0] : false;
// start the reactor
if ($filter) {
// try for a new style intermediate size
if ($intermediate = image_get_intermediate_size($id, $size)) {
$img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
$img_path = str_replace($img_path_basename, $intermediate['file'], $img_path);
$width = $intermediate['width'];
$height = $intermediate['height'];
$is_intermediate = true;
} elseif ($size == 'thumbnail') {
// fall back to the old thumbnail
if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) {
$img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url);
$img_path = str_replace($img_path_basename, wp_basename($thumb_file), $img_path);
$width = $info[0];
$height = $info[1];
$is_intermediate = true;
}
}
if (!$width && !$height && isset($meta['width'], $meta['height'])) {
// any other type: use the real image
$width = $meta['width'];
$height = $meta['height'];
}
if ($img_url && $img_path) {
$input = $img_path;
$output = $this->filtered_url($input, $filter);
// generate filtered thumb
if (!file_exists($output)) {
$this->filter($filter, $input, $output);
}
// point to our new file
$img_url = $this->filtered_url($img_url, $filter);
// we have the actual image size, but might need to further constrain it if content_width is narrower
list($width, $height) = image_constrain_size_for_editor($width, $height, $size);
return array($img_url, $width, $height, $is_intermediate);
}
// don't continue the downsize funtion
return true;
}
return $out;
}
示例3: image_downsize
public function image_downsize($out, $id, $size)
{
// we don't handle this
if (is_array($size)) {
return false;
}
$meta = wp_get_attachment_metadata($id);
$wanted_width = $wanted_height = 0;
if (empty($meta['file'])) {
return false;
}
// get $size dimensions
global $_wp_additional_image_sizes;
if (isset($_wp_additional_image_sizes) && isset($_wp_additional_image_sizes[$size])) {
$wanted_width = $_wp_additional_image_sizes[$size]['width'];
$wanted_height = $_wp_additional_image_sizes[$size]['height'];
$wanted_crop = isset($_wp_additional_image_sizes[$size]['crop']) ? $_wp_additional_image_sizes[$size]['crop'] : false;
} else {
if (in_array($size, array('thumbnail', 'medium', 'large'))) {
$wanted_width = get_option($size . '_size_w');
$wanted_height = get_option($size . '_size_h');
$wanted_crop = 'thumbnail' === $size ? (bool) get_option('thumbnail_crop') : false;
} else {
// unknown size, bail out
return false;
}
}
if (0 === absint($wanted_width) && 0 === absint($wanted_height)) {
return false;
}
if ($intermediate = image_get_intermediate_size($id, $size)) {
return false;
} else {
// image size not found, create it
$attachment_path = get_attached_file($id);
$image_editor = wp_get_image_editor($attachment_path);
if (!is_wp_error($image_editor)) {
$image_editor->resize($wanted_width, $wanted_height, $wanted_crop);
$result_image_size = $image_editor->get_size();
$result_width = $result_image_size['width'];
$result_height = $result_image_size['height'];
$suffix = $result_width . 'x' . $result_height;
$filename = $image_editor->generate_filename($suffix);
$image_editor->save($filename);
$meta['sizes'][$size] = array('file' => basename($filename), 'width' => $result_width, 'height' => $result_height, 'mime-type' => get_post_mime_type($id));
wp_update_attachment_metadata($id, $meta);
} else {
return false;
}
}
return false;
}
示例4: swift_delete_attachment
function swift_delete_attachment($post_id)
{
if (!$this->swift_plugin_setup()) {
return;
}
$backup_sizes = get_post_meta($post_id, '_wp_attachment_backup_sizes', true);
$intermediate_sizes = array();
foreach (get_intermediate_image_sizes() as $size) {
if ($intermediate = image_get_intermediate_size($post_id, $size)) {
$intermediate_sizes[] = $intermediate;
}
}
if (!($swiftObject = $this->swift_get_attachment_info($post_id))) {
return;
}
$swift_path = dirname($swiftObject['key']);
$objects = array();
// remove intermediate and backup images if there are any
foreach ($intermediate_sizes as $intermediate) {
$objects[] = array('Key' => path_join($swift_path, $intermediate['file']));
}
if (is_array($backup_sizes)) {
foreach ($backup_sizes as $size) {
$objects[] = array('Key' => $swift_path);
}
}
// Try removing any @2x images but ignore any errors
if ($objects) {
$hidpi_images = array();
foreach ($objects as $object) {
$hidpi_images[] = array('Key' => $this->swift_get_hidpi_file_path($object['Key']));
}
try {
foreach ($hidpi_images as $image) {
$this->swift_get_client()->getContainer($swiftObject['bucket'])->getObject($image['Key'])->delete();
}
} catch (Exception $e) {
}
}
$objects[] = array('Key' => $swiftObject['key']);
try {
foreach ($objects as $object) {
$this->swift_get_client()->getContainer($swiftObject['bucket'])->getObject($object['Key'])->delete();
}
} catch (Exception $e) {
error_log('Error removing files from Swift: ' . $e->getMessage());
return;
}
delete_post_meta($post_id, 'swift_info');
}
示例5: add_post_featured_image_as_rss_item_enclosure
function add_post_featured_image_as_rss_item_enclosure()
{
if (!has_post_thumbnail()) {
return;
}
$thumbnail_size = apply_filters('rss_enclosure_image_size', 'thumbnail');
$thumbnail_id = get_post_thumbnail_id(get_the_ID());
$thumbnail = image_get_intermediate_size($thumbnail_id, $thumbnail_size);
if (empty($thumbnail)) {
return;
}
$upload_dir = wp_upload_dir();
printf('<enclosure url="%s" length="%s" type="%s" />', $thumbnail['url'], filesize(path_join($upload_dir['basedir'], $thumbnail['path'])), get_post_mime_type($thumbnail_id));
}
示例6: filePath
public function filePath($size = null)
{
if ($this->isImage() and !is_null($size)) {
if ($size === 'thumb') {
$size = 'thumbnail';
}
$base = wp_upload_dir();
$data = image_get_intermediate_size($this->id(), $size);
$path = isset($data['path']) ? $base['basedir'] . '/' . $data['path'] : null;
} else {
$path = get_attached_file($this->id(), true);
}
return $path;
}
示例7: delete_attachment
function delete_attachment($post_id)
{
if (!$this->is_plugin_setup()) {
return;
}
$backup_sizes = get_post_meta($post_id, '_wp_attachment_backup_sizes', true);
$intermediate_sizes = array();
foreach (get_intermediate_image_sizes() as $size) {
if ($intermediate = image_get_intermediate_size($post_id, $size)) {
$intermediate_sizes[] = $intermediate;
}
}
if (!($dsobject = $this->get_attachment_dreamspeed_info($post_id))) {
return;
}
$amazon_path = dirname($dsobject['key']);
$objects = array();
// remove intermediate and backup images if there are any
foreach ($intermediate_sizes as $intermediate) {
$objects[] = array('Key' => path_join($amazon_path, $intermediate['file']));
}
if (is_array($backup_sizes)) {
foreach ($backup_sizes as $size) {
$objects[] = array('Key' => path_join($amazon_path, $del_file));
}
}
// Try removing any @2x images but ignore any errors
if ($objects) {
$hidpi_images = array();
foreach ($objects as $object) {
$hidpi_images[] = array('Key' => $this->get_hidpi_file_path($object['Key']));
}
try {
$this->get_doclient()->deleteObjects(array('Bucket' => $dsobject['bucket'], 'Objects' => $hidpi_images));
} catch (Exception $e) {
}
}
$objects[] = array('Key' => $dsobject['key']);
try {
$this->get_doclient()->deleteObjects(array('Bucket' => $dsobject['bucket'], 'Objects' => $objects));
} catch (Exception $e) {
error_log('Error removing files from DreamSpeed: ' . $e->getMessage());
return;
}
delete_post_meta($post_id, 'amazonS3_info');
}
示例8: wpsc_product_image
function wpsc_product_image($attachment_id = 0, $width = null, $height = null)
{
$uploads = wp_upload_dir();
// Do some dancing around the image size
if ($width >= 10 && $height >= 10 && ($width <= 1024 && $height <= 1024)) {
$intermediate_size = "wpsc-{$width}x{$height}";
}
// Get image url if we have enough info
if ($attachment_id > 0 && !empty($intermediate_size)) {
// Get all the required information about the attachment
$image_meta = get_post_meta($attachment_id, '');
$file_path = get_attached_file($attachment_id);
// Clean up the meta array
foreach ($image_meta as $meta_name => $meta_value) {
$image_meta[$meta_name] = maybe_unserialize(array_pop($meta_value));
}
$attachment_metadata = $image_meta['_wp_attachment_metadata'];
// Determine if we already have an image of this size
if (isset($attachment_metadata['sizes']) && count($attachment_metadata['sizes']) > 0 && isset($attachment_metadata['sizes'][$intermediate_size])) {
$intermediate_image_data = image_get_intermediate_size($attachment_id, $intermediate_size);
$image_url = $intermediate_image_data['url'];
} else {
$image_url = home_url("index.php?wpsc_action=scale_image&attachment_id={$attachment_id}&width={$width}&height={$height}");
}
// Not enough info so attempt to fallback
} else {
if (!empty($attachment_id)) {
$image_url = home_url("index.php?wpsc_action=scale_image&attachment_id={$attachment_id}&width={$width}&height={$height}");
} else {
$image_url = false;
}
}
if (empty($image_url) && !empty($file_path)) {
$image_meta = get_post_meta($attachment_id, '_wp_attached_file');
if (!empty($image_meta)) {
$image_url = $uploads['baseurl'] . '/' . $image_meta[0];
}
}
if (is_ssl()) {
str_replace('http://', 'https://', $image_url);
}
return apply_filters('wpsc_product_image', $image_url);
}
示例9: __wp_supercustom_cms_image_downsize
/**
* Uses WordPress filter for image_downsize, kill wp-image-dimension
* code by Andrew Rickmann
* http://www.wp-fun.co.uk/
* @param $value, $id, $size
*/
function __wp_supercustom_cms_image_downsize($value = FALSE, $id = 0, $size = 'medium')
{
if (!wp_attachment_is_image($id)) {
return FALSE;
}
$img_url = wp_get_attachment_url($id);
// Mimic functionality in image_downsize function in wp-includes/media.php
if ($intermediate = image_get_intermediate_size($id, $size)) {
$img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
} elseif ($size == 'thumbnail') {
// fall back to the old thumbnail
if ($thumb_file = wp_get_attachment_thumb_file() && ($info = getimagesize($thumb_file))) {
$img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
}
}
if ($img_url) {
return array($img_url, 0, 0);
}
return FALSE;
}
示例10: taxonomy_image_plugin_get_image_src
/**
* Get Image Source.
*
* Return a uri to a custom image size.
*
* If size doesn't exist, attempt to create a resized version.
* The output of this function should be escaped before printing to the browser.
*
* @param int Image ID.
* @return string URI of custom image on success; emtpy string otherwise.
*
* @access private.
* @since 2010-10-28
*/
function taxonomy_image_plugin_get_image_src($id)
{
$detail = taxonomy_image_plugin_detail_image_size();
/* Return url to custom intermediate size if it exists. */
$img = image_get_intermediate_size($id, $detail['name']);
if (isset($img['url'])) {
return $img['url'];
}
/* Detail image does not exist, attempt to create it. */
$wp_upload_dir = wp_upload_dir();
if (isset($wp_upload_dir['basedir'])) {
/* Create path to original uploaded image. */
$path = trailingslashit($wp_upload_dir['basedir']) . get_post_meta($id, '_wp_attached_file', true);
if (is_file($path)) {
/* Attempt to create a new downsized version of the original image. */
$new = image_resize($path, $detail['size'][0], $detail['size'][1], $detail['size'][2]);
/* Image creation successful. Generate and cache image metadata. Return url. */
if (!is_wp_error($new)) {
$meta = wp_generate_attachment_metadata($id, $path);
wp_update_attachment_metadata($id, $meta);
$img = image_get_intermediate_size($id, $detail['name']);
if (isset($img['url'])) {
return $img['url'];
}
}
}
}
/* Custom intermediate size cannot be created, try for thumbnail. */
$img = image_get_intermediate_size($id, 'thumbnail');
if (isset($img['url'])) {
return $img['url'];
}
/* Thumbnail cannot be found, try fullsize. */
$url = wp_get_attachment_url($id);
if (!empty($url)) {
return $url;
}
/**
* No image can be found.
* This is most likely caused by a user deleting an attachment before deleting it's association with a taxonomy.
* If we are in the administration panels:
* - Delete the association.
* - Return uri to default.png.
*/
if (is_admin()) {
$assoc = taxonomy_image_plugin_get_associations();
foreach ($assoc as $term => $img) {
if ($img === $id) {
unset($assoc[$term]);
}
}
update_option('taxonomy_image_plugin', $assoc);
return taxonomy_image_plugin_url('default.png');
}
/*
* No image can be found.
* Return path to blank-image.png.
*/
return taxonomy_image_plugin_url('blank.png');
}
示例11: taxonomy_images_plugin_get_queried_term_image_data
/**
* Queried Term Image Data.
*
* Returns a url to the image associated with the current queried
* term. In the event that no image is found an empty string will
* be returned.
*
* Designed to be used in archive templates including
* (but not limited to) archive.php, category.php, tag.php,
* taxonomy.php as well as derivatives of these templates.
*
* Recognized Arguments
*
* image_size (string) - May be any image size registered with
* WordPress. If no image size is specified, 'thumbnail' will be
* used as a default value. In the event that an unregistered size
* is specified, this function will return an empty array.
*
* @param mixed Default value for apply_filters() to return. Unused.
* @param array Named Arguments.
* @return array Image data: url, width and height.
*
* @access private Use the 'taxonomy-images-queried-term-image-data' filter.
* @since 0.7
* @alter 0.7.2
*/
function taxonomy_images_plugin_get_queried_term_image_data( $default, $args = array() ) {
$filter = 'taxonomy-images-queried-term-image-data';
if ( current_filter() !== $filter ) {
taxonomy_image_plugin_please_use_filter( __FUNCTION__, $filter );
}
$args = wp_parse_args( $args, array(
'image_size' => 'thumbnail',
) );
$ID = apply_filters( 'taxonomy-images-queried-term-image-id', 0 );
if ( empty( $ID ) ) {
return array();
}
$data = array();
if ( in_array( $args['image_size'], array( 'full', 'fullsize' ) ) ) {
$src = wp_get_attachment_image_src( $ID, 'full' );
if ( isset( $src[0] ) ) {
$data['url'] = $src[0];
}
if ( isset( $src[1] ) ) {
$data['width'] = $src[1];
}
if ( isset( $src[2] ) ) {
$data['height'] = $src[2];
}
} else {
$data = image_get_intermediate_size( $ID, $args['image_size'] );
}
if ( ! empty( $data ) ) {
return $data;
}
return array();
}
示例12: image_downsize
/**
* Scale an image to fit a particular size (such as 'thumb' or 'medium').
*
* Array with image url, width, height, and whether is intermediate size, in
* that order is returned on success is returned. $is_intermediate is true if
* $url is a resized image, false if it is the original.
*
* The URL might be the original image, or it might be a resized version. This
* function won't create a new resized copy, it will just return an already
* resized one if it exists.
*
* A plugin may use the 'image_downsize' filter to hook into and offer image
* resizing services for images. The hook must return an array with the same
* elements that are returned in the function. The first element being the URL
* to the new image that was resized.
*
* @since 2.5.0
*
* @param int $id Attachment ID for image.
* @param array|string $size Optional. Image size to scale to. Accepts any valid image size,
* or an array of width and height values in pixels (in that order).
* Default 'medium'.
* @return false|array Array containing the image URL, width, height, and boolean for whether
* the image is an intermediate size. False on failure.
*/
function image_downsize($id, $size = 'medium')
{
if (!wp_attachment_is_image($id)) {
return false;
}
/**
* Filter whether to preempt the output of image_downsize().
*
* Passing a truthy value to the filter will effectively short-circuit
* down-sizing the image, returning that value as output instead.
*
* @since 2.5.0
*
* @param bool $downsize Whether to short-circuit the image downsize. Default false.
* @param int $id Attachment ID for image.
* @param array|string $size Size of image. Image size or array of width and height values (in that order).
* Default 'medium'.
*/
if ($out = apply_filters('image_downsize', false, $id, $size)) {
return $out;
}
$img_url = wp_get_attachment_url($id);
$meta = wp_get_attachment_metadata($id);
$width = $height = 0;
$is_intermediate = false;
$img_url_basename = wp_basename($img_url);
// try for a new style intermediate size
if ($intermediate = image_get_intermediate_size($id, $size)) {
$img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
$width = $intermediate['width'];
$height = $intermediate['height'];
$is_intermediate = true;
} elseif ($size == 'thumbnail') {
// fall back to the old thumbnail
if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) {
$img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url);
$width = $info[0];
$height = $info[1];
$is_intermediate = true;
}
}
if (!$width && !$height && isset($meta['width'], $meta['height'])) {
// any other type: use the real image
$width = $meta['width'];
$height = $meta['height'];
}
if ($img_url) {
// we have the actual image size, but might need to further constrain it if content_width is narrower
list($width, $height) = image_constrain_size_for_editor($width, $height, $size);
return array($img_url, $width, $height, $is_intermediate);
}
return false;
}
示例13: wp_get_attachment_image_sizes
/**
* Retrieves a source size attribute for an image from an array of values.
*
* @since 4.4.0
*
* @param int $attachment_id Image attachment ID.
* @param string $size Optional. Name of image size. Default value: 'medium'.
* @param array $args {
* Optional. Arguments to retrieve attachments.
*
* @type array|string $sizes An array or string containing of size information.
* @type int $width A single width value used in the default `sizes` string.
* }
* @return string|bool A valid source size value for use in a 'sizes' attribute or false.
*/
function wp_get_attachment_image_sizes($attachment_id, $size = 'medium', $args = null)
{
$img_width = 0;
// Try to get the image width from $args.
if (is_array($args) && !empty($args['width'])) {
$img_width = (int) $args['width'];
} elseif ($img = image_get_intermediate_size($attachment_id, $size)) {
list($img_width) = image_constrain_size_for_editor($img['width'], $img['height'], $size);
}
// Bail early if $image_width isn't set.
if (!$img_width) {
return false;
}
// Set the image width in pixels.
$img_width = $img_width . 'px';
// Set up our default values.
$defaults = array('sizes' => array(array('size_value' => '100vw', 'mq_value' => $img_width, 'mq_name' => 'max-width'), array('size_value' => $img_width)));
$args = wp_parse_args($args, $defaults);
/**
* Filter arguments used to create 'sizes' attribute.
*
* @since 4.4.0
*
* @param array $args An array of arguments used to create a 'sizes' attribute.
* @param int $attachment_id Post ID of the original image.
* @param string $size Name of the image size being used.
*/
$args = apply_filters('wp_image_sizes_args', $args, $attachment_id, $size);
// If sizes is passed as a string, just use the string.
if (is_string($args['sizes'])) {
$size_list = $args['sizes'];
// Otherwise, breakdown the array and build a sizes string.
} elseif (is_array($args['sizes'])) {
$size_list = '';
foreach ($args['sizes'] as $size) {
// Use 100vw as the size value unless something else is specified.
$size_value = $size['size_value'] ? $size['size_value'] : '100vw';
// If a media length is specified, build the media query.
if (!empty($size['mq_value'])) {
$media_length = $size['mq_value'];
// Use max-width as the media condition unless min-width is specified.
$media_condition = !empty($size['mq_name']) ? $size['mq_name'] : 'max-width';
// If a media_length was set, create the media query.
$media_query = '(' . $media_condition . ": " . $media_length . ') ';
} else {
// If no media length was set, $media_query is blank.
$media_query = '';
}
// Add to the source size list string.
$size_list .= $media_query . $size_value . ', ';
}
// Remove the trailing comma and space from the end of the string.
$size_list = substr($size_list, 0, -2);
}
// Return the sizes value as $size_list or false.
return $size_list ? $size_list : false;
}
示例14: test_insert_image_delete
function test_insert_image_delete()
{
if (!function_exists('imagejpeg')) {
$this->markTestSkipped('jpeg support unavailable');
}
update_option('medium_size_w', 400);
update_option('medium_size_h', 0);
update_option('medium_large_size_w', 600);
update_option('medium_large_size_h', 0);
$filename = DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG';
$contents = file_get_contents($filename);
$upload = wp_upload_bits(basename($filename), null, $contents);
$this->assertTrue(empty($upload['error']));
$id = $this->_make_attachment($upload);
$uploads = wp_upload_dir();
// check that the file and intermediates exist
$thumb = image_get_intermediate_size($id, 'thumbnail');
$this->assertEquals('2007-06-17DSC_4173-150x150.jpg', $thumb['file']);
$this->assertTrue(is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']));
$medium = image_get_intermediate_size($id, 'medium');
$this->assertEquals('2007-06-17DSC_4173-400x602.jpg', $medium['file']);
$this->assertTrue(is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path']));
$medium_large = image_get_intermediate_size($id, 'medium_large');
$this->assertEquals('2007-06-17DSC_4173-600x904.jpg', $medium_large['file']);
$this->assertTrue(is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path']));
$meta = wp_get_attachment_metadata($id);
$original = $meta['file'];
$this->assertTrue(is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $original));
// now delete the attachment and make sure all files are gone
wp_delete_attachment($id);
$this->assertFalse(is_file($thumb['path']));
$this->assertFalse(is_file($medium['path']));
$this->assertFalse(is_file($medium_large['path']));
$this->assertFalse(is_file($original));
}
示例15: wpmlm_scale_image
/**
* wpmlm scale image function, dynamically resizes an image oif no image already exists of that size.
*/
function wpmlm_scale_image()
{
global $wpdb;
if (!isset($_REQUEST['wpmlm_action']) || !isset($_REQUEST['attachment_id']) || 'scale_image' != $_REQUEST['wpmlm_action'] || !is_numeric($_REQUEST['attachment_id'])) {
return false;
}
require_once ABSPATH . 'wp-admin/includes/image.php';
$attachment_id = absint($_REQUEST['attachment_id']);
$width = absint($_REQUEST['width']);
$height = absint($_REQUEST['height']);
$intermediate_size = '';
if ($width >= 10 && $height >= 10 && ($width <= 1024 && $height <= 1024)) {
$intermediate_size = "wpmlm-{$width}x{$height}";
$generate_thumbnail = true;
} else {
if (isset($_REQUEST['intermediate_size'])) {
$intermediate_size = esc_attr($_REQUEST['intermediate_size']);
}
$generate_thumbnail = false;
}
// If the attachment ID is greater than 0, and the width and height is greater than or equal to 10, and less than or equal to 1024
if ($attachment_id > 0 && $intermediate_size != '') {
// Get all the required information about the attachment
$uploads = wp_upload_dir();
$image_meta = get_post_meta($attachment_id, '');
$file_path = get_attached_file($attachment_id);
foreach ($image_meta as $meta_name => $meta_value) {
// clean up the meta array
$image_meta[$meta_name] = maybe_unserialize(array_pop($meta_value));
}
if (!isset($image_meta['_wp_attachment_metadata'])) {
$image_meta['_wp_attachment_metadata'] = '';
}
$attachment_metadata = $image_meta['_wp_attachment_metadata'];
if (!isset($attachment_metadata['sizes'])) {
$attachment_metadata['sizes'] = '';
}
if (!isset($attachment_metadata['sizes'][$intermediate_size])) {
$attachment_metadata['sizes'][$intermediate_size] = '';
}
// determine if we already have an image of this size
if (count($attachment_metadata['sizes']) > 0 && $attachment_metadata['sizes'][$intermediate_size]) {
$intermediate_image_data = image_get_intermediate_size($attachment_id, $intermediate_size);
if (file_exists($file_path)) {
$original_modification_time = filemtime($file_path);
$cache_modification_time = filemtime($uploads['basedir'] . "/" . $intermediate_image_data['path']);
if ($original_modification_time < $cache_modification_time) {
$generate_thumbnail = false;
}
}
}
if ($generate_thumbnail == true) {
//JS - 7.1.2010 - Added true parameter to function to not crop - causing issues on WPShop
$intermediate_size_data = image_make_intermediate_size($file_path, $width, $height, true);
$attachment_metadata['sizes'][$intermediate_size] = $intermediate_size_data;
wp_update_attachment_metadata($attachment_id, $attachment_metadata);
$intermediate_image_data = image_get_intermediate_size($attachment_id, $intermediate_size);
}
/// if we are serving the page using SSL, we have to use for the image too.
if (is_ssl()) {
$output_url = str_replace("http://", "https://", $intermediate_image_data['url']);
} else {
$output_url = $intermediate_image_data['url'];
}
wp_redirect($output_url);
} else {
_e("Invalid Image parameters", 'wpmlm');
}
exit;
}