Edit File by line
/home/zeestwma/ceyloniy.../wp-admin/includes
File: image-edit.php
}
[500] Fix | Delete
return false;
[501] Fix | Delete
case 'image/avif':
[502] Fix | Delete
if ( function_exists( 'imageavif' ) ) {
[503] Fix | Delete
return imageavif( $image, $filename );
[504] Fix | Delete
}
[505] Fix | Delete
return false;
[506] Fix | Delete
default:
[507] Fix | Delete
return false;
[508] Fix | Delete
}
[509] Fix | Delete
}
[510] Fix | Delete
}
[511] Fix | Delete
[512] Fix | Delete
/**
[513] Fix | Delete
* Image preview ratio. Internal use only.
[514] Fix | Delete
*
[515] Fix | Delete
* @since 2.9.0
[516] Fix | Delete
*
[517] Fix | Delete
* @ignore
[518] Fix | Delete
* @param int $w Image width in pixels.
[519] Fix | Delete
* @param int $h Image height in pixels.
[520] Fix | Delete
* @return float|int Image preview ratio.
[521] Fix | Delete
*/
[522] Fix | Delete
function _image_get_preview_ratio( $w, $h ) {
[523] Fix | Delete
$max = max( $w, $h );
[524] Fix | Delete
return $max > 600 ? ( 600 / $max ) : 1;
[525] Fix | Delete
}
[526] Fix | Delete
[527] Fix | Delete
/**
[528] Fix | Delete
* Returns an image resource. Internal use only.
[529] Fix | Delete
*
[530] Fix | Delete
* @since 2.9.0
[531] Fix | Delete
* @deprecated 3.5.0 Use WP_Image_Editor::rotate()
[532] Fix | Delete
* @see WP_Image_Editor::rotate()
[533] Fix | Delete
*
[534] Fix | Delete
* @ignore
[535] Fix | Delete
* @param resource|GdImage $img Image resource.
[536] Fix | Delete
* @param float|int $angle Image rotation angle, in degrees.
[537] Fix | Delete
* @return resource|GdImage|false GD image resource or GdImage instance, false otherwise.
[538] Fix | Delete
*/
[539] Fix | Delete
function _rotate_image_resource( $img, $angle ) {
[540] Fix | Delete
_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
[541] Fix | Delete
[542] Fix | Delete
if ( function_exists( 'imagerotate' ) ) {
[543] Fix | Delete
$rotated = imagerotate( $img, $angle, 0 );
[544] Fix | Delete
[545] Fix | Delete
if ( is_gd_image( $rotated ) ) {
[546] Fix | Delete
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
[547] Fix | Delete
imagedestroy( $img );
[548] Fix | Delete
}
[549] Fix | Delete
[550] Fix | Delete
$img = $rotated;
[551] Fix | Delete
}
[552] Fix | Delete
}
[553] Fix | Delete
[554] Fix | Delete
return $img;
[555] Fix | Delete
}
[556] Fix | Delete
[557] Fix | Delete
/**
[558] Fix | Delete
* Flips an image resource. Internal use only.
[559] Fix | Delete
*
[560] Fix | Delete
* @since 2.9.0
[561] Fix | Delete
* @deprecated 3.5.0 Use WP_Image_Editor::flip()
[562] Fix | Delete
* @see WP_Image_Editor::flip()
[563] Fix | Delete
*
[564] Fix | Delete
* @ignore
[565] Fix | Delete
* @param resource|GdImage $img Image resource or GdImage instance.
[566] Fix | Delete
* @param bool $horz Whether to flip horizontally.
[567] Fix | Delete
* @param bool $vert Whether to flip vertically.
[568] Fix | Delete
* @return resource|GdImage (maybe) flipped image resource or GdImage instance.
[569] Fix | Delete
*/
[570] Fix | Delete
function _flip_image_resource( $img, $horz, $vert ) {
[571] Fix | Delete
_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
[572] Fix | Delete
[573] Fix | Delete
$w = imagesx( $img );
[574] Fix | Delete
$h = imagesy( $img );
[575] Fix | Delete
$dst = wp_imagecreatetruecolor( $w, $h );
[576] Fix | Delete
[577] Fix | Delete
if ( is_gd_image( $dst ) ) {
[578] Fix | Delete
$sx = $vert ? ( $w - 1 ) : 0;
[579] Fix | Delete
$sy = $horz ? ( $h - 1 ) : 0;
[580] Fix | Delete
$sw = $vert ? -$w : $w;
[581] Fix | Delete
$sh = $horz ? -$h : $h;
[582] Fix | Delete
[583] Fix | Delete
if ( imagecopyresampled( $dst, $img, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
[584] Fix | Delete
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
[585] Fix | Delete
imagedestroy( $img );
[586] Fix | Delete
}
[587] Fix | Delete
[588] Fix | Delete
$img = $dst;
[589] Fix | Delete
}
[590] Fix | Delete
}
[591] Fix | Delete
[592] Fix | Delete
return $img;
[593] Fix | Delete
}
[594] Fix | Delete
[595] Fix | Delete
/**
[596] Fix | Delete
* Crops an image resource. Internal use only.
[597] Fix | Delete
*
[598] Fix | Delete
* @since 2.9.0
[599] Fix | Delete
*
[600] Fix | Delete
* @ignore
[601] Fix | Delete
* @param resource|GdImage $img Image resource or GdImage instance.
[602] Fix | Delete
* @param float $x Source point x-coordinate.
[603] Fix | Delete
* @param float $y Source point y-coordinate.
[604] Fix | Delete
* @param float $w Source width.
[605] Fix | Delete
* @param float $h Source height.
[606] Fix | Delete
* @return resource|GdImage (maybe) cropped image resource or GdImage instance.
[607] Fix | Delete
*/
[608] Fix | Delete
function _crop_image_resource( $img, $x, $y, $w, $h ) {
[609] Fix | Delete
$dst = wp_imagecreatetruecolor( $w, $h );
[610] Fix | Delete
[611] Fix | Delete
if ( is_gd_image( $dst ) ) {
[612] Fix | Delete
if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
[613] Fix | Delete
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
[614] Fix | Delete
imagedestroy( $img );
[615] Fix | Delete
}
[616] Fix | Delete
[617] Fix | Delete
$img = $dst;
[618] Fix | Delete
}
[619] Fix | Delete
}
[620] Fix | Delete
[621] Fix | Delete
return $img;
[622] Fix | Delete
}
[623] Fix | Delete
[624] Fix | Delete
/**
[625] Fix | Delete
* Performs group of changes on Editor specified.
[626] Fix | Delete
*
[627] Fix | Delete
* @since 2.9.0
[628] Fix | Delete
*
[629] Fix | Delete
* @param WP_Image_Editor $image WP_Image_Editor instance.
[630] Fix | Delete
* @param array $changes Array of change operations.
[631] Fix | Delete
* @return WP_Image_Editor WP_Image_Editor instance with changes applied.
[632] Fix | Delete
*/
[633] Fix | Delete
function image_edit_apply_changes( $image, $changes ) {
[634] Fix | Delete
if ( is_gd_image( $image ) ) {
[635] Fix | Delete
/* translators: 1: $image, 2: WP_Image_Editor */
[636] Fix | Delete
_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
[637] Fix | Delete
}
[638] Fix | Delete
[639] Fix | Delete
if ( ! is_array( $changes ) ) {
[640] Fix | Delete
return $image;
[641] Fix | Delete
}
[642] Fix | Delete
[643] Fix | Delete
// Expand change operations.
[644] Fix | Delete
foreach ( $changes as $key => $obj ) {
[645] Fix | Delete
if ( isset( $obj->r ) ) {
[646] Fix | Delete
$obj->type = 'rotate';
[647] Fix | Delete
$obj->angle = $obj->r;
[648] Fix | Delete
unset( $obj->r );
[649] Fix | Delete
} elseif ( isset( $obj->f ) ) {
[650] Fix | Delete
$obj->type = 'flip';
[651] Fix | Delete
$obj->axis = $obj->f;
[652] Fix | Delete
unset( $obj->f );
[653] Fix | Delete
} elseif ( isset( $obj->c ) ) {
[654] Fix | Delete
$obj->type = 'crop';
[655] Fix | Delete
$obj->sel = $obj->c;
[656] Fix | Delete
unset( $obj->c );
[657] Fix | Delete
}
[658] Fix | Delete
[659] Fix | Delete
$changes[ $key ] = $obj;
[660] Fix | Delete
}
[661] Fix | Delete
[662] Fix | Delete
// Combine operations.
[663] Fix | Delete
if ( count( $changes ) > 1 ) {
[664] Fix | Delete
$filtered = array( $changes[0] );
[665] Fix | Delete
[666] Fix | Delete
for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) {
[667] Fix | Delete
$combined = false;
[668] Fix | Delete
[669] Fix | Delete
if ( $filtered[ $i ]->type === $changes[ $j ]->type ) {
[670] Fix | Delete
switch ( $filtered[ $i ]->type ) {
[671] Fix | Delete
case 'rotate':
[672] Fix | Delete
$filtered[ $i ]->angle += $changes[ $j ]->angle;
[673] Fix | Delete
$combined = true;
[674] Fix | Delete
break;
[675] Fix | Delete
case 'flip':
[676] Fix | Delete
$filtered[ $i ]->axis ^= $changes[ $j ]->axis;
[677] Fix | Delete
$combined = true;
[678] Fix | Delete
break;
[679] Fix | Delete
}
[680] Fix | Delete
}
[681] Fix | Delete
[682] Fix | Delete
if ( ! $combined ) {
[683] Fix | Delete
$filtered[ ++$i ] = $changes[ $j ];
[684] Fix | Delete
}
[685] Fix | Delete
}
[686] Fix | Delete
[687] Fix | Delete
$changes = $filtered;
[688] Fix | Delete
unset( $filtered );
[689] Fix | Delete
}
[690] Fix | Delete
[691] Fix | Delete
// Image resource before applying the changes.
[692] Fix | Delete
if ( $image instanceof WP_Image_Editor ) {
[693] Fix | Delete
[694] Fix | Delete
/**
[695] Fix | Delete
* Filters the WP_Image_Editor instance before applying changes to the image.
[696] Fix | Delete
*
[697] Fix | Delete
* @since 3.5.0
[698] Fix | Delete
*
[699] Fix | Delete
* @param WP_Image_Editor $image WP_Image_Editor instance.
[700] Fix | Delete
* @param array $changes Array of change operations.
[701] Fix | Delete
*/
[702] Fix | Delete
$image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
[703] Fix | Delete
} elseif ( is_gd_image( $image ) ) {
[704] Fix | Delete
[705] Fix | Delete
/**
[706] Fix | Delete
* Filters the GD image resource before applying changes to the image.
[707] Fix | Delete
*
[708] Fix | Delete
* @since 2.9.0
[709] Fix | Delete
* @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead.
[710] Fix | Delete
*
[711] Fix | Delete
* @param resource|GdImage $image GD image resource or GdImage instance.
[712] Fix | Delete
* @param array $changes Array of change operations.
[713] Fix | Delete
*/
[714] Fix | Delete
$image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
[715] Fix | Delete
}
[716] Fix | Delete
[717] Fix | Delete
foreach ( $changes as $operation ) {
[718] Fix | Delete
switch ( $operation->type ) {
[719] Fix | Delete
case 'rotate':
[720] Fix | Delete
if ( 0 !== $operation->angle ) {
[721] Fix | Delete
if ( $image instanceof WP_Image_Editor ) {
[722] Fix | Delete
$image->rotate( $operation->angle );
[723] Fix | Delete
} else {
[724] Fix | Delete
$image = _rotate_image_resource( $image, $operation->angle );
[725] Fix | Delete
}
[726] Fix | Delete
}
[727] Fix | Delete
break;
[728] Fix | Delete
case 'flip':
[729] Fix | Delete
if ( 0 !== $operation->axis ) {
[730] Fix | Delete
if ( $image instanceof WP_Image_Editor ) {
[731] Fix | Delete
$image->flip( ( $operation->axis & 1 ) !== 0, ( $operation->axis & 2 ) !== 0 );
[732] Fix | Delete
} else {
[733] Fix | Delete
$image = _flip_image_resource( $image, ( $operation->axis & 1 ) !== 0, ( $operation->axis & 2 ) !== 0 );
[734] Fix | Delete
}
[735] Fix | Delete
}
[736] Fix | Delete
break;
[737] Fix | Delete
case 'crop':
[738] Fix | Delete
$sel = $operation->sel;
[739] Fix | Delete
[740] Fix | Delete
if ( $image instanceof WP_Image_Editor ) {
[741] Fix | Delete
$size = $image->get_size();
[742] Fix | Delete
$w = $size['width'];
[743] Fix | Delete
$h = $size['height'];
[744] Fix | Delete
[745] Fix | Delete
$scale = isset( $sel->r ) ? $sel->r : 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling.
[746] Fix | Delete
$image->crop( (int) ( $sel->x * $scale ), (int) ( $sel->y * $scale ), (int) ( $sel->w * $scale ), (int) ( $sel->h * $scale ) );
[747] Fix | Delete
} else {
[748] Fix | Delete
$scale = isset( $sel->r ) ? $sel->r : 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling.
[749] Fix | Delete
$image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
[750] Fix | Delete
}
[751] Fix | Delete
break;
[752] Fix | Delete
}
[753] Fix | Delete
}
[754] Fix | Delete
[755] Fix | Delete
return $image;
[756] Fix | Delete
}
[757] Fix | Delete
[758] Fix | Delete
[759] Fix | Delete
/**
[760] Fix | Delete
* Streams image in post to browser, along with enqueued changes
[761] Fix | Delete
* in `$_REQUEST['history']`.
[762] Fix | Delete
*
[763] Fix | Delete
* @since 2.9.0
[764] Fix | Delete
*
[765] Fix | Delete
* @param int $post_id Attachment post ID.
[766] Fix | Delete
* @return bool True on success, false on failure.
[767] Fix | Delete
*/
[768] Fix | Delete
function stream_preview_image( $post_id ) {
[769] Fix | Delete
$post = get_post( $post_id );
[770] Fix | Delete
[771] Fix | Delete
wp_raise_memory_limit( 'admin' );
[772] Fix | Delete
[773] Fix | Delete
$img = wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
[774] Fix | Delete
[775] Fix | Delete
if ( is_wp_error( $img ) ) {
[776] Fix | Delete
return false;
[777] Fix | Delete
}
[778] Fix | Delete
[779] Fix | Delete
$changes = ! empty( $_REQUEST['history'] ) ? json_decode( wp_unslash( $_REQUEST['history'] ) ) : null;
[780] Fix | Delete
if ( $changes ) {
[781] Fix | Delete
$img = image_edit_apply_changes( $img, $changes );
[782] Fix | Delete
}
[783] Fix | Delete
[784] Fix | Delete
// Scale the image.
[785] Fix | Delete
$size = $img->get_size();
[786] Fix | Delete
$w = $size['width'];
[787] Fix | Delete
$h = $size['height'];
[788] Fix | Delete
[789] Fix | Delete
$ratio = _image_get_preview_ratio( $w, $h );
[790] Fix | Delete
$w2 = max( 1, $w * $ratio );
[791] Fix | Delete
$h2 = max( 1, $h * $ratio );
[792] Fix | Delete
[793] Fix | Delete
if ( is_wp_error( $img->resize( $w2, $h2 ) ) ) {
[794] Fix | Delete
return false;
[795] Fix | Delete
}
[796] Fix | Delete
[797] Fix | Delete
return wp_stream_image( $img, $post->post_mime_type, $post_id );
[798] Fix | Delete
}
[799] Fix | Delete
[800] Fix | Delete
/**
[801] Fix | Delete
* Restores the metadata for a given attachment.
[802] Fix | Delete
*
[803] Fix | Delete
* @since 2.9.0
[804] Fix | Delete
*
[805] Fix | Delete
* @param int $post_id Attachment post ID.
[806] Fix | Delete
* @return stdClass Image restoration message object.
[807] Fix | Delete
*/
[808] Fix | Delete
function wp_restore_image( $post_id ) {
[809] Fix | Delete
$meta = wp_get_attachment_metadata( $post_id );
[810] Fix | Delete
$file = get_attached_file( $post_id );
[811] Fix | Delete
$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
[812] Fix | Delete
$old_backup_sizes = $backup_sizes;
[813] Fix | Delete
$restored = false;
[814] Fix | Delete
$msg = new stdClass();
[815] Fix | Delete
[816] Fix | Delete
if ( ! is_array( $backup_sizes ) ) {
[817] Fix | Delete
$msg->error = __( 'Cannot load image metadata.' );
[818] Fix | Delete
return $msg;
[819] Fix | Delete
}
[820] Fix | Delete
[821] Fix | Delete
$parts = pathinfo( $file );
[822] Fix | Delete
$suffix = time() . rand( 100, 999 );
[823] Fix | Delete
$default_sizes = get_intermediate_image_sizes();
[824] Fix | Delete
[825] Fix | Delete
if ( isset( $backup_sizes['full-orig'] ) && is_array( $backup_sizes['full-orig'] ) ) {
[826] Fix | Delete
$data = $backup_sizes['full-orig'];
[827] Fix | Delete
[828] Fix | Delete
if ( $parts['basename'] !== $data['file'] ) {
[829] Fix | Delete
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
[830] Fix | Delete
// Delete only if it's an edited image.
[831] Fix | Delete
if ( preg_match( '/-e[0-9]{13}\./', $parts['basename'] ) ) {
[832] Fix | Delete
wp_delete_file( $file );
[833] Fix | Delete
}
[834] Fix | Delete
} elseif ( isset( $meta['width'], $meta['height'] ) ) {
[835] Fix | Delete
$backup_sizes[ "full-$suffix" ] = array(
[836] Fix | Delete
'width' => $meta['width'],
[837] Fix | Delete
'height' => $meta['height'],
[838] Fix | Delete
'filesize' => $meta['filesize'],
[839] Fix | Delete
'file' => $parts['basename'],
[840] Fix | Delete
);
[841] Fix | Delete
}
[842] Fix | Delete
}
[843] Fix | Delete
[844] Fix | Delete
$restored_file = path_join( $parts['dirname'], $data['file'] );
[845] Fix | Delete
$restored = update_attached_file( $post_id, $restored_file );
[846] Fix | Delete
[847] Fix | Delete
$meta['file'] = _wp_relative_upload_path( $restored_file );
[848] Fix | Delete
$meta['width'] = $data['width'];
[849] Fix | Delete
$meta['height'] = $data['height'];
[850] Fix | Delete
if ( isset( $data['filesize'] ) ) {
[851] Fix | Delete
/*
[852] Fix | Delete
* Restore the original filesize if it was backed up.
[853] Fix | Delete
*
[854] Fix | Delete
* See https://core.trac.wordpress.org/ticket/59684.
[855] Fix | Delete
*/
[856] Fix | Delete
$meta['filesize'] = $data['filesize'];
[857] Fix | Delete
}
[858] Fix | Delete
}
[859] Fix | Delete
[860] Fix | Delete
foreach ( $default_sizes as $default_size ) {
[861] Fix | Delete
if ( isset( $backup_sizes[ "$default_size-orig" ] ) ) {
[862] Fix | Delete
$data = $backup_sizes[ "$default_size-orig" ];
[863] Fix | Delete
[864] Fix | Delete
if ( isset( $meta['sizes'][ $default_size ] ) && $meta['sizes'][ $default_size ]['file'] !== $data['file'] ) {
[865] Fix | Delete
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
[866] Fix | Delete
// Delete only if it's an edited image.
[867] Fix | Delete
if ( preg_match( '/-e[0-9]{13}-/', $meta['sizes'][ $default_size ]['file'] ) ) {
[868] Fix | Delete
$delete_file = path_join( $parts['dirname'], $meta['sizes'][ $default_size ]['file'] );
[869] Fix | Delete
wp_delete_file( $delete_file );
[870] Fix | Delete
}
[871] Fix | Delete
} else {
[872] Fix | Delete
$backup_sizes[ "$default_size-{$suffix}" ] = $meta['sizes'][ $default_size ];
[873] Fix | Delete
}
[874] Fix | Delete
}
[875] Fix | Delete
[876] Fix | Delete
$meta['sizes'][ $default_size ] = $data;
[877] Fix | Delete
} else {
[878] Fix | Delete
unset( $meta['sizes'][ $default_size ] );
[879] Fix | Delete
}
[880] Fix | Delete
}
[881] Fix | Delete
[882] Fix | Delete
if ( ! wp_update_attachment_metadata( $post_id, $meta )
[883] Fix | Delete
|| ( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) )
[884] Fix | Delete
) {
[885] Fix | Delete
$msg->error = __( 'Cannot save image metadata.' );
[886] Fix | Delete
return $msg;
[887] Fix | Delete
}
[888] Fix | Delete
[889] Fix | Delete
if ( ! $restored ) {
[890] Fix | Delete
$msg->error = __( 'Image metadata is inconsistent.' );
[891] Fix | Delete
} else {
[892] Fix | Delete
$msg->msg = __( 'Image restored successfully.' );
[893] Fix | Delete
[894] Fix | Delete
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
[895] Fix | Delete
delete_post_meta( $post_id, '_wp_attachment_backup_sizes' );
[896] Fix | Delete
}
[897] Fix | Delete
}
[898] Fix | Delete
[899] Fix | Delete
return $msg;
[900] Fix | Delete
}
[901] Fix | Delete
[902] Fix | Delete
/**
[903] Fix | Delete
* Saves image to post, along with enqueued changes
[904] Fix | Delete
* in `$_REQUEST['history']`.
[905] Fix | Delete
*
[906] Fix | Delete
* @since 2.9.0
[907] Fix | Delete
*
[908] Fix | Delete
* @param int $post_id Attachment post ID.
[909] Fix | Delete
* @return stdClass
[910] Fix | Delete
*/
[911] Fix | Delete
function wp_save_image( $post_id ) {
[912] Fix | Delete
$_wp_additional_image_sizes = wp_get_additional_image_sizes();
[913] Fix | Delete
[914] Fix | Delete
$return = new stdClass();
[915] Fix | Delete
$success = false;
[916] Fix | Delete
$delete = false;
[917] Fix | Delete
$scaled = false;
[918] Fix | Delete
$nocrop = false;
[919] Fix | Delete
$post = get_post( $post_id );
[920] Fix | Delete
[921] Fix | Delete
$img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) );
[922] Fix | Delete
[923] Fix | Delete
if ( is_wp_error( $img ) ) {
[924] Fix | Delete
$return->error = esc_js( __( 'Unable to create new image.' ) );
[925] Fix | Delete
return $return;
[926] Fix | Delete
}
[927] Fix | Delete
[928] Fix | Delete
$full_width = ! empty( $_REQUEST['fwidth'] ) ? (int) $_REQUEST['fwidth'] : 0;
[929] Fix | Delete
$full_height = ! empty( $_REQUEST['fheight'] ) ? (int) $_REQUEST['fheight'] : 0;
[930] Fix | Delete
$target = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : '';
[931] Fix | Delete
$scale = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do'];
[932] Fix | Delete
[933] Fix | Delete
/** This filter is documented in wp-admin/includes/image-edit.php */
[934] Fix | Delete
$edit_thumbnails_separately = (bool) apply_filters( 'image_edit_thumbnails_separately', false );
[935] Fix | Delete
[936] Fix | Delete
if ( $scale ) {
[937] Fix | Delete
$size = $img->get_size();
[938] Fix | Delete
$original_width = $size['width'];
[939] Fix | Delete
$original_height = $size['height'];
[940] Fix | Delete
[941] Fix | Delete
if ( $full_width > $original_width || $full_height > $original_height ) {
[942] Fix | Delete
$return->error = esc_js( __( 'Images cannot be scaled to a size larger than the original.' ) );
[943] Fix | Delete
return $return;
[944] Fix | Delete
}
[945] Fix | Delete
[946] Fix | Delete
if ( $full_width > 0 && $full_height > 0 ) {
[947] Fix | Delete
// Check if it has roughly the same w / h ratio.
[948] Fix | Delete
$diff = round( $original_width / $original_height, 2 ) - round( $full_width / $full_height, 2 );
[949] Fix | Delete
if ( -0.1 < $diff && $diff < 0.1 ) {
[950] Fix | Delete
// Scale the full size image.
[951] Fix | Delete
if ( $img->resize( $full_width, $full_height ) ) {
[952] Fix | Delete
$scaled = true;
[953] Fix | Delete
}
[954] Fix | Delete
}
[955] Fix | Delete
[956] Fix | Delete
if ( ! $scaled ) {
[957] Fix | Delete
$return->error = esc_js( __( 'Error while saving the scaled image. Please reload the page and try again.' ) );
[958] Fix | Delete
return $return;
[959] Fix | Delete
}
[960] Fix | Delete
}
[961] Fix | Delete
} elseif ( ! empty( $_REQUEST['history'] ) ) {
[962] Fix | Delete
$changes = json_decode( wp_unslash( $_REQUEST['history'] ) );
[963] Fix | Delete
if ( $changes ) {
[964] Fix | Delete
$img = image_edit_apply_changes( $img, $changes );
[965] Fix | Delete
}
[966] Fix | Delete
} else {
[967] Fix | Delete
$return->error = esc_js( __( 'Nothing to save, the image has not changed.' ) );
[968] Fix | Delete
return $return;
[969] Fix | Delete
}
[970] Fix | Delete
[971] Fix | Delete
$meta = wp_get_attachment_metadata( $post_id );
[972] Fix | Delete
$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
[973] Fix | Delete
[974] Fix | Delete
if ( ! is_array( $meta ) ) {
[975] Fix | Delete
$return->error = esc_js( __( 'Image data does not exist. Please re-upload the image.' ) );
[976] Fix | Delete
return $return;
[977] Fix | Delete
}
[978] Fix | Delete
[979] Fix | Delete
if ( ! is_array( $backup_sizes ) ) {
[980] Fix | Delete
$backup_sizes = array();
[981] Fix | Delete
}
[982] Fix | Delete
[983] Fix | Delete
// Generate new filename.
[984] Fix | Delete
$path = get_attached_file( $post_id );
[985] Fix | Delete
[986] Fix | Delete
$basename = pathinfo( $path, PATHINFO_BASENAME );
[987] Fix | Delete
$dirname = pathinfo( $path, PATHINFO_DIRNAME );
[988] Fix | Delete
$ext = pathinfo( $path, PATHINFO_EXTENSION );
[989] Fix | Delete
$filename = pathinfo( $path, PATHINFO_FILENAME );
[990] Fix | Delete
$suffix = time() . rand( 100, 999 );
[991] Fix | Delete
[992] Fix | Delete
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE
[993] Fix | Delete
&& isset( $backup_sizes['full-orig'] ) && $backup_sizes['full-orig']['file'] !== $basename
[994] Fix | Delete
) {
[995] Fix | Delete
[996] Fix | Delete
if ( $edit_thumbnails_separately && 'thumbnail' === $target ) {
[997] Fix | Delete
$new_path = "{$dirname}/{$filename}-temp.{$ext}";
[998] Fix | Delete
} else {
[999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function