Skip to content

Commit 3d023b0

Browse files
authored
Merge pull request #214 from cloudinary/add/original-image-storage
Add/original image storage
2 parents 80e30d2 + 1e7712d commit 3d023b0

File tree

5 files changed

+107
-131
lines changed

5 files changed

+107
-131
lines changed

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ public function get_crop( $url, $attachment_id ) {
495495
// Make the WP Size array.
496496
$wp_size = array(
497497
'wpsize' => $size_name,
498+
'file' => $size['file'],
498499
'width' => $size['width'],
499500
'height' => $size['height'],
500501
'crop' => $cropped ? 'fill' : 'scale',
@@ -604,9 +605,6 @@ public function get_transformations_from_string( $str, $type = 'image' ) {
604605
$transformation_chains = explode( '/', $str );
605606
$transformations = array();
606607
foreach ( $transformation_chains as $index => $chain ) {
607-
if ( false !== strpos( $chain, 'wpsize' ) ) {
608-
continue; // A wpsize is not a transformation.
609-
}
610608
$items = explode( ',', $chain );
611609
foreach ( $items as $item ) {
612610
$item = trim( $item );
@@ -724,7 +722,7 @@ public function apply_default_transformations( array $transformations, $type = '
724722
*
725723
* @return string The converted URL.
726724
*/
727-
public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $overwrite_transformations = false, $clean = false ) {
725+
public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $overwrite_transformations = false ) {
728726

729727
if ( ! ( $cloudinary_id ) ) {
730728
$cloudinary_id = $this->cloudinary_id( $attachment_id );
@@ -744,13 +742,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
744742
'resource_type' => $resource_type,
745743
);
746744

747-
// Check size and correct if string or size.
748-
if ( is_string( $size ) || ( is_array( $size ) && 3 === count( $size ) ) ) {
749-
$intermediate = image_get_intermediate_size( $attachment_id, $size );
750-
if ( is_array( $intermediate ) ) {
751-
$size = $this->get_crop( $intermediate['url'], $attachment_id );
752-
}
753-
}
745+
$size = $this->prepare_size( $attachment_id, $size );
754746
if ( false === $overwrite_transformations ) {
755747
$overwrite_transformations = $this->maybe_overwrite_featured_image( $attachment_id );
756748
}
@@ -789,6 +781,44 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
789781
return apply_filters( 'cloudinary_converted_url', $url, $attachment_id, $pre_args );
790782
}
791783

784+
/**
785+
* Prepare the Size array for the Cloudinary URL API.
786+
*
787+
* @param int $attachment_id The attachment ID.
788+
* @param array|string $size The size array or slug.
789+
*
790+
* @return array|string
791+
*/
792+
public function prepare_size( $attachment_id, $size ) {
793+
// Check size and correct if string or size.
794+
if ( empty( $size ) || 'full' === $size ) {
795+
// Maybe get full size if scaled.
796+
$meta = wp_get_attachment_metadata( $attachment_id, true );
797+
if ( ! empty( $meta['original_image'] ) ) {
798+
$size = array(
799+
'width' => $meta['width'],
800+
'height' => $meta['height'],
801+
'full' => true,
802+
);
803+
}
804+
} elseif ( is_string( $size ) || ( is_array( $size ) && 3 === count( $size ) ) ) {
805+
$intermediate = image_get_intermediate_size( $attachment_id, $size );
806+
if ( is_array( $intermediate ) ) {
807+
$size = $this->get_crop( $intermediate['url'], $attachment_id );
808+
}
809+
} elseif ( array_keys( $size ) === array( 0, 1 ) ) {
810+
$size = array(
811+
'width' => $size[0],
812+
'height' => $size[1],
813+
);
814+
if ( $size['width'] === $size['height'] ) {
815+
$size['crop'] = 'fill';
816+
}
817+
}
818+
819+
return $size;
820+
}
821+
792822
/**
793823
* Add domain to subdir.
794824
*
@@ -998,7 +1028,7 @@ public function convert_url( $url, $attachment_id, $transformations = array(), $
9981028
}
9991029
$size = $this->get_crop( $url, $attachment_id );
10001030

1001-
return $this->cloudinary_url( $attachment_id, $size, $transformations, null, $overwrite_transformations, true );
1031+
return $this->cloudinary_url( $attachment_id, $size, $transformations, null, $overwrite_transformations );
10021032
}
10031033

10041034
/**
@@ -1017,8 +1047,8 @@ public function image_srcset( $sources, $size_array, $image_src, $image_meta, $a
10171047
if ( ! $cloudinary_id ) {
10181048
return $sources; // Return WordPress default sources.
10191049
}
1020-
// Get transformations from URL.
1021-
$transformations = $this->get_transformations_from_string( $image_src );
1050+
// Get transformations if any.
1051+
$transformations = $this->get_post_meta( $attachment_id, Sync::META_KEYS['transformation'], true );
10221052
// Use Cloudinary breakpoints for same ratio.
10231053

10241054
if ( 'on' === $this->plugin->config['settings']['global_transformations']['enable_breakpoints'] && wp_image_matches_ratio( $image_meta['width'], $image_meta['height'], $size_array[0], $size_array[1] ) ) {
@@ -1050,7 +1080,7 @@ function ( $item ) use ( $crop ) {
10501080
'width' => $breakpoint['width'],
10511081
);
10521082
$sources[ $breakpoint['width'] ] = array(
1053-
'url' => $this->cloudinary_url( $attachment_id, $size, $transformations, $cloudinary_id, true ),
1083+
'url' => $this->cloudinary_url( $attachment_id, $size, $transformations, $cloudinary_id, $image_meta['overwrite_transformations'] ),
10541084
'descriptor' => 'w',
10551085
'value' => $breakpoint['width'],
10561086
);
@@ -1076,35 +1106,13 @@ function ( $item ) use ( $crop ) {
10761106
// Use current sources, but convert the URLS.
10771107
foreach ( $sources as &$source ) {
10781108
if ( ! $this->is_cloudinary_url( $source['url'] ) ) {
1079-
$source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, true ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL.
1109+
$source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, $image_meta['overwrite_transformations'] ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL.
10801110
}
10811111
}
10821112

10831113
return $sources;
10841114
}
10851115

1086-
/**
1087-
* Alter the image sizes metadata to match the Cloudinary ID so that WordPress can detect a matched source for responsive breakpoints.
1088-
*
1089-
* @param array $image_meta The image metadata array.
1090-
* @param array $size_array The size array.
1091-
* @param string $image_src The image src.
1092-
* @param int $attachment_id The attachment ID.
1093-
*
1094-
* @return array
1095-
*/
1096-
public function match_responsive_sources( $image_meta, $size_array, $image_src, $attachment_id ) {
1097-
if ( wp_attachment_is_image( $attachment_id ) && ! empty( $image_meta['sizes'] ) ) {
1098-
$cloudinary_id = $this->cloudinary_id( $attachment_id );
1099-
if ( $cloudinary_id ) {
1100-
// Set the file to the Cloudinary ID so that it will be matched.
1101-
$image_meta['file'] = $cloudinary_id;
1102-
}
1103-
}
1104-
1105-
return $image_meta;
1106-
}
1107-
11081116
/**
11091117
* Check if a url is a cloudinary url or not.
11101118
*
@@ -1789,8 +1797,6 @@ public function setup() {
17891797

17901798
// Filter live URLS. (functions that return a URL).
17911799
add_filter( 'wp_calculate_image_srcset', array( $this, 'image_srcset' ), 10, 5 );
1792-
add_filter( 'wp_calculate_image_srcset_meta', array( $this, 'match_responsive_sources' ), 10, 4 );
1793-
add_filter( 'wp_get_attachment_metadata', array( $this, 'match_file_name_with_cloudinary_source' ), 10, 2 );
17941800
add_filter( 'wp_get_attachment_url', array( $this, 'attachment_url' ), 10, 2 );
17951801
add_filter( 'image_downsize', array( $this, 'filter_downsize' ), 10, 3 );
17961802

@@ -1802,23 +1808,4 @@ public function setup() {
18021808
add_action( 'begin_fetch_post_thumbnail_html', array( $this, 'set_doing_featured' ), 10, 2 );
18031809
}
18041810
}
1805-
1806-
/**
1807-
* Ensure the file in image meta is the same as the Cloudinary ID.
1808-
*
1809-
* @param array $image_meta Meta information of the attachment.
1810-
* @param int $attachment_id The attachment ID.
1811-
*
1812-
* @return array
1813-
*/
1814-
public function match_file_name_with_cloudinary_source( $image_meta, $attachment_id ) {
1815-
if ( $this->has_public_id( $attachment_id ) ) {
1816-
$cld_file = 'v' . $this->get_cloudinary_version( $attachment_id ) . '/' . $this->get_cloudinary_id( $attachment_id );
1817-
if ( false === strpos( $image_meta['file'], $cld_file ) ) {
1818-
$image_meta['file'] = $cld_file;
1819-
}
1820-
}
1821-
1822-
return $image_meta;
1823-
}
18241811
}

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public function get_signature( $attachment_id, $cached = true ) {
286286
public function generate_public_id( $attachment_id ) {
287287

288288
$cld_folder = $this->managers['media']->get_cloudinary_folder();
289-
$file = get_attached_file( $attachment_id );
289+
$file = wp_get_original_image_path( $attachment_id );
290290
$file_info = pathinfo( $file );
291291
$public_id = $cld_folder . $file_info['filename'];
292292

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ class Api {
9696
'pg' => 'page',
9797
'sp' => 'streaming_profile',
9898
'vs' => 'video_sampling',
99-
'$wpsize' => 'wpsize',
10099
),
101100
'video' => array(
102101
'w' => 'width',
@@ -141,7 +140,7 @@ class Api {
141140
* @param string The plugin version.
142141
*/
143142
public function __construct( $connect, $version ) {
144-
$this->credentials = $connect->get_credentials();
143+
$this->credentials = $connect->get_credentials();
145144
$this->plugin_version = $version;
146145
// Use CNAME.
147146
if ( ! empty( $this->credentials['cname'] ) ) {
@@ -172,8 +171,12 @@ public function url( $resource, $function = null, $endpoint = false ) {
172171
$parts[] = $this->credentials['cloud_name'];
173172
}
174173

175-
$parts[] = $resource;
176-
$parts[] = $function;
174+
if ( false === $endpoint && 'image' === $resource ) {
175+
$parts[] = 'images';
176+
} else {
177+
$parts[] = $resource;
178+
$parts[] = $function;
179+
}
177180

178181
$parts = array_filter( $parts );
179182
$url = implode( '/', $parts );
@@ -203,14 +206,6 @@ function ( $item ) use ( $transformation_index ) {
203206

204207
foreach ( $item as $type => $value ) { // phpcs:ignore
205208
$key = array_search( $type, $transformation_index, true );
206-
if ( false !== strpos( $type, 'wpsize' ) ) {
207-
if ( ! empty( $item['clean'] ) ) {
208-
continue;
209-
}
210-
211-
$value = '!' . $value . '!';
212-
}
213-
214209
if ( false !== $key ) {
215210
$transform[] = $key . '_' . $value;
216211
}
@@ -231,13 +226,12 @@ function ( $item ) use ( $transformation_index ) {
231226
* Generate a Cloudinary URL.
232227
*
233228
* @param string|null $public_id The Public ID to get a url for.
234-
* @param array $args Additional args.
235-
* @param array $size The WP Size array.
236-
* @param bool $clean Flag to produce a non variable size url.
229+
* @param array $args Additional args.
230+
* @param array $size The WP Size array.
237231
*
238232
* @return string
239233
*/
240-
public function cloudinary_url( $public_id = null, $args = array(), $size = array(), $clean = false ) {
234+
public function cloudinary_url( $public_id = null, $args = array(), $size = array() ) {
241235

242236
if ( null === $public_id ) {
243237
return 'https://' . $this->url( null, null );
@@ -267,22 +261,18 @@ public function cloudinary_url( $public_id = null, $args = array(), $size = arra
267261
if ( ! empty( $args['transformation'] ) ) {
268262
$url_parts[] = self::generate_transformation_string( $args['transformation'] );
269263
}
270-
264+
$base = pathinfo( $public_id );
265+
if ( 'image' === $args['resource_type'] ) {
266+
$new_path = $base['filename'] . '/' . $base['basename'];
267+
$public_id = str_replace( $base['basename'], $new_path, $public_id );
268+
}
271269
// Add size.
272270
if ( ! empty( $size ) && is_array( $size ) ) {
273-
if ( true === $clean ) {
274-
$size['clean'] = true;
275-
}
276-
if ( array_keys( $size ) == array( 0, 1 ) ) {
277-
$size = array(
278-
'width' => $size[0],
279-
'height' => $size[1],
280-
);
281-
if ( $size['width'] === $size['height'] ) {
282-
$size['crop'] = 'fill';
283-
}
284-
}
285271
$url_parts[] = self::generate_transformation_string( array( $size ) );
272+
// add size to ID if scaled.
273+
if ( ! empty( $size['file'] ) ) {
274+
$public_id = str_replace( $base['basename'], $size['file'], $public_id );
275+
}
286276
}
287277

288278
$url_parts[] = $args['version'];
@@ -418,7 +408,7 @@ public function upload( $attachment_id, $args, $headers = array() ) {
418408
$url = $this->url( $resource, 'upload', true );
419409
$args = $this->clean_args( $args );
420410
$disable_https_fetch = get_transient( '_cld_disable_http_upload' );
421-
$file_url = wp_get_attachment_url( $attachment_id );
411+
$file_url = wp_get_original_image_url( $attachment_id );
422412
$media = get_plugin_instance()->get_component( 'media' );
423413
if ( $media && $media->is_cloudinary_url( $file_url ) ) {
424414
// If this is a Cloudinary URL, then we can use it to fetch from that location.
@@ -430,7 +420,7 @@ public function upload( $attachment_id, $args, $headers = array() ) {
430420
} else {
431421
// We should have the file in args at this point, but if the transient was set, it will be defaulting here.
432422
if ( empty( $args['file'] ) ) {
433-
$args['file'] = get_attached_file( $attachment_id );
423+
$args['file'] = wp_get_original_image_path( $attachment_id );
434424
}
435425
// Headers indicate chunked upload.
436426
if ( empty( $headers ) ) {
@@ -466,7 +456,7 @@ public function upload( $attachment_id, $args, $headers = array() ) {
466456
// Hook in flag to allow for non accessible URLS.
467457
if ( is_wp_error( $result ) ) {
468458
$error = $result->get_error_message();
469-
$code = $result->get_error_code();
459+
$code = $result->get_error_code();
470460
/**
471461
* If there's an error and the file is a URL in the error message,
472462
* it's likely due to CURL or the location does not support URL file attachments.

0 commit comments

Comments
 (0)