function sp_generate_attachment_metadata($attachment_id, $file)
{
    $attachment = get_post($attachment_id);
    $metadata = array();
    if (preg_match('!^image/!', get_post_mime_type($attachment)) && file_is_displayable_image($file)) {
        $imagesize = getimagesize($file);
        $metadata['width'] = $imagesize[0];
        $metadata['height'] = $imagesize[1];
        list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
        $metadata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
        // Make the file path relative to the upload dir
        $metadata['file'] = $attachment->post_title;
        // make thumbnails and other intermediate sizes
        global $_wp_additional_image_sizes;
        $temp_sizes = array('thumbnail', 'medium', 'large');
        // Standard sizes
        if (isset($_wp_additional_image_sizes) && count($_wp_additional_image_sizes)) {
            $temp_sizes = array_merge($temp_sizes, array_keys($_wp_additional_image_sizes));
        }
        $temp_sizes = apply_filters('intermediate_image_sizes', $temp_sizes);
        foreach ($temp_sizes as $s) {
            $sizes[$s] = array('width' => '', 'height' => '', 'crop' => FALSE);
            if (isset($_wp_additional_image_sizes[$s]['width'])) {
                $sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
            } else {
                $sizes[$s]['width'] = get_option("{$s}_size_w");
            }
            // For default sizes set in options
            if (isset($_wp_additional_image_sizes[$s]['height'])) {
                $sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
            } else {
                $sizes[$s]['height'] = get_option("{$s}_size_h");
            }
            // For default sizes set in options
            if (isset($_wp_additional_image_sizes[$s]['crop'])) {
                $sizes[$s]['crop'] = intval($_wp_additional_image_sizes[$s]['crop']);
            } else {
                $sizes[$s]['crop'] = get_option("{$s}_crop");
            }
            // For default sizes set in options
        }
        $sizes = apply_filters('intermediate_image_sizes_advanced', $sizes);
        foreach ($sizes as $size => $size_data) {
            $resized = image_make_intermediate_size($file, $size_data['width'], $size_data['height'], $size_data['crop']);
            if ($resized) {
                $metadata['sizes'][$size] = $resized;
            }
        }
        // fetch additional metadata from exif/iptc
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            $metadata['image_meta'] = $image_meta;
        }
    }
    return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id);
}
function wp_upload_display( $dims = false, $href = '' ) {
	global $post;
	$id = get_the_ID();
	$attachment_data = wp_get_attachment_metadata( $id );
	$is_image = (int) wp_attachment_is_image();
	if ( !isset($attachment_data['width']) && $is_image ) {
		if ( $image_data = getimagesize( get_attached_file( $id ) ) ) {
			$attachment_data['width'] = $image_data[0];
			$attachment_data['height'] = $image_data[1];
			wp_update_attachment_metadata( $id, $attachment_data );
		}
	}
	if ( isset($attachment_data['width']) )
		list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);

	ob_start();
		the_title();
		$post_title = attribute_escape(ob_get_contents());
	ob_end_clean();
	$post_content = attribute_escape(apply_filters( 'content_edit_pre', $post->post_content ));

	$class = 'text';
	$innerHTML = get_attachment_innerHTML( $id, false, $dims );
	if ( $image_src = get_attachment_icon_src() ) {
		$image_rel = wp_make_link_relative($image_src);
		$innerHTML = ' ' . str_replace($image_src, $image_rel, $innerHTML);
		$class = 'image';
	}

	$src_base = wp_get_attachment_url();
	$src = wp_make_link_relative( $src_base );
	$src_base = str_replace($src, '', $src_base);

	$r = '';

	if ( $href )
		$r .= "<a id='file-link-$id' href='$href' title='$post_title' class='file-link $class'>\n";
	if ( $href || $image_src )
		$r .= "\t\t\t$innerHTML";
	if ( $href )
		$r .= "</a>\n";
	$r .= "\n\t\t<div class='upload-file-data'>\n\t\t\t<p>\n";
	$r .= "\t\t\t\t<input type='hidden' name='attachment-url-$id' id='attachment-url-$id' value='$src' />\n";
	$r .= "\t\t\t\t<input type='hidden' name='attachment-url-base-$id' id='attachment-url-base-$id' value='$src_base' />\n";

	if ( !$thumb_base = wp_get_attachment_thumb_url() )
		$thumb_base = wp_mime_type_icon();
	if ( $thumb_base ) {
		$thumb_rel = wp_make_link_relative( $thumb_base );
		$thumb_base = str_replace( $thumb_rel, '', $thumb_base );
		$r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-$id' id='attachment-thumb-url-$id' value='$thumb_rel' />\n";
		$r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-base-$id' id='attachment-thumb-url-base-$id' value='$thumb_base' />\n";
	}

	$r .= "\t\t\t\t<input type='hidden' name='attachment-is-image-$id' id='attachment-is-image-$id' value='$is_image' />\n";

	if ( isset($width) ) {
		$r .= "\t\t\t\t<input type='hidden' name='attachment-width-$id' id='attachment-width-$id' value='$width' />\n";
		$r .= "\t\t\t\t<input type='hidden' name='attachment-height-$id' id='attachment-height-$id' value='$height' />\n";
	}
	$r .= "\t\t\t\t<input type='hidden' name='attachment-page-url-$id' id='attachment-page-url-$id' value='" . get_attachment_link( $id ) . "' />\n";
	$r .= "\t\t\t\t<input type='hidden' name='attachment-title-$id' id='attachment-title-$id' value='$post_title' />\n";
	$r .= "\t\t\t\t<input type='hidden' name='attachment-description-$id' id='attachment-description-$id' value='$post_content' />\n";
	$r .= "\t\t\t</p>\n\t\t</div>\n";
	return $r;
}
Esempio n. 3
0
/**
 * Calculated the new dimentions for a downsampled image.
 *
 * @since 2.0.0
 * @see wp_shrink_dimensions()
 *
 * @param int $width Current width of the image
 * @param int $height Current height of the image
 * @return mixed Array(height,width) of shrunk dimensions.
 */
