【问题标题】:PHP - cropping image with imagecopyresampled()?PHP - 使用 imagecopyresampled() 裁剪图像?
【发布时间】:2011-06-25 14:56:24
【问题描述】:

我想使用 imagecreatetruecolor 裁剪图像,但它总是裁剪它留下黑色空间,或者缩放太大。我希望图像正好是 191px 宽和 90px 高,所以我还需要调整图像大小以及裁剪,因为必须保持比例。嗯,有一些项目示例:

调整大小脚本(简化)如下所示:

$src_img=imagecreatefromjpeg($photoTemp);    
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height);

$newImage['crop'] 数组包括:

['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-height']

但我得到的是:

任何人都知道,我做错了什么?

谢谢,迈克。

【问题讨论】:

    标签: php image resize crop


    【解决方案1】:

    你也可以,我自己做的,效果很好

    (x1,y1)=> 裁剪开始的地方

    (x2,y2)=> 裁剪结束的地方

    $filename = $_GET['imageurl'];
    $percent = 0.5;
    
    list($width, $height) = getimagesize($filename);
    
    $new_width  = $_GET['x2'] - $_GET['x1'];
    $new_height = $_GET['y2'] - $_GET['y1'];
    
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    
    imagecopyresampled($image_p, $image, 0, 0 , $_GET['x1'] , $_GET['y1'] , $new_width, $new_height, $new_width, $new_height);
    
    // Outputs the image
    header('Content-Type: image/jpeg');
    imagejpeg($image_p, null, 100);
    

    【讨论】:

      【解决方案2】:

      试试

      <?php
      
      $dst_img = imagecreatetruecolor($newImage['crop']['width'], $newImage['crop']['height']);
      
      imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], 0, 0, $width, $height);
      

      【讨论】:

      • 试过id,我得到的只是一张黑色图像。 =/
      【解决方案3】:

      好的,我自己发现了问题,代码应该是这样的:

      imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']); 
      

      【讨论】:

        【解决方案4】:

        还有imagecrop 函数,它允许您传入具有xywidthheight 值的数组。

        【讨论】:

          猜你喜欢
          • 2016-05-21
          • 2011-03-18
          • 1970-01-01
          • 2015-02-05
          • 1970-01-01
          • 2019-06-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多