Index: src/wp-admin/includes/image.php
===================================================================
--- src/wp-admin/includes/image.php	(revision 40106)
+++ src/wp-admin/includes/image.php	(working copy)
@@ -251,7 +251,15 @@
 			$editor = wp_get_image_editor( $file );
 
 			if ( ! is_wp_error( $editor ) ) { // No support for this type of file
-				$uploaded = $editor->save( $file, 'image/jpeg' );
+				/*
+				 * PDFs may have the same file filename as JPGs.
+				 * Ensure the PDF preview image does not overwrite any JPG images that already exist.
+				 */
+				$dirname = dirname( $file );
+				$filename = preg_replace( '/\.[^.]+$/', '', $file )  . '-pdf.pdf';
+				$filename = $dirname . '/' . wp_unique_filename( $dirname, wp_basename( $filename ) );
+
+				$uploaded = $editor->save( $filename, 'image/jpeg' );
 				unset( $editor );
 
 				// Resize based on the full size image, rather than the source.
Index: tests/phpunit/data/images/test-image.pdf
===================================================================
--- tests/phpunit/data/images/test-image.pdf	(revision 0)
+++ tests/phpunit/data/images/test-image.pdf	(working copy)
@@ -0,0 +1,58 @@
+%PDF-1.1
+%¥±ë
+
+1 0 obj
+  << /Type /Catalog
+     /Pages 2 0 R
+  >>
+endobj
+
+2 0 obj
+  << /Type /Pages
+     /Kids [3 0 R]
+     /Count 1
+     /MediaBox [0 0 612 792]
+  >>
+endobj
+
+3 0 obj
+  <<  /Type /Page
+      /Parent 2 0 R
+      /Resources
+       << /Font
+           << /F1
+               << /Type /Font
+                  /Subtype /Type1
+                  /BaseFont /Times-Roman
+               >>
+           >>
+       >>
+      /Contents 4 0 R
+  >>
+endobj
+
+4 0 obj
+  << /Length 55 >>
+stream
+  BT
+    /F1 18 Tf
+    0 0 Td
+    (Hello World) Tj
+  ET
+endstream
+endobj
+
+xref
+0 5
+0000000000 65535 f 
+0000000018 00000 n 
+0000000077 00000 n 
+0000000178 00000 n 
+0000000457 00000 n 
+trailer
+  <<  /Root 1 0 R
+      /Size 5
+  >>
+startxref
+565
+%%EOF

Property changes on: tests/phpunit/data/images/test-image.pdf
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: tests/phpunit/tests/image/functions.php
===================================================================
--- tests/phpunit/tests/image/functions.php	(revision 40106)
+++ tests/phpunit/tests/image/functions.php	(working copy)
@@ -373,25 +373,25 @@
 		$expected = array(
 			'sizes' => array(
 				'thumbnail' => array(
-					'file'      => "wordpress-gsoc-flyer-116x150.jpg",
+					'file'      => "wordpress-gsoc-flyer-pdf-116x150.jpg",
 					'width'     => 116,
 					'height'    => 150,
 					'mime-type' => "image/jpeg",
 				),
 				'medium'    => array(
-					'file'      => "wordpress-gsoc-flyer-232x300.jpg",
+					'file'      => "wordpress-gsoc-flyer-pdf-232x300.jpg",
 					'width'     => 232,
 					'height'    => 300,
 					'mime-type' => "image/jpeg",
 				),
 				'large'     => array(
-					'file'      => "wordpress-gsoc-flyer-791x1024.jpg",
+					'file'      => "wordpress-gsoc-flyer-pdf-791x1024.jpg",
 					'width'     => 791,
 					'height'    => 1024,
 					'mime-type' => "image/jpeg",
 				),
 				'full'      => array(
-					'file'      => "wordpress-gsoc-flyer.jpg",
+					'file'      => "wordpress-gsoc-flyer-pdf.jpg",
 					'width'     => 1088,
 					'height'    => 1408,
 					'mime-type' => "image/jpeg",
@@ -427,7 +427,7 @@
 		add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
 
 		$expected = array(
-			'file'      => 'wordpress-gsoc-flyer-77x100.jpg',
+			'file'      => 'wordpress-gsoc-flyer-pdf-77x100.jpg',
 			'width'     => 77,
 			'height'    => 100,
 			'mime-type' => 'image/jpeg',
@@ -443,6 +443,42 @@
 		unlink( $test_file );
 	}
 
+	/**
+	 * Test PDF preview JPG does not overwrite an image with the same name.
+	 *
+	 * @ticket 39875
+	 */
+	function test_pdf_preview_does_not_overwrite_preexisting_jpg() {
+		$orig_jpg_file = DIR_TESTDATA . '/images/waffles.jpg';
+		$test_jpg_file = '/tmp/waffles.jpg';
+		copy( $orig_jpg_file, $test_jpg_file );
+
+		$jpg_attachment_id = $this->factory->attachment->create_object( $test_jpg_file, 0, array(
+			'post_mime_type' => 'image/jpeg',
+		) );
+
+		$this->assertNotEmpty( $jpg_attachment_id );
+
+		$jpg_metadata = wp_generate_attachment_metadata( $jpg_attachment_id, $test_jpg_file );
+
+		$orig_pdf_file = DIR_TESTDATA . '/images/waffles.pdf';
+		$test_pdf_file = '/tmp/waffles.pdf';
+		copy( $orig_pdf_file, $test_pdf_file );
+
+		$pdf_attachment_id = $this->factory->attachment->create_object( $test_pdf_file, 0, array(
+			'post_mime_type' => 'application/pdf',
+		) );
+
+		$this->assertNotEmpty( $pdf_attachment_id );
+
+		$pdf_metadata = wp_generate_attachment_metadata( $pdf_attachment_id, $test_pdf_file );
+
+		$this->assertEquals( 'waffles-pdf.jpg', $pdf_metadata['sizes']['full']['file'] );
+
+		unlink( $test_jpg_file );
+		unlink( $test_pdf_file );
+	}
+
 	function filter_fallback_intermediate_image_sizes( $fallback_sizes, $metadata ) {
 		// Add the 'test-size' to the list of fallback sizes.
 		$fallback_sizes[] = 'test-size';
