图像裁剪功能无法在Wordpress中使用

时间:2017-12-05 10:00:28

标签: php wordpress

我在localhost中使用了以下函数,该函数运行正常

// 图片裁剪

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);
        }
      }
    }

但我在我的wordpress主题中使用它,它显示了一些错误

JFIF?I ^ li766m0ib〜XKRJ W9.7wK〜J),J)的.A_] PW @“{{1M 57#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��� '#' ג}Ø\ PS] ohZy $ | NSQ Yt8f + O

我已使用此代码显示图像

                                    $docimg = $doctor_img['url'];

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

此代码中是否有任何更改。这样我就可以得到图像src而不是有线字符

2 个答案:

答案 0 :(得分:1)

我已经为此找到了解决方案,但我不明白如果png文件将会发生什么。

ob_start();
header( "Content-type: image/jpeg" ); 
cropImage( $docimg, 200, null);
$i = ob_get_clean();
echo "<img src='data:image/jpeg;base64," . base64_encode( $i )."'>";

因为如您所见,只为jpeg文件编写。

答案 1 :(得分:0)

这是工作示例

$parts = explode('.', $docimg);
                                                    $ext = $parts[count($parts) - 1];
                                                    if ($ext == 'jpg' || $ext == 'jpeg') {
                                                        $format = 'jpg';
                                                        } else {
                                                        $format = 'png';
                                                    }
                                                    ob_start();
                                                     header( "Content-type: image/".$format );
                                                     cropImage( $docimg, 200, null);