function get_udims($width, $height)
{
    return wp_shrink_dimensions($width, $height);
}
Esempio n. 4
0
function wp_upload_display($dims = false, $href = '')
{
    global $post;
    $id = get_the_ID();
    $attachment_data = wp_get_attachment_metadata($id);
    $is_image = (int) wp_attachment_is_image();
    $filesystem_path = get_attached_file($id);
    if (!isset($attachment_data['width']) && $is_image) {
        if ($image_data = getimagesize($filesystem_path)) {
            $attachment_data['width'] = $image_data[0];
            $attachment_data['height'] = $image_data[1];
            wp_update_attachment_metadata($id, $attachment_data);
        }
    }
    if (isset($attachment_data['width'])) {
        list($width, $height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
    }
    $post_title = attribute_escape(the_title('', '', false));
    $post_content = attribute_escape(apply_filters('content_edit_pre', $post->post_content));
    $class = 'text';
    $innerHTML = get_attachment_innerHTML($id, false, $dims);
    if ($image_src = get_attachment_icon_src()) {
        $image_rel = wp_make_link_relative($image_src);
        $innerHTML = '&nbsp;' . str_replace($image_src, $image_rel, $innerHTML);
        $class = 'image';
    }
    $src_base = wp_get_attachment_url();
    $src = wp_make_link_relative($src_base);
    $src_base = str_replace($src, '', $src_base);
    if (!trim($post_title)) {
        $post_title = basename($src);
    }
    $r = '';
    if ($href) {
        $r .= "<a id='file-link-{$id}' href='{$href}' title='{$post_title}' class='file-link {$class}'>\n";
    }
    if ($href || $image_src) {
        $r .= "\t\t\t{$innerHTML}";
    }
    if ($href) {
        $r .= "</a>\n";
    }
    $size = @filesize($filesystem_path);
    if (!empty($size)) {
        $r .= "\t\t\t\t<span class='upload-file-size'>" . size_format($size) . "</span>\n";
    }
    $r .= "\n\t\t<div class='upload-file-data'>\n\t\t\t<p>\n";
    $r .= "\t\t\t\t<input type='hidden' name='attachment-url-{$id}' id='attachment-url-{$id}' value='{$src}' />\n";
    $r .= "\t\t\t\t<input type='hidden' name='attachment-url-base-{$id}' id='attachment-url-base-{$id}' value='{$src_base}' />\n";
    if (!($thumb_base = wp_get_attachment_thumb_url())) {
        $thumb_base = wp_mime_type_icon();
    }
    if ($thumb_base) {
        $thumb_rel = wp_make_link_relative($thumb_base);
        $thumb_base = str_replace($thumb_rel, '', $thumb_base);
        $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-{$id}' id='attachment-thumb-url-{$id}' value='{$thumb_rel}' />\n";
        $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-base-{$id}' id='attachment-thumb-url-base-{$id}' value='{$thumb_base}' />\n";
    }
    $r .= "\t\t\t\t<input type='hidden' name='attachment-is-image-{$id}' id='attachment-is-image-{$id}' value='{$is_image}' />\n";
    if (isset($width)) {
        $r .= "\t\t\t\t<input type='hidden' name='attachment-width-{$id}' id='attachment-width-{$id}' value='{$width}' />\n";
        $r .= "\t\t\t\t<input type='hidden' name='attachment-height-{$id}' id='attachment-height-{$id}' value='{$height}' />\n";
    }
    $r .= "\t\t\t\t<input type='hidden' name='attachment-page-url-{$id}' id='attachment-page-url-{$id}' value='" . get_attachment_link($id) . "' />\n";
    $r .= "\t\t\t\t<input type='hidden' name='attachment-title-{$id}' id='attachment-title-{$id}' value='{$post_title}' />\n";
    $r .= "\t\t\t\t<input type='hidden' name='attachment-description-{$id}' id='attachment-description-{$id}' value='{$post_content}' />\n";
    $r .= "\t\t\t</p>\n\t\t</div>\n";
    return $r;
}
Esempio n. 5
0
function wp_save_image($post_id)
{
    $return = '';
    $success = $delete = $scaled = $nocrop = false;
    $post = get_post($post_id);
    @ini_set('memory_limit', '256M');
    $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 = pathinfo52($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_shrink_dimensions($meta['width'], $meta['height']);
        $meta['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
        if ($success && ('nothumb' == $target || 'all' == $target)) {
            $sizes = apply_filters('intermediate_image_sizes', array('large', 'medium', 'thumbnail'));
            if ('nothumb' == $target) {
                $sizes = array_diff($sizes, array('thumbnail'));
            }
        }
        $return->fw = $meta['width'];
        $return->fh = $meta['height'];
    } elseif ('thumbnail' == $target) {
        $sizes = array('thumbnail');
        $success = $delete = $nocrop = true;
    }
    if (isset($sizes)) {
        foreach ($sizes as $size) {
            $tag = false;
            if (isset($meta['sizes'][$size])) {
                if (isset($backup_sizes["{$size}-orig"])) {
                    if ((!defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE) && $backup_sizes["{$size}-orig"]['file'] != $meta['sizes'][$size]['file']) {
                        $tag = "{$size}-{$suffix}";
                    }
                } else {
                    $tag = "{$size}-orig";
                }
                if ($tag) {
                    $backup_sizes[$tag] = $meta['sizes'][$size];
                }
            }
            $crop = $nocrop ? false : get_option("{$size}_crop");
            $resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), $crop);
            if ($resized) {
                $meta['sizes'][$size] = $resized;
            } else {
                unset($meta['sizes'][$size]);
            }
        }
    }
    if ($success) {
        wp_update_attachment_metadata($post_id, $meta);
        update_post_meta($post_id, '_wp_attachment_backup_sizes', $backup_sizes);
        if ($target == 'thumbnail' || $target == 'all' || $target == 'full') {
            $file_url = wp_get_attachment_url($post_id);
            if ($thumb = $meta['sizes']['thumbnail']) {
                $return->thumbnail = path_join(dirname($file_url), $thumb['file']);
            } else {
                $return->thumbnail = "{$file_url}?w=128&h=128";
            }
        }
    } else {
        $delete = true;
    }
    if ($delete) {
        $delpath = apply_filters('wp_delete_file', $new_path);
        @unlink($delpath);
    }
    imagedestroy($img);
    $return->msg = esc_js(__('Image saved'));
    return $return;
}
Esempio n. 6
0
 function scissors_resize($post, $filename, $src)
 {
     $w = intval($_POST['width']);
     $h = intval($_POST['height']);
     if ($w > 0 && $h > 0) {
         $srcW = imagesx($src);
         $srcH = imagesy($src);
         if ($w != $srcW || $h != $srcH) {
             $dst = scissors_create_image($w, $h);
             if (is_resource($dst)) {
                 if (imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $srcW, $srcH)) {
                     scissors_rebuild_watermark_meta($dst, $post->post_mime_type, $post->ID);
                     if (scissors_is_watermarking_enabled('full', $post->ID, TRUE)) {
                         scissors_watermark_image($dst);
                     }
                     if (scissors_save_image($dst, $filename, $post->post_mime_type)) {
                         // update meta data, no need to rebuild intermediate images because the aspect ratio always stays the same
                         $metadata = wp_get_attachment_metadata($post->ID);
                         $metadata['width'] = $w;
                         $metadata['height'] = $h;
                         list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
                         $metadata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
                         wp_update_attachment_metadata($post->ID, $metadata);
                         $msg = "done;full,{$w},{$h}";
                         // signal change of the full-size image
                         if (!scissors_has_thumbnail($metadata)) {
                             $msg .= "+thumbnail,{$w},{$h}";
                         }
                         // full size image is used as thumbnail, force GUI updated
                     } else {
                         $msg = __('Failed to save new image.', 'scissors');
                     }
                 } else {
                     $msg = __('Failed to resize image.', 'scissors');
                 }
                 imagedestroy($dst);
                 return $msg;
             } else {
                 return __('Failed to allocate memory.', 'scissors');
             }
         } else {
             return 'done;*';
         }
         // signal no change
     } else {
         return __('Invalid destination dimensions.', 'scissors');
     }
 }
Esempio n. 7
0
	/**
	 * @expectedDeprecated wp_shrink_dimensions
	 */
	function test_shrink_dimensions_boundary() {
		// one dimension is larger than the constraint, one smaller - result should be constrained
		$out = wp_shrink_dimensions(1024, 768, 500, 800);
		$this->assertequals(array(500, 375), $out);

		$out = wp_shrink_dimensions(1024, 768, 2000, 700);
		$this->assertequals(array(933, 700), $out);

		// portrait
		$out = wp_shrink_dimensions(768, 1024, 800, 500);
		$this->assertequals(array(375, 500), $out);

		$out = wp_shrink_dimensions(768, 1024, 2000, 700);
		$this->assertequals(array(525, 700), $out);
	}
Esempio n. 8
0
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);
        @unlink($delpath);
    }
    imagedestroy($img);
    return $msg;
}