/** Attachments **/
function add_attachment($file)
{
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $filename = basename($file);
    // Construct the attachment array
    $attachment = array('post_title' => $filename, 'post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $url);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post = '');
    if (preg_match('!^image/!', $attachment['post_mime_type'])) {
        $imagesize = getimagesize($file);
        $imagedata['width'] = $imagesize['0'];
        $imagedata['height'] = $imagesize['1'];
        list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
        $imagedata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
        $imagedata['file'] = $file;
        add_post_meta($id, '_wp_attachment_metadata', $imagedata);
    }
}
function wp_generate_attachment_metadata( $attachment_id, $file ) {
	$attachment = get_post( $attachment_id );

	$metadata = array();
	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
		$imagesize = getimagesize($file);
		$metadata['width'] = $imagesize['0'];
		$metadata['height'] = $imagesize['1'];
		list($uwidth, $uheight) = get_udims($metadata['width'], $metadata['height']);
		$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
		$metadata['file'] = $file;

		$max = apply_filters( 'wp_thumbnail_creation_size_limit', 3 * 1024 * 1024, $attachment_id, $file );

		if ( $max < 0 || $metadata['width'] * $metadata['height'] < $max ) {
			$max_side = apply_filters( 'wp_thumbnail_max_side_length', 128, $attachment_id, $file );
			$thumb = wp_create_thumbnail( $file, $max_side );

			if ( @file_exists($thumb) )
				$metadata['thumb'] = basename($thumb);
		}
	}
	return apply_filters( 'wp_generate_attachment_metadata', $metadata );
}
Ejemplo n.º 3
0
 $noscript = "<noscript>\n\t\t<div class='caption'><a href=\"" . basename(__FILE__) . "?action=links&amp;attachment={$ID}&amp;post={$post}&amp;all={$all}&amp;start={$start}\">Choose Links</a></div>\n\t\t</noscript>\n";
 $send_delete_cancel = "<a onclick=\"sendToEditor({$ID});return false;\" href=\"javascript:void()\">{$__send_to_editor}</a>\n<a onclick=\"return confirm('{$__confirmdelete}')\" href=\"" . basename(__FILE__) . "?action=delete&amp;attachment={$ID}&amp;all={$all}&amp;start={$start}&amp;post={$post}\">{$__delete}</a>\n\t\t<a onclick=\"popup.style.display='none';return false;\" href=\"javascript:void()\">{$__close}</a>\n";
 $uwidth_sum += 128;
 if (preg_match('!^image/!', $attachment['post_mime_type'])) {
     $image =& $attachment;
     if (($image['width'] > 128 || $image['height'] > 96) && !empty($image['thumb']) && file_exists(dirname($image['file']) . '/' . $image['thumb'])) {
         $src = str_replace(basename($image['guid']), $image['thumb'], $image['guid']);
         $script .= "srca[{$ID}] = '{$src}';\nsrcb[{$ID}] = '{$image['guid']}';\n";
         $thumb = 'true';
         $thumbtext = $__using_thumbnail;
     } else {
         $src = $image['guid'];
         $thumb = 'false';
         $thumbtext = $__no_thumbnail;
     }
     list($image['uwidth'], $image['uheight']) = get_udims($image['width'], $image['height']);
     $height_width = 'height="' . $image['uheight'] . '" width="' . $image['uwidth'] . '"';
     $xpadding = (128 - $image['uwidth']) / 2;
     $ypadding = (96 - $image['uheight']) / 2;
     $style .= "#target{$ID} img { padding: {$ypadding}px {$xpadding}px; }\n";
     $script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" class=\"imagelink\" href=\"{$href}\" onclick=\"doPopup({$ID});return false;\" title=\"{$image['post_title']}\">';\nab[{$ID}] = '<a class=\"imagelink\" href=\"{$image['guid']}\" onclick=\"doPopup({$ID});return false;\" title=\"{$image['post_title']}\">';\nimga[{$ID}] = '<img id=\"image{$ID}\" src=\"{$src}\" alt=\"{$image['post_title']}\" {$height_width} />';\nimgb[{$ID}] = '<img id=\"image{$ID}\" src=\"{$image['guid']}\" alt=\"{$image['post_title']}\" {$height_width} />';\n";
     $html .= "<div id='target{$ID}' class='attwrap left'>\n\t<div id='div{$ID}' class='imagewrap' onclick=\"doPopup({$ID});\">\n\t\t<img id=\"image{$ID}\" src=\"{$src}\" alt=\"{$image['post_title']}\" {$height_width} />\n\t</div>\n\t{$noscript}\n</div>\n";
     $popups .= "<div id='popup{$ID}' class='popup'>\n\t<a id=\"I{$ID}\" onclick=\"if({$thumb})toggleImage({$ID});else alert('{$__nothumb}');return false;\" href=\"javascript:void()\">{$thumbtext}</a>\n\t<a id=\"L{$ID}\" onclick=\"toggleLink({$ID});return false;\" href=\"javascript:void()\">{$__not_linked}</a>\n\t{$send_delete_cancel}\n</div>\n";
 } else {
     $title = $attachment['post_title'];
     $filename = basename($attachment['guid']);
     $icon = get_attachment_icon($ID);
     $toggle_icon = "<a id=\"I{$ID}\" onclick=\"toggleOtherIcon({$ID});return false;\" href=\"javascript:void()\">{$__using_title}</a>";
     $script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" href=\"{$href}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">{$attachment['post_title']}</a>';\nab[{$ID}] = '<a id=\"p{$ID}\" href=\"{$filename}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">{$attachment['post_title']}</a>';\ntitle[{$ID}] = '{$attachment['post_title']}';\nfilename[{$ID}] = '{$filename}';\nicon[{$ID}] = '{$icon}';\n";
     $html .= "<div id='target{$ID}' class='attwrap left'>\n\t<div id='div{$ID}' class='otherwrap usingtext' onmousedown=\"selectLink({$ID})\" onclick=\"doPopup({$ID});return false;\">\n\t\t<a id=\"p{$ID}\" href=\"{$attachment['guid']}\" onmousedown=\"selectLink({$ID});\" onclick=\"return false;\">{$attachment['post_title']}</a>\n\t</div>\n\t{$noscript}\n</div>\n";
     $popups .= "<div id='popup{$ID}' class='popup'>\n\t<div class='filetype'>" . __('File Type:') . ' ' . str_replace('/', "/\n", $attachment['post_mime_type']) . "</div>\n\t<a id=\"L{$ID}\" onclick=\"toggleOtherLink({$ID});return false;\" href=\"javascript:void()\">{$__linked_to_file}</a>\n\t{$toggle_icon}\n\t{$send_delete_cancel}\n</div>\n";
 $file = wp_handle_upload($_FILES['video'], $overrides);
 if (!isset($file['error'])) {
     $url = $file['url'];
     $type = $file['type'];
     $file = $file['file'];
     $filename = basename($file);
     // Construct the attachment array
     $attachment = array('post_title' => $_POST['videotitle'] ? $_POST['videotitle'] : $filename, 'post_content' => $_POST['descr'], 'post_status' => 'attachment', 'post_parent' => $_GET['post'], 'post_mime_type' => $type, 'guid' => $url);
     // Save the data
     $id = wp_insert_attachment($attachment, $file, $post);
     if (preg_match('!^image/!', $attachment['post_mime_type'])) {
         // Generate the attachment's postmeta.
         $imagesize = getimagesize($file);
         $imagedata['width'] = $imagesize['0'];
         $imagedata['height'] = $imagesize['1'];
         list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
         $imagedata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
         $imagedata['file'] = $file;
         add_post_meta($id, '_wp_attachment_metadata', $imagedata);
         if ($imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024) {
             if ($imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3) {
                 $thumb = wp_create_thumbnail($file, 128);
             } elseif ($imagedata['height'] > 96) {
                 $thumb = wp_create_thumbnail($file, 96);
             }
             if (@file_exists($thumb)) {
                 $newdata = $imagedata;
                 $newdata['thumb'] = basename($thumb);
                 update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
             } else {
                 $error = $thumb;