WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru

Как WordPress хранит вложения в БД (картинки или файлы)

При загрузке картинки WP добавляет данные в таблицу wp_posts - там все стандартно:

global $wpdb;
$po = $wpdb->get_row( "SELECT * from $wpdb->posts where ID = 1821" );
print_r( $po );

/*
stdClass Object
(
	[ID]                    => 1821
	[post_author]           => 1
	[post_date]             => 2020-03-08 22:55:32
	[post_date_gmt]         => 2020-03-08 17:55:32
	[post_content]          => 
	[post_title]            => sredyaya-kartinka-e1583064965160
	[post_excerpt]          => 
	[post_status]           => inherit
	[comment_status]        => open
	[ping_status]           => closed
	[post_password]         => 
	[post_name]             => sredyaya-kartinka-e1583064965160
	[to_ping]               => 
	[pinged]                => 
	[post_modified]         => 2020-03-08 22:55:32
	[post_modified_gmt]     => 2020-03-08 17:55:32
	[post_content_filtered] => 
	[post_parent]           => 5
	[guid]                  => http://wptest.ru/wp-content/uploads/2020/03/sredyaya-kartinka-e1583064965160.jpg
	[menu_order]            => 0
	[post_type]             => attachment
	[post_mime_type]        => image/jpeg
	[comment_count]         => 0
)
*/

Также добавляет метаданные - этот момент подробнее показан ниже.

Пример с ресайзом (WP 5.3)

Пример когда изображение было загружено и отредактировано в редакторе (было сделано кадрирование).

После этого измененная картинка полностью заменяет оригинал. Оригинал (файл) при этом не удаляется, а единственное упоминание о нём сохраняется в метаполе _wp_attachment_backup_sizes.

$attach_id = 1821;
echo 'wp_get_original_image_url(): '. wp_get_original_image_url( $attach_id )."\n";
echo 'wp_get_original_image_path(): '. wp_get_original_image_path( $attach_id )."\n";
echo 'get_attached_file(): '. get_attached_file( $attach_id )."\n";
echo "\npost_meta -------------\n";
print_r( get_post_meta( $attach_id ) );
echo "\nwp_get_attachment_metadata -------------\n";
print_r( wp_get_attachment_metadata( $attach_id ) );
echo "\n_wp_attachment_backup_sizes -------------\n";
print_r( get_post_meta( $attach_id, '_wp_attachment_backup_sizes', true ) );

