diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index 0861ebd1d..3a697c70f 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -574,9 +574,10 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation // Check size and correct if string or size. if ( is_string( $size ) || ( is_array( $size ) && 3 === count( $size ) ) ) { $intermediate = image_get_intermediate_size( $attachment_id, $size ); - if ( is_array( $intermediate ) ) { - $size = $this->get_crop( $intermediate['url'], $attachment_id ); + if ( ! is_array( $intermediate ) ) { + return false; } + $size = $this->get_crop( $intermediate['url'], $attachment_id ); } /** diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php index 5c2b0890a..553b4117a 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php @@ -671,6 +671,45 @@ public function filter_image_block_pre_render( $block, $source_block ) { return $block; } + /** + * Attempt to set the width and height for SVGs. + * + * @param array|false $image The image details. + * @param int $attachment_id The attachment ID. + * @param string|int[] $size The requested image size. + * + * @return array|false + */ + public function filter_svg_image_size( $image, $attachment_id, $size ) { + if ( is_array( $image ) && preg_match('/\.svg$/i', $image[0] ) && $image[1] <= 1 ) { + $image[1] = $image[2] = null; + + if ( is_array( $size ) ) { + $image[1] = $size[0]; + $image[2] = $size[1]; + } elseif ( false !== ( $xml = simplexml_load_file( $image[0] ) ) ) { + $attr = $xml->attributes(); + $viewbox = explode( ' ', $attr->viewBox ); + + // Get width + if ( isset( $attr->width ) && preg_match( '/\d+/', $attr->width, $value ) ) { + $image[1] = (int) $value[0]; + } elseif ( 4 === count( $viewbox ) ) { + $image[1] = (int) $viewbox[2]; + } + + // Get height + if ( isset( $attr->height ) && preg_match( '/\d+/', $attr->height, $value ) ) { + $image[2] = (int) $value[0]; + } elseif ( 4 === count( $viewbox ) ) { + $image[2] = (int) $viewbox[3]; + } + } + } + + return $image; + } + /** * Setup hooks for the filters. */ @@ -709,5 +748,7 @@ function ( $type ) use ( $filter ) { // Filter for block rendering. add_filter( 'render_block_data', array( $this, 'filter_image_block_pre_render' ), 10, 2 ); + // Try to get SVGs size. + add_filter( 'wp_get_attachment_image_src', array( $this, 'filter_svg_image_size' ), 10, 3 ); } } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 3316e0ac3..527fd68dd 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -378,8 +378,9 @@ public function prepare_upload( $post, $down_sync = false ) { $resource_type = $this->get_resource_type( $post ); $max_size = ( 'image' === $resource_type ? 'image_max_size_bytes' : 'video_max_size_bytes' ); - if ( $file_size > $this->plugin->components['connect']->usage['media_limits'][ $max_size ] ) { - $max_size_hr = size_format( $this->plugin->components['connect']->usage['media_limits'][ $max_size ] ); + if ( ! empty( $this->plugin->components['connect']->usage[ $max_size ] ) && $file_size > $this->plugin->components['connect']->usage[ $max_size ] ) { + $max_size_hr = size_format( $this->plugin->components['connect']->usage[ $max_size ] ); + // translators: variable is file size. $error = sprintf( __( 'File size exceeds the maximum of %s. This media asset will be served from WordPress.', 'cloudinary' ), $max_size_hr ); $this->plugin->components['media']->delete_post_meta( $post->ID, Sync::META_KEYS['pending'] ); // Remove Flag. diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php index d0b5a8f46..967549e1c 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php @@ -205,7 +205,7 @@ public function prep_on_demand_upload( $cloudinary_id, $attachment_id ) { $max_size = ( wp_attachment_is_image( $attachment_id ) ? 'image_max_size_bytes' : 'video_max_size_bytes' ); $file = get_attached_file( $attachment_id ); // Get the file size to make sure it can exist in cloudinary. - if ( file_exists( $file ) && filesize( $file ) < $this->plugin->components['connect']->usage['media_limits'][ $max_size ] ) { + if ( ! empty( $this->plugin->components['connect']->usage[ $max_size ] ) && file_exists( $file ) && filesize( $file ) < $this->plugin->components['connect']->usage[ $max_size ] ) { $this->add_to_sync( $attachment_id ); } else { // Check if the src is a url.