【问题标题】:crop an image after resizing in GD library在 GD 库中调整大小后裁剪图像
【发布时间】:2013-03-05 02:19:57
【问题描述】:

我需要先按比例调整图像大小(宽度是重要的尺寸),然后裁剪以剪掉任何多余的高度,然后将新版本存储在目录中。

我已经成功地调整了大小,最终得到了目录中宽度正确的图像。在这里,我需要剪掉多余的高度。但我不知道我需要在哪里做。我是否需要以某种方式使用 copyimageresampled 。我想裁剪所有图像,使它们的高度为 50 像素。

这是我目前调整大小的内容:

$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
ImageJpeg($dst, 'images/' . $_FILES['image']['name']);

【问题讨论】:

    标签: php gd


    【解决方案1】:

    这就是我所追求的。一个2阶段的过程。诀窍是将图像调整为临时图像,然后对其进行裁剪:

    http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html

    【讨论】:

      【解决方案2】:

      这可能有助于您在调整大小后进行裁剪:911-need-code-help

      <?php
        //----------------------------------------------------------------
        // Crop-to-fit PHP-GD
        // Revision 2 [2009-06-01]
        // Corrected aspect ratio of the output image
        //----------------------------------------------------------------
      
        define( 'DESIRED_IMAGE_WIDTH', 150 );
        define( 'DESIRED_IMAGE_HEIGHT', 150 );
      
        $source_path = $_FILES[ 'Image1' ][ 'tmp_name' ];
      
        //
        // Add file validation code here
        //
      
        list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );
      
        switch ( $source_type )
        {
          case IMAGETYPE_GIF:
            $source_gdim = imagecreatefromgif( $source_path );
            break;
      
          case IMAGETYPE_JPEG:
            $source_gdim = imagecreatefromjpeg( $source_path );
            break;
      
          case IMAGETYPE_PNG:
            $source_gdim = imagecreatefrompng( $source_path );
            break;
        }
      
        $source_aspect_ratio = $source_width / $source_height;
        $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;
      
        if ( $source_aspect_ratio > $desired_aspect_ratio )
        {
          //
          // Triggered when source image is wider
          //
          $temp_height = DESIRED_IMAGE_HEIGHT;
          $temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio );
        }
        else
        {
          //
          // Triggered otherwise (i.e. source image is similar or taller)
          //
          $temp_width = DESIRED_IMAGE_WIDTH;
          $temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio );
        }
      
        //
        // Resize the image into a temporary GD image
        //
      
        $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
        imagecopyresampled(
          $temp_gdim,
          $source_gdim,
          0, 0,
          0, 0,
          $temp_width, $temp_height,
          $source_width, $source_height
        );
      
        //
        // Copy cropped region from temporary image into the desired GD image
        //
      
        $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2;
        $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2;
      
        $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT );
        imagecopy(
          $desired_gdim,
          $temp_gdim,
          0, 0,
          $x0, $y0,
          DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
        );
      
        //
        // Render the image
        // Alternatively, you can save the image in file-system or database
        //
      
        header( 'Content-type: image/jpeg' );
        imagejpeg( $desired_gdim );
      
        //
        // Add clean-up code here
        //
      ?>
      

      【讨论】:

        【解决方案3】:

        裁剪就像使用 GD 调整大小

        一些示例代码:

        // Original image
        $filename = 'someimage.jpg';
        
        // Get dimensions of the original image
        list($current_width, $current_height) = getimagesize($filename);
        
        // The x and y coordinates on the original image where we
        // will begin cropping the image
        $left = 50;
        $top = 50;
        
        // This will be the final size of the image (e.g. how many pixels
        // left and down we will be going)
        $crop_width = 200;
        $crop_height = 200;
        
        // Resample the image
        $canvas = imagecreatetruecolor($crop_width, $crop_height);
        $current_image = imagecreatefromjpeg($filename);
        imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
        imagejpeg($canvas, $filename, 100);
        

        您定义了裁剪宽度和高度,并且应该全部设置好。如您所见,它只不过是调整大小而已。

        参考:http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/

        【讨论】:

        • 除非我误读了您的回复,否则我会追求一些微妙的不同。我不想放弃全尺寸图像。我想先按正确的宽度按比例调整它的大小,然后从调整大小的图像中剪掉任何多余的高度。我认为上面的代码只是裁剪全尺寸图像?
        • @elduderino,您正在描述裁剪,无论您是否调整大小,裁剪定义“新”图像尺寸,并切断其他部分(高度/宽度),只需从您的$left & $top 坐标,(0 和 0)并定义裁剪后的最终尺寸(比如 200x200)
        • 我知道我在描述裁剪,但我需要先将图像大小调整到正确的宽度。然后使用生成的图像,我需要剪掉任何多余的高度。裁剪全尺寸图像对我不起作用。我需要找到一种方法来使用我上面的代码,并在我已经在做的调整大小阶段之后合并一个裁剪。我似乎无法正确输入代码。
        猜你喜欢
        • 2010-11-03
        • 2015-02-22
        • 2013-03-01
        • 2012-12-27
        • 2015-10-27
        • 2011-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多