• Resolved joelstrategycube

    (@joelstrategycube)


    I’ve running a function that creates retina sizes for all media on hook ‘wp_generate_attachment_metadata’

    I’ve added:

    function add_retina_for_cropped_thumbnails( $fullFilePath, $imageSizeName, $imageMetadata ) {
    
        $original = preg_replace( '/-\d+[Xx]\d+\./', '.', $fullFilePath );
        make_it_retina( $original, $imageMetadata['width'], $imageMetadata['height'], true );
        
    }
    add_action( 'crop_thumbnails_after_save_new_thumb', 'add_retina_for_cropped_thumbnails', 10, 3 );

    Which works great except for the fact that the retina images don’t retain the crop, since, well, none of that crop data is being sent to my retina function.

    It seems I have 2 options.

    1) Write the cropping function into my retina function and then pass the crop data to it. Have to scale the cropping data to match the original source file. Is there a chance you would be able to provide a snippet of what that crop function would look like? Below I’ve pasted the metadata I’m receiving from ‘crop_thumbnails_after_save_new_thumb’.

    2) Write all my add_image_size as the retina ‘@2x’ and then when I use Crop Thumbnails, I can downsize to 50%. That way the crop doesn’t matter because I could downsize on the cropped file instead of the original. If I go this way, I’d have to swap files in the database so WP doesn’t default to @2x.

    Hopefully you can weigh in and provide a preferred approach.

    Thanks in advance!

    Metadata:

    "file":"example-800x1000.jpg",
    "width":800,
    "height":1000,
    "mime-type":"image/jpeg",
    "cpt_last_cropping_data":{
        "x":1025,
        "y":0,
        "x2":2275,
        "y2":1565,
        "original_width":2560,
        "original_height":1565
    }

    If I want to apply this same crop to my newly created 1600×2000 image how can I scale the crop to send to wp_crop_image

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Volkmar Kantor

    (@volkmar-kantor)

    Hi,
    i think you need an additional action-hook right before or after the “crop_thumbnails_after_save_new_thumb” with all the input data – that would solve your problem. I will have a look on it in the next week.

    Thread Starter joelstrategycube

    (@joelstrategycube)

    Thanks for the quick reply. I’m not quite sure how an additional action allows me to retina-ize the current thumbnail but I appreciate any time you are able to spend on this. Let me know if there is anything you need from me.

    Thread Starter joelstrategycube

    (@joelstrategycube)

    Just following up on this. Thanks.

    Plugin Author Volkmar Kantor

    (@volkmar-kantor)

    @joelstrategycube
    Sorry about the delay. I added 2 new actions (in version 1.3.0) right before and after the wp_crop function, which should provide all needed variables. Also the wp_crop is now a filter, so its also possible to override the hole functionality all together.

    Have a look on functions\save.php
    Actions:
    crop_thumbnails_before_crop
    crop_thumbnails_after_crop

    Filter:
    crop_thumbnails_do_crop

    Thread Starter joelstrategycube

    (@joelstrategycube)

    @volkmar-kantor
    Thank you! I was able to get something working.

    It’s not fully tested but if anyone stumbles across this:

    /**
     * 
     * Action for Crop Thumbnails plugin
     * Uses input crop data to generate retina size
     * 
     */
    function add_retina_for_cropped_thumbnails( $input, $croppedSize, $temporaryCopyFile, $currentFilePath ) {
    
        $temporaryCopyFile = generate_retina_filename( $temporaryCopyFile, '@2x' );
        $retina_file = generate_retina_filename( $currentFilePath, '@2x' );
    
        $currentFilePathInfo = pathinfo($retina_file);
        $currentFilePathInfo['basename'] = wp_basename($retina_file);//uses the i18n version of the file-basename
        
        $retina_w = $croppedSize['width'] * 2;
        $retina_h = $croppedSize['height'] * 2;
        
        $cropped = wp_crop_image(						    // * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
            $input->sourceImageId,							// * @param string|int $src The source file or Attachment ID.
            $input->selection->x,							// * @param int $src_x The start x position to crop from.
            $input->selection->y,							// * @param int $src_y The start y position to crop from.
            $input->selection->x2 - $input->selection->x,	// * @param int $src_w The width to crop.
            $input->selection->y2 - $input->selection->y,	// * @param int $src_h The height to crop.
            $retina_w,							            // * @param int $dst_w The destination width.
            $retina_h,							            // * @param int $dst_h The destination height.
            false,											// * @param int $src_abs Optional. If the source crop points are absolute.
            $temporaryCopyFile								// * @param string $dst_file Optional. The destination file to write to.
        );
        
        // delete old file
        $should_delete = apply_filters('crop_thumbnails_should_delete_old_file',
            false, // default value
            $input->activeImageSizes->name,
            $input->activeImageSizes,
            $cropped
        );
    
        $_error = false;
        if( !empty($cropped) ) {
            if( $should_delete ) {
                @unlink($currentFilePathInfo['dirname'].DIRECTORY_SEPARATOR.$currentFilePathInfo['basename']);
            }
            if(!@copy($cropped, $retina_file)) {
                $_error = true;
            }
            if(!@unlink($cropped)) {
                $_error = true;
            }
        }    
    }
    add_action( 'crop_thumbnails_before_crop', 'add_retina_for_cropped_thumbnails', 10, 4 );
    
    function generate_retina_filename( $file, $suffix ) {
        $dir = pathinfo( $file, PATHINFO_DIRNAME );
        $ext = pathinfo( $file, PATHINFO_EXTENSION );
     
        $name    = wp_basename( $file, ".$ext" );
        $new_ext = strtolower( $extension ? $extension : $ext );
     
        if ( ! is_null( $dest_path ) ) {
            $_dest_path = realpath( $dest_path );
            if ( $_dest_path ) {
                $dir = $_dest_path;
            }
        }
     
        return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
    }
    Plugin Author Volkmar Kantor

    (@volkmar-kantor)

    Hi @joelstrategycube,
    thank you for sharing your solution.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Retina Integration’ is closed to new replies.