[wp-trac] [WordPress Trac] #33278: Naming files with dimensions in the name (/wp-admin/includes/image-edit.php)

WordPress Trac noreply at wordpress.org
Wed Aug 5 15:38:29 UTC 2015


#33278: Naming files with dimensions in the name (/wp-admin/includes/image-
edit.php)
-----------------------------+-----------------------------
 Reporter:  acbaile          |      Owner:
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Media            |    Version:  4.1.6
 Severity:  normal           |   Keywords:
  Focuses:  accessibility    |
-----------------------------+-----------------------------
 Hi. When I create a new dimension for image file in image editor, I get
 file with random name. It is more useful to name it like basic files
 (filename-150x150.ext, filename-300x300.ext). I change for it "/wp-
 admin/includes/image-edit.php". This code first try to use dimension-name
 before using random. Starting from line 707. My code in a pluses.

 {{{

         // Generate new filename.
         $path = get_attached_file($post_id);
         $path_parts = pathinfo( $path );
         $filename = $path_parts['filename'];
         $suffix = time() . rand(100, 999);

         //++++++++++++++++++++++++++++++++++++++++++++++++
         // First try suffix-sizes - mine
         $suffix_sizes = $img->get_size();
         $suffix_sizes =
 "-".$suffix_sizes["width"]."x".$suffix_sizes["height"];
         //++++++++++++++++++++++++++++++++++++++++++++++++

         if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE &&
                 isset($backup_sizes['full-orig']) && $backup_sizes['full-
 orig']['file'] != $path_parts['basename'] ) {

                 if ( 'thumbnail' == $target )
                         $new_path =
 "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";
                 else
                         $new_path = $path;
         } else {
                 //++++++++++++++++++++++++++++++++++++++++++++++++
                 // First try suffix-sizes - mine
                 $filename = preg_replace( "/-e([0-9]+)$/", "", $filename
 );
                 $filename = preg_replace( "/-([0-9]{1,5})x([0-9]{1,5})$/",
 "", $filename );
                 $filename .= $suffix_sizes;
                 $new_filename = $filename.".".$path_parts["extension"];
                 $new_path = $path_parts["dirname"]."/".$new_filename;
                 if ( file_exists($new_path) ){
                 //++++++++++++++++++++++++++++++++++++++++++++++++
                         while( true ) {
                                 $filename = preg_replace( '/-e([0-9]+)$/',
 '', $filename );
                                 $filename .= "-e{$suffix}";
                                 $new_filename =
 "{$filename}.{$path_parts['extension']}";
                                 $new_path =
 "{$path_parts['dirname']}/$new_filename";
                                 if ( file_exists($new_path) )
                                         $suffix++;
                                 else
                                         break;
                         }
                 //++++++++++++++++++++++++++++++++++++++++++++++++
                 }
                 //++++++++++++++++++++++++++++++++++++++++++++++++
         }


 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/33278>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list