图像裁剪功能无法在Wordpress中运行

I have used this following function in my localhost which is working fine

// Image cropping

function cropImage($sourcePath, $thumbSize, $destination = null) {

      $parts = explode('.', $sourcePath);
      $ext = $parts[count($parts) - 1];
      if ($ext == 'jpg' || $ext == 'jpeg') {
        $format = 'jpg';
      } else {
        $format = 'png';
      }
      if ($format == 'jpg') {
        $sourceImage = imagecreatefromjpeg($sourcePath);
      }
      if ($format == 'png') {
        $sourceImage = imagecreatefrompng($sourcePath);
      }

      list($srcWidth, $srcHeight) = getimagesize($sourcePath);

      // calculating the part of the image to use for thumbnail
      if ($srcWidth > $srcHeight) {
        $y = 0;
        $x = ($srcWidth - $srcHeight) / 2;
        $smallestSide = $srcHeight;
      } else {
        $x = 0;
        $y = ($srcHeight - $srcWidth) / 2;
        $smallestSide = $srcWidth;
      }

      $destinationImage = imagecreatetruecolor($thumbSize, $thumbSize);
      imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

      if ($destination == null) {
        header('Content-Type: image/jpeg');
        if ($format == 'jpg') {
          imagejpeg($destinationImage, null, 100);
        }
        if ($format == 'png') {
          imagejpeg($destinationImage);
        }
        if ($destination = null) {
        }
      } else {
        if ($format == 'jpg') {
          imagejpeg($destinationImage, $destination, 100);
        }
        if ($format == 'png') {
          imagepng($destinationImage, $destination);
        }
      }
    }

But i am using this in my wordpress theme and it is showing some errors

����JFIF����?I��^��l�i�����76����6m�0ib�~���X��KR����J��W�9��.�7wK~��J��)�J��)�.�������A�_�]�pw��@��'��{1M{���5���7�#X��|�w�#9W^�t�]������t}ms�l���%���W{���G��1T+8�2s���0a��3�XX� �yL��FrU��@�Y٦'���r���{g��簪�;c�[�0r0q�q���N9��G��{?�c�w�K�ӯE�|ެ�Td2���F�?!#�G�z>ЀP�����u۝�ᓒ����d��A8��9't��=���b���'#'ג}O��\����P�S�]���o�h�Zy$|�Ns�q��Y��t8f+�O�

I have used this code for showing the image

                                    $docimg = $doctor_img['url'];

                                    if ($docimg):
                                        ?>
                                        <?php cropImage( $docimg, 200, null); ?>

Is there anything to change in this code. So that i can get the image src instead of wired characters

查看全部
dousuo8400
dousuo8400
2017/12/05 10:00
  • wordpress
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复