Adding image URL to post meta
-
Following is the code I’m working with. It generates @2x copies of thumbnails and deletes them upon deletion of original images. I’m looking for a way to add the @2x image path to post meta. Which should be deleted upon deletion of original image.
function ci_retina_image_make_intermediate_size($file, $width, $height, $crop=false) { if ( $width || $height ) { $resized_file = image_resize($file, $width*2, $height*2, $crop, $width . 'x' . $height . '@2x'); if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) { $resized_file = apply_filters('ci_retina_image_make_intermediate_size', $resized_file); return array( 'file' => wp_basename( $resized_file ), 'width' => $info[0], 'height' => $info[1], ); } } return false; } function ci_generate_retina_attachment_metadata( $metadata, $attachment_id ) { $attachment = get_post( $attachment_id ); $file = get_attached_file($attachment_id); $old_metadata = $metadata; foreach ($metadata as $k => $v) { if (is_array($v)) { foreach ($v as $key => $val) { if (is_array($val)) { ci_retina_image_make_intermediate_size($file, $val['width'], $val['height'], true); } } } } return $old_metadata; } add_filter('wp_generate_attachment_metadata', 'ci_generate_retina_attachment_metadata', 10, 2); function ci_delete_retina_images( $attachment_id ) { $ci_metas = wp_get_attachment_metadata( $attachment_id ); $ci_updir = wp_upload_dir(); $ci_path = pathinfo($ci_metas['file']); $ci_path_name = $ci_path['dirname']; $ci_updir = wp_upload_dir(); foreach ($ci_metas as $ci_meta => $ci_meta_val) { if ($ci_meta === "sizes") { foreach ($ci_meta_val as $ci_sizes => $ci_size) { $ci_original_filename = $ci_updir['basedir'] . "/" . $ci_path_name . "/" . $ci_size['file']; $ci_x2_filename = substr_replace($ci_original_filename, "@2x.", strrpos($ci_original_filename, "."), strlen(".")); if (file_exists($ci_x2_filename)) { unlink($ci_x2_filename); } } } } }
- The topic ‘Adding image URL to post meta’ is closed to new replies.