/*
wp_get_original_image_url(): http://wptest.ru/wp-content/uploads/2020/03/sredyaya-kartinka-e1583064965160.jpg
wp_get_original_image_path(): C:\sites\wptest.ru\www/wp-content/uploads/2020/03/sredyaya-kartinka-e1583064965160.jpg
get_attached_file(): C:\sites\wptest.ru\www/wp-content/uploads/2020/03/sredyaya-kartinka-e1583064965160.jpg

post_meta -------------
Array
(
	[_wp_attached_file] => Array
		(
			[0] => 2020/03/sredyaya-kartinka-e1583064965160.jpg
		)

	[_wp_attachment_metadata] => Array
		(
			[0] => a:5:{s:5:"width";i:672;s:6:"height";i:422;s:4:"file";s:44:"2020/03/sredyaya-kartinka-e1583064965160.jpg";
s:5:"sizes";a:4:{s:6:"medium";a:4:{s:4:"file";s:44:"sredyaya-kartinka-e1583064965160-300x188.jpg";
s:5:"width";i:300;s:6:"height";i:188;s:9:"mime-type";s:10:"image/jpeg";}
s:5:"large";a:4:{s:4:"file";s:30:"sredyaya-kartinka-1024x576.jpg";s:5:"width";i:1024;s:6:"height";
i:576;s:9:"mime-type";s:10:"image/jpeg";}s:9:"thumbnail";a:4:{s:4:"file";s:44:"sredyaya-kartinka-e1583064965160-150x150.jpg";
s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}
s:12:"medium_large";a:4:{s:4:"file";s:29:"sredyaya-kartinka-768x432.jpg";s:5:"width";i:768;
s:6:"height";i:432;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";
a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";
s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";
s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}
		)

	[_edit_lock] => Array
		(
			[0] => 1583080079:1
		)

	[_edit_last] => Array
		(
			[0] => 1
		)

	[_wp_attachment_backup_sizes] => Array
		(
			[0] => a:5:{s:9:"full-orig";a:3:{s:5:"width";i:1280;s:6:"height";i:720;s:4:"file";s:21:"sredyaya-kartinka.jpg";}
s:14:"thumbnail-orig";a:4:{s:4:"file";s:29:"sredyaya-kartinka-150x150.jpg";
s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:11:"medium-orig";
a:4:{s:4:"file";s:29:"sredyaya-kartinka-300x169.jpg";s:5:"width";i:300;s:6:"height";i:169;s:9:"mime-type";s:10:"image/jpeg";}
s:17:"medium_large-orig";a:4:{s:4:"file";s:29:"sredyaya-kartinka-768x432.jpg";s:5:"width";i:768;
s:6:"height";i:432;s:9:"mime-type";s:10:"image/jpeg";}s:10:"large-orig";a:4:{s:4:"file";
s:30:"sredyaya-kartinka-1024x576.jpg";s:5:"width";i:1024;s:6:"height";i:576;s:9:"mime-type";s:10:"image/jpeg";}}
		)

)

wp_get_attachment_metadata -------------
Array
(
	[width] => 672
	[height] => 422
	[file] => 2020/03/sredyaya-kartinka-e1583064965160.jpg
	[sizes] => Array
		(
			[medium] => Array
				(
					[file] => sredyaya-kartinka-e1583064965160-300x188.jpg
					[width] => 300
					[height] => 188
					[mime-type] => image/jpeg
				)

			[large] => Array
				(
					[file] => sredyaya-kartinka-1024x576.jpg
					[width] => 1024
					[height] => 576
					[mime-type] => image/jpeg
				)

			[thumbnail] => Array
				(
					[file] => sredyaya-kartinka-e1583064965160-150x150.jpg
					[width] => 150
					[height] => 150
					[mime-type] => image/jpeg
				)

			[medium_large] => Array
				(
					[file] => sredyaya-kartinka-768x432.jpg
					[width] => 768
					[height] => 432
					[mime-type] => image/jpeg
				)

		)

	[image_meta] => Array
		(
			[aperture] => 0
			[credit] => 
			[camera] => 
			 => 
			[created_timestamp] => 0
			[copyright] => 
			[focal_length] => 0
			[iso] => 0
			[shutter_speed] => 0
			[title] => 
			[orientation] => 0
			[keywords] => Array
				(
				)

		)

)

_wp_attachment_backup_sizes -------------
Array
(
	[full-orig] => Array
		(
			[width] => 1280
			[height] => 720
			[file] => sredyaya-kartinka.jpg
		)

	[thumbnail-orig] => Array
		(
			[file] => sredyaya-kartinka-150x150.jpg
			[width] => 150
			[height] => 150
			[mime-type] => image/jpeg
		)

	[medium-orig] => Array
		(
			[file] => sredyaya-kartinka-300x169.jpg
			[width] => 300
			[height] => 169
			[mime-type] => image/jpeg
		)

	[medium_large-orig] => Array
		(
			[file] => sredyaya-kartinka-768x432.jpg
			[width] => 768
			[height] => 432
			[mime-type] => image/jpeg
		)

	[large-orig] => Array
		(
			[file] => sredyaya-kartinka-1024x576.jpg
			[width] => 1024
			[height] => 576
			[mime-type] => image/jpeg
		)

)
*/

Пример с большой фоткой (WP 5.3)

Просто загружена большая фотка с которой создались все возможные размеры.

