PHP的图像裁剪坐标

时间:2019-08-28 10:29:57

标签: php crop

我有所有坐标的图像信息。我需要裁剪图像的精确选择的部分。

我在wordpress中创建了一个c自定义函数,并传递了所有必填字段。此代码裁剪图像,但未提供准确的输出。

这里的imageId是wordpress中图片的附件ID

 <?php 
      $ft_data = array(
                  "img_w" => 600,
                  "img_h" => 449.94375703037,
                  "w" => 120,
                  "h" => 80,
                  "s" => 100,
                  "x" => 42,
                  "y" => 0
                );

    $crop_coords => array(
                        "x" => 105,
                        "y" => 0,
                        "x2" => 405,
                        "y2" => 200,
                        "w" => 300,
                        "h" => 200
                    );
);

$ imageId = 101;

       function imageCrop($ft_data , $crop_coords , $imageId){
       $json_string = stripslashes($ft_data);
        $data = json_decode($json_string, true);

        $x1     = $data['x'];
        $y1     = $data['y'];
        $width  = $data['w'];
        $height = $data['h'];



        $image_url = wp_get_attachment_url( $imageId );

        $info = pathinfo($image_url);

        // get the filename without the extension
        $image_name =  basename($image_url,'.'.$info['extension']);

        // get the extension without the image name
        $ext    = end(explode('.', $image_url));
        $im     = imagecreatefromfile($image_url);
        $size   = min(imagesx($im), imagesy($im));

        $to_crop_array = array('x' =>$x1 , 'y' => $y1, 'width' => $width, 'height'=> $height );

        $im2    = imagecrop($im, $to_crop_array );

        if ($im2 !== FALSE) {


          $NewImg = $image_name ."-new".date('d-m-y-H-i-s',time())."." . $ext;
          $upload_dir   = wp_upload_dir();

          $savePath = $upload_dir['path']."/".$NewImg;
          echo $newImgUrl = $upload_dir['url']."/".$NewImg;
            session_start();
          $_SESSION["cropedImage"] = $newImgUrl;
          // setcookie('cropedImage', $newImgUrl, time()+3600);
          // WC()->session->set( 'cropedImage', $newImgUrl);

          $res = imagepng($im2, $savePath );
          imagedestroy($im2); 
          die;
        }
          imagedestroy($im);
       }

?>

我有所有坐标的图像信息。我需要裁剪图像的精确选择的部分。

我有一个自定义函数,并传递所有必填字段。该代码裁剪图像,但未提供准确的输出

0 个答案:

没有答案