dongtuan5367 2019-07-19 18:32
浏览 95
已采纳

使用PHP将图像转换为WordPress中的灰度

I'm trying to customize a WordPress plugin which has a function that I wish to add the action to convert colored images to grayscale. I learned that imagefilter($image, IMG_FILTER_GRAYSCALE)does the trick. But I can't seem to get it to work.

public function stream_photo( $image_path, $src, $key, $user_id, $coord, $crop ) {
    $image = wp_get_image_editor( $image_path ); // Return an implementation that extends WP_Image_Editor

    //DEBUG
    imagefilter($image, IMG_FILTER_GRAYSCALE);

    $quality = UM()->options()->get( 'image_compression' );

    if ( ! is_wp_error( $image ) ) {
        if ( ! empty( $crop ) ) {

            if( ! is_array( $crop ) ) {
                $crop = explode(",", $crop );
            }

            $src_x = $crop[0];
            $src_y = $crop[1];
            $src_w = $crop[2];
            $src_h = $crop[3];

            $image->crop( $src_x, $src_y, $src_w, $src_h );

            $max_w = UM()->options()->get('image_max_width');
            if ( $src_w > $max_w ) {
                $image->resize( $max_w, $src_h );
            }
        }

        //DEBUG
        //echo "Image path is: " . $image_path;
        //$image = wp_load_image($image_path);
        //imagefilter($image_path, IMG_FILTER_GRAYSCALE);

        $image->save( $image_path );

        //DEBUG
        //imagefilter($image, IMG_FILTER_GRAYSCALE);

        $image->set_quality( $quality );

        //DEBUG
        //echo "Image path is: " . $image_path;
        //$image2 = wp_load_image($image_path);
        //imagefilter($image2, IMG_FILTER_GRAYSCALE);
    } else {
        wp_send_json_error( esc_js( __( "Unable to crop stream image file: {$image_path}", 'ultimate-member' ) ) );
    }
}
  • 写回答

1条回答 默认 最新

  • duanlinghe8417 2019-07-20 06:10
    关注

    The problem is this lines of code:

    $image = wp_get_image_editor( $image_path ); // Return an implementation that extends WP_Image_Editor
    
    //DEBUG
    imagefilter($image, IMG_FILTER_GRAYSCALE);
    

    wp_get_image_editor return a WP_Image_Editor object, but imagefilter require only 'image resource'. The change below should work.

    $image = wp_get_image_editor( $image_path ); // Return an implementation that extends WP_Image_Editor
    
    $im = imagecreatefrompng($image_path);
    if($im && imagefilter($im, IMG_FILTER_GRAYSCALE)){
        // Convert to grayscale
        imagefilter(imagecreatefrompng($image_path), IMG_FILTER_GRAYSCALE);
        // Save image
        imagepng($im, $image_path);
        imagedestroy($im);
    }
    

    That's for PNG, should you catch all image extension instead of PNG only.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00