/*
wp_get_original_image_url(): http://wptest.ru/wp-content/uploads/2020/03/jpg-big-image.jpeg
wp_get_original_image_path(): C:\sites\wptest.ru\www/wp-content/uploads/2020/03/jpg-big-image.jpeg
get_attached_file(): C:\sites\wptest.ru\www/wp-content/uploads/2020/03/jpg-big-image-scaled.jpeg

post_meta -------------
Array
(
	[_wp_attached_file] => Array
		(
			[0] => 2020/03/jpg-big-image-scaled.jpeg
		)

	[_wp_attachment_metadata] => Array
		(
			[0] => a:6:{s:5:"width";i:2560;s:6:"height";i:1707;s:4:"file";s:33:"2020/03/jpg-big-image-scaled.jpeg";s:5:"sizes";
a:6:{s:6:"medium";a:4:{s:4:"file";s:26:"jpg-big-image-300x200.jpeg";s:5:"width";i:300;s:6:"height";i:200;s:9:"mime-type";s:10:"image/jpeg";}
s:5:"large";a:4:{s:4:"file";s:27:"jpg-big-image-1024x683.jpeg";s:5:"width";i:1024;s:6:"height";i:683;s:9:"mime-type";s:10:"image/jpeg";}
s:9:"thumbnail";a:4:{s:4:"file";s:26:"jpg-big-image-150x150.jpeg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}
s:12:"medium_large";a:4:{s:4:"file";s:26:"jpg-big-image-768x512.jpeg";s:5:"width";i:768;s:6:"height";i:512;s:9:"mime-type";s:10:"image/jpeg";}
s:9:"1536x1536";a:4:{s:4:"file";s:28:"jpg-big-image-1536x1024.jpeg";s:5:"width";i:1536;s:6:"height";i:1024;s:9:"mime-type";s:10:"image/jpeg";}
s:9:"2048x2048";a:4:{s:4:"file";s:28:"jpg-big-image-2048x1365.jpeg";s:5:"width";i:2048;s:6:"height";i:1365;s:9:"mime-type";s:10:"image/jpeg";}}
s:10:"image_meta";a:12:{s:8:"aperture";s:2:"11";s:6:"credit";s:0:"";s:6:"camera";s:11:"NIKON D5300";s:7:"caption";s:0:"";s:17:"created_timestamp";
s:10:"1413130422";s:9:"copyright";s:0:"";s:12:"focal_length";s:2:"31";s:3:"iso";s:3:"320";s:13:"shutter_speed";s:5:"0.002";s:5:"title";s:0:"";s:11:"orientation";
s:1:"1";s:8:"keywords";a:0:{}}s:14:"original_image";s:18:"jpg-big-image.jpeg";}
		)

	[_edit_lock] => Array
		(
			[0] => 1583088196:1
		)

)

wp_get_attachment_metadata -------------
Array
(
	[width] => 2560
	[height] => 1707
	[file] => 2020/03/jpg-big-image-scaled.jpeg
	[sizes] => Array
		(
			[medium] => Array
				(
					[file] => jpg-big-image-300x200.jpeg
					[width] => 300
					[height] => 200
					[mime-type] => image/jpeg
				)

			[large] => Array
				(
					[file] => jpg-big-image-1024x683.jpeg
					[width] => 1024
					[height] => 683
					[mime-type] => image/jpeg
				)

			[thumbnail] => Array
				(
					[file] => jpg-big-image-150x150.jpeg
					[width] => 150
					[height] => 150
					[mime-type] => image/jpeg
				)

			[medium_large] => Array
				(
					[file] => jpg-big-image-768x512.jpeg
					[width] => 768
					[height] => 512
					[mime-type] => image/jpeg
				)

			[1536x1536] => Array
				(
					[file] => jpg-big-image-1536x1024.jpeg
					[width] => 1536
					[height] => 1024
					[mime-type] => image/jpeg
				)

			[2048x2048] => Array
				(
					[file] => jpg-big-image-2048x1365.jpeg
					[width] => 2048
					[height] => 1365
					[mime-type] => image/jpeg
				)

		)

	[image_meta] => Array (
			[aperture] => 11
			[credit] => 
			[camera] => NIKON D5300
			 => 
			[created_timestamp] => 1413130422
			[copyright] => 
			[focal_length] => 31
			[iso] => 320
			[shutter_speed] => 0.002
			[title] => 
			[orientation] => 1
			[keywords] => Array()
		)

	[original_image] => jpg-big-image.jpeg
)

_wp_attachment_backup_sizes -------------
пусто
*/

Функции вложений

Функции отвечающие за URL и пути вложений. Показана иерархия использования функций друг другом.

Группа:

Группа:

Комментариев нет