Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/image-op.../classes/image
File: wp-image-meta.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace ImageOptimization\Classes\Image;
[2] Fix | Delete
[3] Fix | Delete
use ImageOptimization\Classes\File_Utils;
[4] Fix | Delete
use ImageOptimization\Classes\Image\Exceptions\Invalid_Image_Exception;
[5] Fix | Delete
[6] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[7] Fix | Delete
exit; // Exit if accessed directly.
[8] Fix | Delete
}
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Class WP_Image_Meta
[12] Fix | Delete
*
[13] Fix | Delete
* This class is used to manage the metadata of an image in WordPress.
[14] Fix | Delete
*/
[15] Fix | Delete
class WP_Image_Meta {
[16] Fix | Delete
private int $image_id;
[17] Fix | Delete
private array $image_meta;
[18] Fix | Delete
private Image $image;
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Get the size data of the image.
[22] Fix | Delete
*
[23] Fix | Delete
* @param string $image_size The size of the image.
[24] Fix | Delete
* @return array|null The size data of the image or null if the size does not exist.
[25] Fix | Delete
*/
[26] Fix | Delete
public function get_size_data( string $image_size ): ?array {
[27] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[28] Fix | Delete
$output = [];
[29] Fix | Delete
[30] Fix | Delete
$output['file'] = $this->image_meta['file'];
[31] Fix | Delete
$output['filesize'] = $this->image_meta['filesize'];
[32] Fix | Delete
$output['width'] = $this->image_meta['width'];
[33] Fix | Delete
$output['height'] = $this->image_meta['height'];
[34] Fix | Delete
$output['mime-type'] = $this->image->get_attachment_object()->post_mime_type;
[35] Fix | Delete
[36] Fix | Delete
return $output;
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
if ( ! isset( $this->image_meta['sizes'][ $image_size ] ) ) {
[40] Fix | Delete
return null;
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
return $this->image_meta['sizes'][ $image_size ];
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Get the keys of the image sizes.
[48] Fix | Delete
*
[49] Fix | Delete
* @return array The keys of the image sizes.
[50] Fix | Delete
*/
[51] Fix | Delete
public function get_size_keys(): array {
[52] Fix | Delete
return [ Image::SIZE_FULL, ...array_keys( $this->image_meta['sizes'] ) ];
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Returns keys of sizes that have the same dimensions as the one provided.
[57] Fix | Delete
*
[58] Fix | Delete
* @param string $image_size The size of the image.
[59] Fix | Delete
*
[60] Fix | Delete
* @return array
[61] Fix | Delete
*/
[62] Fix | Delete
public function get_size_duplicates( string $image_size ): array {
[63] Fix | Delete
$duplicates = [];
[64] Fix | Delete
[65] Fix | Delete
$width = $this->get_width( $image_size );
[66] Fix | Delete
$height = $this->get_height( $image_size );
[67] Fix | Delete
[68] Fix | Delete
foreach ( $this->image_meta['sizes'] as $size_key => $size_data ) {
[69] Fix | Delete
if ( $size_data['width'] === $width && $size_data['height'] === $height && $image_size !== $size_key ) {
[70] Fix | Delete
$duplicates[] = $size_key;
[71] Fix | Delete
}
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
return array_unique( $duplicates );
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
/**
[78] Fix | Delete
* Get the file size of the image.
[79] Fix | Delete
*
[80] Fix | Delete
* @param string $image_size The size of the image.
[81] Fix | Delete
* @return int|null The file size of the image or null if the size does not exist.
[82] Fix | Delete
*/
[83] Fix | Delete
public function get_file_size( string $image_size ): ?int {
[84] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[85] Fix | Delete
return $this->image_meta['filesize'] ?? null;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
if ( ! isset( $this->image_meta['sizes'][ $image_size ]['filesize'] ) ) {
[89] Fix | Delete
return null;
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
return $this->image_meta['sizes'][ $image_size ]['filesize'];
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Set the file size of the image.
[97] Fix | Delete
*
[98] Fix | Delete
* @param string $image_size The size of the image.
[99] Fix | Delete
* @param int $file_size The file size to set.
[100] Fix | Delete
* @return WP_Image_Meta The current instance.
[101] Fix | Delete
*/
[102] Fix | Delete
public function set_file_size( string $image_size, int $file_size ): WP_Image_Meta {
[103] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[104] Fix | Delete
$this->image_meta['filesize'] = $file_size;
[105] Fix | Delete
return $this;
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
$this->maybe_add_new_size( $image_size );
[109] Fix | Delete
[110] Fix | Delete
$this->image_meta['sizes'][ $image_size ]['filesize'] = $file_size;
[111] Fix | Delete
[112] Fix | Delete
return $this;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Set the file path of the image.
[117] Fix | Delete
*
[118] Fix | Delete
* @param string $image_size The size of the image.
[119] Fix | Delete
* @param string $full_path The full path to set.
[120] Fix | Delete
* @return WP_Image_Meta The current instance.
[121] Fix | Delete
*/
[122] Fix | Delete
public function set_file_path( string $image_size, string $full_path ): WP_Image_Meta {
[123] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[124] Fix | Delete
$this->image_meta['file'] = File_Utils::get_relative_upload_path( $full_path );
[125] Fix | Delete
return $this;
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
$this->maybe_add_new_size( $image_size );
[129] Fix | Delete
[130] Fix | Delete
$this->image_meta['sizes'][ $image_size ]['file'] = File_Utils::get_basename( $full_path );
[131] Fix | Delete
[132] Fix | Delete
return $this;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* Get the width of the image.
[137] Fix | Delete
*
[138] Fix | Delete
* @param string $image_size The size of the image.
[139] Fix | Delete
*
[140] Fix | Delete
* @return int|null
[141] Fix | Delete
*/
[142] Fix | Delete
public function get_width( string $image_size ): ?int {
[143] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[144] Fix | Delete
return $this->image_meta['width'];
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
if ( ! isset( $this->image_meta['sizes'][ $image_size ]['width'] ) ) {
[148] Fix | Delete
return null;
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
return $this->image_meta['sizes'][ $image_size ]['width'];
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Set the width of the image.
[156] Fix | Delete
*
[157] Fix | Delete
* @param string $image_size The size of the image.
[158] Fix | Delete
* @param int $width The width to set.
[159] Fix | Delete
* @return WP_Image_Meta The current instance.
[160] Fix | Delete
*/
[161] Fix | Delete
public function set_width( string $image_size, int $width ): WP_Image_Meta {
[162] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[163] Fix | Delete
$this->image_meta['width'] = $width;
[164] Fix | Delete
return $this;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
$this->maybe_add_new_size( $image_size );
[168] Fix | Delete
[169] Fix | Delete
$this->image_meta['sizes'][ $image_size ]['width'] = $width;
[170] Fix | Delete
[171] Fix | Delete
return $this;
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Get the height of the image.
[176] Fix | Delete
*
[177] Fix | Delete
* @param string $image_size The size of the image.
[178] Fix | Delete
*
[179] Fix | Delete
* @return int|null
[180] Fix | Delete
*/
[181] Fix | Delete
public function get_height( string $image_size ): ?int {
[182] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[183] Fix | Delete
return $this->image_meta['height'];
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
if ( ! isset( $this->image_meta['sizes'][ $image_size ]['height'] ) ) {
[187] Fix | Delete
return null;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
return $this->image_meta['sizes'][ $image_size ]['height'];
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* Set the height of the image.
[195] Fix | Delete
*
[196] Fix | Delete
* @param string $image_size The size of the image.
[197] Fix | Delete
* @param int $height The height to set.
[198] Fix | Delete
* @return WP_Image_Meta The current instance.
[199] Fix | Delete
*/
[200] Fix | Delete
public function set_height( string $image_size, int $height ): WP_Image_Meta {
[201] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[202] Fix | Delete
$this->image_meta['height'] = $height;
[203] Fix | Delete
return $this;
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
$this->maybe_add_new_size( $image_size );
[207] Fix | Delete
[208] Fix | Delete
$this->image_meta['sizes'][ $image_size ]['height'] = $height;
[209] Fix | Delete
[210] Fix | Delete
return $this;
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* Set the mime type of the image.
[215] Fix | Delete
*
[216] Fix | Delete
* @param string $image_size The size of the image.
[217] Fix | Delete
* @param string $mime_type The mime type to set.
[218] Fix | Delete
* @return WP_Image_Meta The current instance.
[219] Fix | Delete
*/
[220] Fix | Delete
public function set_mime_type( string $image_size, string $mime_type ): WP_Image_Meta {
[221] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[222] Fix | Delete
// WP doesn't store it in meta for the original image
[223] Fix | Delete
return $this;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
$this->maybe_add_new_size( $image_size );
[227] Fix | Delete
[228] Fix | Delete
$this->image_meta['sizes'][ $image_size ]['mime-type'] = $mime_type;
[229] Fix | Delete
[230] Fix | Delete
return $this;
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Add a new size to the image if it does not exist.
[235] Fix | Delete
*
[236] Fix | Delete
* @param string $image_size The size of the image.
[237] Fix | Delete
*/
[238] Fix | Delete
private function maybe_add_new_size( $image_size ): void {
[239] Fix | Delete
if ( ! isset( $this->image_meta['sizes'][ $image_size ] ) ) {
[240] Fix | Delete
$this->image_meta['sizes'][ $image_size ] = [];
[241] Fix | Delete
}
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
/**
[245] Fix | Delete
* Save the image metadata.
[246] Fix | Delete
*
[247] Fix | Delete
* @return WP_Image_Meta The current instance.
[248] Fix | Delete
*/
[249] Fix | Delete
public function save(): WP_Image_Meta {
[250] Fix | Delete
wp_update_attachment_metadata( $this->image_id, $this->image_meta );
[251] Fix | Delete
[252] Fix | Delete
return $this;
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
/**
[256] Fix | Delete
* Query the image metadata.
[257] Fix | Delete
*
[258] Fix | Delete
* @throws Invalid_Image_Exception
[259] Fix | Delete
*/
[260] Fix | Delete
private function query_meta(): void {
[261] Fix | Delete
$meta = wp_get_attachment_metadata( $this->image_id );
[262] Fix | Delete
[263] Fix | Delete
if ( ! $meta ) {
[264] Fix | Delete
throw new Invalid_Image_Exception( 'Invalid WP image meta' );
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
// Handle unsupported formats that WP doesn't create thumbnails for
[268] Fix | Delete
if ( ! isset( $meta['sizes'] ) ) {
[269] Fix | Delete
$meta['sizes'] = [];
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
$this->image_meta = $meta;
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* WP_Image_Meta constructor.
[277] Fix | Delete
*
[278] Fix | Delete
* @param int $image_id The ID of the image.
[279] Fix | Delete
* @param Image|null $image The image object.
[280] Fix | Delete
* @throws Invalid_Image_Exception If the image metadata is invalid.
[281] Fix | Delete
*/
[282] Fix | Delete
public function __construct( int $image_id, ?Image $image = null ) {
[283] Fix | Delete
$this->image_id = $image_id;
[284] Fix | Delete
$this->image = $image ?? new Image( $image_id );
[285] Fix | Delete
[286] Fix | Delete
$this->query_meta();
[287] Fix | Delete
}
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function